// 'use strict'; // goog.provide('Blockly.Blocks.clouddata'); // goog.require('Blockly.Blocks'); // goog.require('Blockly.Types'); // Blockly.Blocks.clouddata.HUE = 130; // /**************************************** // * * * * * * * * * // * Simple version for CocoCloud * // * Generate JSON * // * * * * * * * * * // ****************************************/ // /*JSON tool // *create a main JSON object // *name a JSON // * // */ // Blockly.Blocks['clouddata_json_generator'] = { // /** // * Block for creating a JSON object with any number of // * elements of [key:value] . // * @this Blockly.Block // */ // init: function() { // this.appendDummyInput() // .appendField(Blockly.Msg.CLOUDDATA_GENERATOR_DATA) // .appendField(new Blockly.FieldTextInput("data"), "VAR"); // this.itemCount_ = 1; // this.updateShape_(); // this.setMutator(new Blockly.Mutator(['clouddata_create_with_item'])); // this.setPreviousStatement(true); // this.setNextStatement(true); // this.setTooltip(''); // this.setHelpUrl(''); // this.setColour(Blockly.Blocks.clouddata.HUE); // }, // /** // * Create XML to represent number of inputs. // * @return {!Element} XML storage element. // * @this Blockly.Block // */ // mutationToDom: function() { // var container = document.createElement('mutation'); // container.setAttribute('items', this.itemCount_); // return container; // }, // /** // * Parse XML to restore the inputs. // * @param {!Element} xmlElement XML storage element. // * @this Blockly.Block // */ // domToMutation: function(xmlElement) { // this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10); // this.updateShape_(); // }, // /** // * Populate the mutator's dialog with this block's components. // * @param {!Blockly.Workspace} workspace Mutator's workspace. // * @return {!Blockly.Block} Root block in mutator. // * @this Blockly.Block // */ // decompose: function(workspace) { // var containerBlock = workspace.newBlock('clouddata_create_with_container'); // containerBlock.initSvg(); // var connection = containerBlock.getInput('STACK').connection; // for (var i = 0; i < this.itemCount_; i++) { // var itemBlock = workspace.newBlock('clouddata_create_with_item'); // itemBlock.initSvg(); // connection.connect(itemBlock.previousConnection); // connection = itemBlock.nextConnection; // } // return containerBlock; // }, // /** // * Reconfigure this block based on the mutator dialog's components. // * @param {!Blockly.Block} containerBlock Root block in mutator. // * @this Blockly.Block // */ // compose: function(containerBlock) { // var itemBlock = containerBlock.getInputTargetBlock('STACK'); // // Count number of inputs. // var connections = []; // while (itemBlock) { // connections.push(itemBlock.valueConnection_); // itemBlock = itemBlock.nextConnection && // itemBlock.nextConnection.targetBlock(); // } // // Disconnect any children that don't belong. // for (var i = 0; i < this.itemCount_; i++) { // var connection = this.getInput('ADD' + i).connection.targetConnection; // if (connection && connections.indexOf(connection) == -1) { // connection.disconnect(); // } // } // this.itemCount_ = connections.length; // this.updateShape_(); // // Reconnect any child blocks. // for (var i = 0; i < this.itemCount_; i++) { // Blockly.Mutator.reconnect(connections[i], this, 'ADD' + i); // } // }, // /** // * Store pointers to any connected child blocks. // * @param {!Blockly.Block} containerBlock Root block in mutator. // * @this Blockly.Block // */ // saveConnections: function(containerBlock) { // var itemBlock = containerBlock.getInputTargetBlock('STACK'); // var i = 0; // while (itemBlock) { // var input = this.getInput('ADD' + i); // itemBlock.valueConnection_ = input && input.connection.targetConnection; // i++; // itemBlock = itemBlock.nextConnection && // itemBlock.nextConnection.targetBlock(); // } // }, // /** // * Modify this block to have the correct number of inputs. // * @private // * @this Blockly.Block // */ // updateShape_: function() { // if (this.itemCount_ && this.getInput('EMPTY')) { // this.removeInput('EMPTY'); // } else if (!this.itemCount_ && !this.getInput('EMPTY')) { // this.appendDummyInput('EMPTY') // .appendField(this.newQuote_(true)) // .appendField(this.newQuote_(false)); // } // // Add new inputs. // for (var i = 0; i < this.itemCount_; i++) { // if (!this.getInput('ADD' + i)) { // var input = this.appendValueInput('ADD' + i); // input.appendField(Blockly.Msg.CLOUDDATA_CREATE_WITH_INPUT_WITH) // .appendField(new Blockly.FieldTextInput("key"), 'KEY' + i); // } // } // // Remove deleted inputs. // while (this.getInput('ADD' + i)) { // this.removeInput('ADD' + i); // i++; // } // }, // }; // Blockly.Blocks['clouddata_create_with_container'] = { // /** // * Mutator block for list container. // * @this Blockly.Block // */ // init: function() { // this.setColour(Blockly.Blocks.clouddata.HUE); // this.appendDummyInput() // .appendField(Blockly.Msg.CLOUDDATA_CREATE_WITH_CONTAINER_TITLE_ADD); // this.appendStatementInput('STACK'); // this.setTooltip(''); // this.contextMenu = false; // } // }; // Blockly.Blocks['clouddata_create_with_item'] = { // /** // * Mutator bolck for adding items. // * @this Blockly.Block // */ // init: function() { // this.setColour(Blockly.Blocks.clouddata.HUE); // this.appendDummyInput() // .appendField(Blockly.Msg.CLOUDDATA_CREATE_WITH_ITEM_TITLE); // this.setPreviousStatement(true); // this.setNextStatement(true); // this.setTooltip(''); // this.contextMenu = false; // } // }; // /*cloud data output // *expose JSONmessageBuffer to user // * // */ // Blockly.Blocks['clouddata_parse_prettyPrint_display'] = { // init: function() { // this.appendDummyInput() // .appendField(Blockly.Msg.CLOUDDATA_PARSE_PRETTYPRINT_DISPLAY); // this.setColour(Blockly.Blocks.clouddata.HUE); // this.setOutput(true, Blockly.Types.TEXT.output); // // this.setPreviousStatement(true, null); // // this.setNextStatement(true, null); // // this.setInputsInline(true); // this.setTooltip(""); // this.setHelpUrl(""); // }, // }; // /**************************************** // * * * * * * * * * // * Simple version for CocoCloud * // * Parse JSON * // * * * * * * * * * // ****************************************/ // Blockly.Blocks['clouddata_json_parser_setup'] = { // init: function() { // this.appendDummyInput() // .appendField(Blockly.Msg.CLOUDDATA_PARSE_SETUP_INIT) // .appendField(new Blockly.FieldTextInput("Data"), "DATA"); // this.appendValueInput("PAYLOAD") // .appendField(Blockly.Msg.CLOUDDATA_PARSE_SETUP_PAYLOAD); // this.setPreviousStatement(true, null); // this.setNextStatement(true, null); // this.setColour(Blockly.Blocks.clouddata.HUE); // this.setTooltip(""); // this.setHelpUrl(""); // } // } // Blockly.Blocks['clouddata_json_parser'] = { // init: function() { // this.appendDummyInput() // .appendField(Blockly.Msg.CLOUDDATA_PARSE_KEY) // .appendField(new Blockly.FieldTextInput("key"), "KEY") // .appendField(Blockly.Msg.CLOUDDATA_PARSE_TYPE) // .appendField(new Blockly.FieldDropdown( // [ // ["Text", "Text"], // ["Number", "Number"], // ["Boolean", "Boolean"], // ["Large Number", "Large Number"], // ["Decimal", "Decimal"] // ]), 'VAR_TYPE') // .appendField(Blockly.Msg.CLOUDDATA_PARSE_JSON) // .appendField(new Blockly.FieldTextInput("Data"), "DATA"); // this.setColour(Blockly.Blocks.clouddata.HUE); // this.setOutput(true, this.getBlockType()); // // this.setPreviousStatement(true, null); // // this.setNextStatement(true, null); // this.setInputsInline(true); // this.setTooltip(""); // this.setHelpUrl(""); // }, // getBlockType: function() { // var blocklyTypeKey = this.getFieldValue('VAR_TYPE'); // return Blockly.Types[blocklyTypeKey]; // }, // }; // /**************************************** // * * * * * * * * * // * Simple version for CocoCloud * // * cloud service * // * * * * * * * * * // ****************************************/ // /** // *CocoCloud Service // *method POST a http request with a JSON data // *@param url :http://staging.cocorobo.cn/cloud/iot/{events} or http://cocorobo.cn/cloud/iot/{events} // *@param event get from URL://cocorobo.cn/cloud/iot/data/apikey/{@param APIKEY}/event // *@param data JSON form - generate from clouddata blocks // */ // Blockly.Blocks['cococloud_simple_post'] = { // init: function() { // this.appendDummyInput() // .appendField(Blockly.Msg.COCOCLOUD_TITLE); // this.appendValueInput('ADD0') // .appendField(Blockly.Msg.COCOCLOUD_POST) // .appendField(new Blockly.FieldDropdown([ // ["event", "event"] // ]), "event0"); // this.itemCount = 1; // this.updateShape_(); // this.eventTag = null; // this.setColour(150); // this.setPreviousStatement(true, null); // this.setNextStatement(true, null); // this.setTooltip(""); // this.setHelpUrl(""); // }, // /** // * Modify this block to have the correct number of inputs. // * @private // * @this Blockly.Block // */ // updateShape_: function() { // // Add new inputs. // var self = this; // var input = this.getInput('ADD0'); // var api_key = document.getElementById('api-key').innerHTML || ""; // $.ajax({ // // url: '//api.cocorobo.cn/iot/data/apikey/' + api_key + '/event/', // url: '//staging.cocorobo.cn/api/iot/data/apikey/' + api_key + '/event/', // xhrFields: { // withCredentials: true // }, // type: 'GET', // success: function(data) { // var eventsName = []; // if (data != null) { // $.each(data, function(i, obj) { // var temparray = [] // temparray.push(obj.name); // temparray.push(obj.url); // eventsName.push(temparray); // }); // self.eventsName = eventsName; // input.removeField("event0"); // input.appendField(new Blockly.FieldDropdown(eventsName), "event0"); // } // }, // error: function(error) { // console.log("error"); // } // }); // } // }; // /** // *CocoCloud Service // *method GET a http request to get a JSON data // *@param url :http://staging.cocorobo.cn/cloud/iot/{events} or http://cocorobo.cn/cloud/iot/{events} // *@param event get from URL://cocorobo.cn/cloud/iot/data/apikey/{@param APIKEY}/event // * data JSON form // */ // Blockly.Blocks['cococloud_simple_get'] = { // init: function() { // this.appendDummyInput() // .appendField(Blockly.Msg.COCOCLOUD_TITLE); // this.appendDummyInput("iuput_id0") // .appendField(Blockly.Msg.COCOCLOUD_GET, "text_field0") // .appendField(new Blockly.FieldDropdown([ // ["event", "event"] // ]), "event0") // .appendField(Blockly.Msg.COCOCLOUD_GET1, "text_field1"); // this.itemCount = 1; // this.updateShape_(); // this.setColour(150); // this.setPreviousStatement(true, null); // this.setNextStatement(true, null); // this.setTooltip(""); // this.setHelpUrl(""); // }, // /** // * Modify this block to have the correct number of inputs. // * @private // * @this Blockly.Block // */ // updateShape_: function() { // // Add new inputs. // var self = this; // var input = this.getInput('iuput_id0'); // var api_key = document.getElementById('api-key').innerHTML || ""; // $.ajax({ // // url: '//api.cocorobo.cn/iot/data/apikey/' + api_key + '/event/', // url: '//staging.cocorobo.cn/api/iot/data/apikey/' + api_key + '/event/', // xhrFields: { // withCredentials: true // }, // type: 'GET', // success: function(data) { // var eventsName = []; // if (data != null) { // $.each(data, function(i, obj) { // var temparray = []; // temparray.push(obj.name); // temparray.push(obj.url); // eventsName.push(temparray); // }); // self.eventsName = eventsName; // input.removeField("text_field0"); // input.removeField("event0"); // input.removeField("text_field1"); // input.appendField(Blockly.Msg.COCOCLOUD_GET, "text_field0") // .appendField(new Blockly.FieldDropdown(eventsName), "event0") // .appendField(Blockly.Msg.COCOCLOUD_GET1, "text_field1"); // } // }, // error: function(error) { // console.log("error"); // } // }); // } // }; // /** // *CocoCloud Service // * get JSON data from cococloud-get-block // */ // Blockly.Blocks['cococloud_simple_getPayLoad'] = { // init: function() { // this.appendDummyInput() // .appendField(Blockly.Msg.COCOCLOUD_TITLE); // this.appendDummyInput() // .appendField(Blockly.Msg.COCOCLOUD_GETPAYLOAD); // this.setColour(150); // this.setOutput(true); // this.setTooltip(""); // this.setHelpUrl(""); // }, // }; // /** // * Time Service // * get standard time // * Setup // */ // Blockly.Blocks['cloud_time_setup'] = { // init: function() { // this.appendDummyInput() // .appendField(new Blockly.FieldImage("http://cocorobo.cn/cocoblockly/blockly/media/main-sensor101-counter.png", 50, 40, "15")); // this.appendDummyInput() // .appendField(Blockly.Msg.COCOCLOUD_TIME_SETUP); // this.setColour(215); // this.setTooltip(""); // this.setHelpUrl(""); // }, // } // /** // * Time Service // * get standard time // * get Date // */ // Blockly.Blocks['cloud_time_getDate'] = { // init: function() { // this.appendDummyInput() // .appendField(Blockly.Msg.COCOCLOUD_TIME_GETDATE) // .appendField(new Blockly.FieldDropdown([ // [Blockly.Msg.COCOCLOUD_TIME_YEAR, "year"], // [Blockly.Msg.COCOCLOUD_TIME_MONTH, "month"], // [Blockly.Msg.COCOCLOUD_TIME_DAY, "day"], // ]), "date_detail") // this.setColour(215); // this.setOutput(true); // this.setTooltip(""); // this.setHelpUrl(""); // }, // }; // /** // * Time Service // * get standard time // * get Time // */ // Blockly.Blocks['cloud_time_getTime'] = { // init: function() { // this.appendDummyInput() // .appendField(Blockly.Msg.COCOCLOUD_TIME_GETTIME) // .appendField(new Blockly.FieldDropdown([ // [Blockly.Msg.COCOCLOUD_TIME_HOUR, "hour"], // [Blockly.Msg.COCOCLOUD_TIME_MINUTE, "minute"], // [Blockly.Msg.COCOCLOUD_TIME_SECOND, "second"], // ]), "time_detail") // this.setColour(215); // this.setOutput(true); // this.setTooltip(""); // this.setHelpUrl(""); // }, // };