var CustomFields = CustomFields || {}; CustomFields.FieldFilter = function (text, dataSource) { CustomFields.FieldFilter.INITWORDS = dataSource; CustomFields.FieldFilter.WORDS = CustomFields.FieldFilter.INITWORDS; CustomFields.FieldFilter.superClass_.constructor.call(this, text); this.setSpellcheck(false); this.clickWrapper_ = null; this.moveWrapper_ = null; }; Blockly.utils.object.inherits(CustomFields.FieldFilter, Blockly.FieldTextInput); CustomFields.FieldFilter.fromJson = function (options) { return new CustomFields.FieldFilter(options['fieldSearch']); }; //CustomFields.FieldFilter.INITWORDS = text; //CustomFields.FieldFilter.INITWORDS = ["", "aaa", "abc", "add", "bbb", "bcd", "ccc", "def", "deg"]; //CustomFields.FieldFilter.INITWORDS = [{ "id": "Section002", "parentId": "", "children": [{ "id": "Section003", "parentId": "Section002", "children": [{ "id": "Section004", "parentId": "Section003", "children": [{ "id": "TextBox005", "parentId": "Section004", "children": [] }] }, { "id": "Section006", "parentId": "Section003", "children": [{ "id": "TextBox007", "parentId": "Section006", "children": [] }] }, { "id": "Section008", "parentId": "Section003", "children": [{ "id": "TextBox009", "parentId": "Section008", "children": [] }] }, { "id": "Section005", "parentId": "Section003", "children": [] }] }] }, { "id": "Section678", "parentId": "", "children": [{ "id": "ID", "parentId": "Section678", "children": [] }, { "id": "PID", "parentId": "Section678", "children": [] }, { "id": "ParentID", "parentId": "Section678", "children": [] }, { "id": "AUR_ModifiedBy", "parentId": "Section678", "children": [] }, { "id": "AUR_ModifiedOn", "parentId": "Section678", "children": [] }] }]; CustomFields.FieldFilter.WORDS = CustomFields.FieldFilter.INITWORDS; CustomFields.FieldFilter.prototype.showEditor_ = function () { CustomFields.FieldFilter.superClass_.showEditor_.call(this); var div = Blockly.WidgetDiv.DIV; if (!div.firstChild) { return; } var editor = this.dropdownCreate_(); Blockly.DropDownDiv.getContentDiv().appendChild(editor); Blockly.DropDownDiv.setColour(this.sourceBlock_.style.colourPrimary, this.sourceBlock_.style.colourTertiary); Blockly.DropDownDiv.showPositionedByField( this, this.dropdownDispose_.bind(this)); this.clickWrapper_ = Blockly.bindEvent_(this.imageElement_, 'click', this, this.hide_); this.moveWrapper_ = Blockly.bindEvent_(this.imageElement_, 'mousemove', this, this.onMouseMove); this.downWrapper_ = Blockly.bindEvent_(this.imageElement_, 'mousedown', this, this.onMouseDown); this.updateGraph_(); }; CustomFields.FieldFilter.prototype.dropdownCreate_ = function () { this.imageElement_ = document.createElement('div'); this.imageElement_.id = 'fieldSearch'; CustomFields.FieldFilter.WORDS = CustomFields.FieldFilter.INITWORDS; var optionsLength = CustomFields.FieldFilter.WORDS.length; var height = 24.4 * optionsLength; this.imageElement_.style = 'border: 1px solid #ccc;height: ' + height + 'px;width: 150px;size: 12px;padding: 0px'; this.imageElement_.innerHTML = CustomFields.FieldFilter.WORDS.join("
"); return this.imageElement_; }; CustomFields.FieldFilter.prototype.dropdownDispose_ = function () { if (this.clickWrapper_) { Blockly.unbindEvent_(this.clickWrapper_); this.clickWrapper_ = null; } if (this.moveWrapper_) { Blockly.unbindEvent_(this.moveWrapper_); this.moveWrapper_ = null; } if (this.downWrapper_) { Blockly.unbindEvent_(this.downWrapper_); this.downWrapper_ = null; } this.imageElement_ = null; }; CustomFields.FieldFilter.prototype.hide_ = function () { Blockly.WidgetDiv.hide(); Blockly.DropDownDiv.hideWithoutAnimation(); }; CustomFields.FieldFilter.prototype.onMouseMove = function (e) { var bBox = this.imageElement_.getBoundingClientRect(); var dy = e.clientY - bBox.top; var highLight = Array.from(CustomFields.FieldFilter.WORDS); var note = (Math.round((dy - 5) / 24.5) < highLight.length) ? Math.round((dy - 5) / 24.5) : -1; if (note != -1) highLight[note] = "" + highLight[note] + ""; this.imageElement_.innerHTML = highLight.join("
"); }; CustomFields.FieldFilter.prototype.onMouseDown = function (e) { var bBox = this.imageElement_.getBoundingClientRect(); var dy = e.clientY - bBox.top; var highLight = Array.from(CustomFields.FieldFilter.WORDS); var note = (Math.round((dy - 5) / 24.5) < highLight.length) ? Math.round((dy - 5) / 24.5) : -1; this.setEditorValue_(note); }; CustomFields.FieldFilter.prototype.valueToNote = function (value) { return CustomFields.FieldFilter.WORDS[Number(value)] || ""; }; CustomFields.FieldFilter.prototype.noteToValue = function (text) { var normalizedText = text.trim(); var i = CustomFields.FieldFilter.WORDS.indexOf(normalizedText); CustomFields.FieldFilter.WORDS = []; var words = CustomFields.FieldFilter.WORDS; var initwords = CustomFields.FieldFilter.INITWORDS; for (var i = 0; i < initwords.length; i++) { if (initwords[i].indexOf(normalizedText) != -1 || normalizedText == "") words.push(initwords[i]); } var optionsLength = CustomFields.FieldFilter.WORDS.length; var height = 24.4 * optionsLength; this.imageElement_.style = 'border: 1px solid #ccc;height: ' + height + 'px;width: 150px;;size: 12px;padding: 0px'; this.imageElement_.innerHTML = CustomFields.FieldFilter.WORDS.join("
"); return i > -1 ? i : -1; }; CustomFields.FieldFilter.prototype.getText_ = function () { if (this.isBeingEdited_) { return CustomFields.FieldFilter.superClass_.getText_.call(this); } return this.valueToNote(this.getValue()) || null; }; CustomFields.FieldFilter.prototype.getEditorText_ = function (value) { return this.valueToNote(value); }; CustomFields.FieldFilter.prototype.getValueFromEditorText_ = function (text) { return this.noteToValue(text); }; CustomFields.FieldFilter.prototype.render_ = function () { CustomFields.FieldFilter.superClass_.render_.call(this); this.updateGraph_(); }; CustomFields.FieldFilter.prototype.updateGraph_ = function () { if (!this.imageElement_) { return; } }; CustomFields.FieldFilter.prototype.doClassValidation_ = function (opt_newValue) { if (opt_newValue === null || opt_newValue === undefined) { return null; } var note = this.valueToNote(opt_newValue); if (note) { return opt_newValue; } return null; }; Blockly.fieldRegistry.register('field_filter', CustomFields.FieldFilter);