bsw/jbe@1309: /** bsw/jbe@1309: * Toolbar Dialog bsw/jbe@1309: * bsw/jbe@1309: * @param {Element} link The toolbar link which causes the dialog to show up bsw/jbe@1309: * @param {Element} container The dialog container bsw/jbe@1309: * bsw/jbe@1309: * @example bsw/jbe@1309: * bsw/jbe@1309: * insert an image bsw/jbe@1309: * bsw/jbe@1309: * bsw/jbe@1309: *
bsw/jbe@1309: * bsw/jbe@1309: * bsw/jbe@1309: *
bsw/jbe@1309: * bsw/jbe@1309: * bsw/jbe@1309: */ bsw/jbe@1309: (function(wysihtml) { bsw/jbe@1309: var dom = wysihtml.dom, bsw/jbe@1309: CLASS_NAME_OPENED = "wysihtml-command-dialog-opened", bsw/jbe@1309: SELECTOR_FORM_ELEMENTS = "input, select, textarea", bsw/jbe@1309: SELECTOR_FIELDS = "[data-wysihtml-dialog-field]", bsw/jbe@1309: ATTRIBUTE_FIELDS = "data-wysihtml-dialog-field"; bsw/jbe@1309: bsw/jbe@1309: bsw/jbe@1309: wysihtml.toolbar.Dialog = wysihtml.lang.Dispatcher.extend( bsw/jbe@1309: /** @scope wysihtml.toolbar.Dialog.prototype */ { bsw/jbe@1309: constructor: function(link, container) { bsw/jbe@1309: this.link = link; bsw/jbe@1309: this.container = container; bsw/jbe@1309: }, bsw/jbe@1309: bsw/jbe@1309: _observe: function() { bsw/jbe@1309: if (this._observed) { bsw/jbe@1309: return; bsw/jbe@1309: } bsw/jbe@1309: bsw/jbe@1309: var that = this, bsw/jbe@1309: callbackWrapper = function(event) { bsw/jbe@1309: var attributes = that._serialize(); bsw/jbe@1309: that.fire("save", attributes); bsw/jbe@1309: that.hide(); bsw/jbe@1309: event.preventDefault(); bsw/jbe@1309: event.stopPropagation(); bsw/jbe@1309: }; bsw/jbe@1309: bsw/jbe@1309: dom.observe(that.link, "click", function() { bsw/jbe@1309: if (dom.hasClass(that.link, CLASS_NAME_OPENED)) { bsw/jbe@1309: setTimeout(function() { that.hide(); }, 0); bsw/jbe@1309: } bsw/jbe@1309: }); bsw/jbe@1309: bsw/jbe@1309: dom.observe(this.container, "keydown", function(event) { bsw/jbe@1309: var keyCode = event.keyCode; bsw/jbe@1309: if (keyCode === wysihtml.ENTER_KEY) { bsw/jbe@1309: callbackWrapper(event); bsw/jbe@1309: } bsw/jbe@1309: if (keyCode === wysihtml.ESCAPE_KEY) { bsw/jbe@1309: that.cancel(); bsw/jbe@1309: } bsw/jbe@1309: }); bsw/jbe@1309: bsw/jbe@1309: dom.delegate(this.container, "[data-wysihtml-dialog-action=save]", "click", callbackWrapper); bsw/jbe@1309: bsw/jbe@1309: dom.delegate(this.container, "[data-wysihtml-dialog-action=cancel]", "click", function(event) { bsw/jbe@1309: that.cancel(); bsw/jbe@1309: event.preventDefault(); bsw/jbe@1309: event.stopPropagation(); bsw/jbe@1309: }); bsw/jbe@1309: bsw/jbe@1309: this._observed = true; bsw/jbe@1309: }, bsw/jbe@1309: bsw/jbe@1309: /** bsw/jbe@1309: * Grabs all fields in the dialog and puts them in key=>value style in an object which bsw/jbe@1309: * then gets returned bsw/jbe@1309: */ bsw/jbe@1309: _serialize: function() { bsw/jbe@1309: var data = {}, bsw/jbe@1309: fields = this.container.querySelectorAll(SELECTOR_FIELDS), bsw/jbe@1309: length = fields.length, bsw/jbe@1309: i = 0; bsw/jbe@1309: bsw/jbe@1309: for (; ifoo bsw/jbe@1309: * bsw/jbe@1309: * and we have the following dialog: bsw/jbe@1309: * bsw/jbe@1309: * bsw/jbe@1309: * bsw/jbe@1309: * after calling _interpolate() the dialog will look like this bsw/jbe@1309: * bsw/jbe@1309: * bsw/jbe@1309: * bsw/jbe@1309: * Basically it adopted the attribute values into the corresponding input fields bsw/jbe@1309: * bsw/jbe@1309: */ bsw/jbe@1309: _interpolate: function(avoidHiddenFields) { bsw/jbe@1309: var field, bsw/jbe@1309: fieldName, bsw/jbe@1309: newValue, bsw/jbe@1309: focusedElement = document.querySelector(":focus"), bsw/jbe@1309: fields = this.container.querySelectorAll(SELECTOR_FIELDS), bsw/jbe@1309: length = fields.length, bsw/jbe@1309: i = 0; bsw/jbe@1309: for (; i