|
| 1 | +/** Renders the color picker used e.g. in the tool form **/ |
| 2 | +import Utils from "utils/utils"; |
| 3 | +import Ui from "mvc/ui/ui-misc"; |
| 4 | + |
| 5 | +/** Renders an input element used e.g. in the tool form */ |
| 6 | +export default Backbone.View.extend({ |
| 7 | + initialize: function(options) { |
| 8 | + this.model = |
| 9 | + (options && options.model) || |
| 10 | + new Backbone.Model({ |
| 11 | + value: {"src": "json", "value": null, "representation": "null"}, |
| 12 | + }).set(options); |
| 13 | + this.$el = $("<div><p>moo cow</p></div>").addClass("ui-field"); |
| 14 | + console.log(this.model.get("value")); |
| 15 | + var menuButton = new Ui.ButtonMenu({ |
| 16 | + id: "options", |
| 17 | + icon: "fa-caret-down", |
| 18 | + title: "Input Type", |
| 19 | + tooltip: "View available input type options" |
| 20 | + }); |
| 21 | + menuButton.addMenu({ |
| 22 | + title: "Integer", |
| 23 | + onclick: () => { |
| 24 | + this._changeType("integer") |
| 25 | + } |
| 26 | + }); |
| 27 | + menuButton.addMenu({ |
| 28 | + title: "Leave Unselected", |
| 29 | + onclick: () => { |
| 30 | + this._changeType("null") |
| 31 | + } |
| 32 | + }); |
| 33 | + this.$menuButton = menuButton; |
| 34 | + this.$inputDiv = $("<div/>").addClass("select-input"); |
| 35 | + |
| 36 | + this.$el.append(menuButton.$el); |
| 37 | + this.$el.append(this.$inputDiv); |
| 38 | + this.setElement(this.$el); |
| 39 | + this.listenTo(this.model, "change", this.render, this); |
| 40 | + this.render(); |
| 41 | + }, |
| 42 | + value: function(new_val) { |
| 43 | + var options = this.model.attributes; |
| 44 | + if (new_val) { |
| 45 | + this.model.set("value", new_val); |
| 46 | + this.model.trigger("change"); |
| 47 | + options.onchange(new_val); |
| 48 | + } |
| 49 | + return this.model.get("value"); |
| 50 | + }, |
| 51 | + render: function() { |
| 52 | + const value = this.model.get("value"); |
| 53 | + const rep = value.representation; |
| 54 | + if ( rep == "null" ) { |
| 55 | + this.$inputDiv.html($("<p>No value selected (null)</p>")); |
| 56 | + } else if ( rep == "integer" ) { |
| 57 | + const tagName = this.model.get("area") ? "textarea" : "input"; |
| 58 | + this.$inputDiv.html($(`<${tagName} value="${value.value}"/>`)); |
| 59 | + console.log(this.$inputDiv.find("input")); |
| 60 | + this.$inputDiv.find("input").on("change", () => { this._onchange() }); |
| 61 | + } |
| 62 | + return this; |
| 63 | + }, |
| 64 | + _changeType: function(representation) { |
| 65 | + const previousValue = this.model.get("value"); |
| 66 | + const previousRawValue = previousValue.value; |
| 67 | + if ( representation == "null" ) { |
| 68 | + this.model.set("value", {"src": "json", "value": null, "representation": "null"}); |
| 69 | + } else if ( representation == "integer" ) { |
| 70 | + var value = parseInt(previousRawValue); |
| 71 | + if ( isNaN( value ) ) { |
| 72 | + value = 0; |
| 73 | + } |
| 74 | + this.model.set("value", {"src": "json", "value": 0, "representation": "integer"}); |
| 75 | + } |
| 76 | + }, |
| 77 | + _rawValue: function(previousValue) { |
| 78 | + const rep = previousValue.representation; |
| 79 | + let rawVal; |
| 80 | + if ( rep == "null" ) { |
| 81 | + rawVal = null; |
| 82 | + } else if ( rep == "integer" ) { |
| 83 | + rawVal = parseInt(this.$inputDiv.find("input").val()); |
| 84 | + } |
| 85 | + console.log("_rawValue returning " + rawVal); |
| 86 | + return rawVal; |
| 87 | + |
| 88 | + }, |
| 89 | + _onchange: function() { |
| 90 | + const previousValue = this.model.get("value"); |
| 91 | + const newValue = this._rawValue(previousValue); |
| 92 | + previousValue["value"] = newValue; |
| 93 | + this.value(previousValue); |
| 94 | + this.model.get("onchange") && this.model.get("onchange")(this.model.get("value")); |
| 95 | + } |
| 96 | +}); |
0 commit comments