From b0fa402cbe14b7f04e0664e40dfa0d0f560e25f7 Mon Sep 17 00:00:00 2001 From: alekseybobkov Date: Sun, 4 Oct 2015 15:00:59 -0700 Subject: [PATCH] Added string list Inspector editor --- .../ui/js/inspector.editor.stringlist.js | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 modules/system/assets/ui/js/inspector.editor.stringlist.js diff --git a/modules/system/assets/ui/js/inspector.editor.stringlist.js b/modules/system/assets/ui/js/inspector.editor.stringlist.js new file mode 100644 index 000000000..4bd938e9b --- /dev/null +++ b/modules/system/assets/ui/js/inspector.editor.stringlist.js @@ -0,0 +1,104 @@ +/* + * Inspector string list editor class. + */ ++function ($) { "use strict"; + + var Base = $.oc.inspector.propertyEditors.text, + BaseProto = Base.prototype + + var StringListEditor = function(inspector, propertyDefinition, containerCell, group) { + Base.call(this, inspector, propertyDefinition, containerCell, group) + } + + StringListEditor.prototype = Object.create(BaseProto) + StringListEditor.prototype.constructor = Base + + StringListEditor.prototype.setLinkText = function(link, value) { + var value = value !== undefined ? value + : this.inspector.getPropertyValue(this.propertyDefinition.property) + + if (value === undefined) { + value = this.propertyDefinition.default + } + + this.checkValueType(value) + + if (!value) { + value = this.propertyDefinition.placeholder + $.oc.foundation.element.addClass(link, 'placeholder') + + if (!value) { + value = '[]' + } + + link.textContent = value + } + else { + $.oc.foundation.element.removeClass(link, 'placeholder') + + link.textContent = '[' + value.join(', ') + ']' + } + } + + StringListEditor.prototype.checkValueType = function(value) { + if (value && Object.prototype.toString.call(value) !== '[object Array]') { + throw new Error('The string list value should be an array.') + } + } + + StringListEditor.prototype.onPopupShown = function(ev, link, popup) { + var $textarea = $(popup).find('textarea'), + value = this.inspector.getPropertyValue(this.propertyDefinition.property) + + $(popup).on('submit.inspector', 'form', this.proxy(this.onSubmit)) + + if (this.propertyDefinition.placeholder) { + $textarea.attr('placeholder', this.propertyDefinition.placeholder) + } + + if (value === undefined) { + value = this.propertyDefinition.default + } + + this.checkValueType(value) + + if (value && value.length) { + $textarea.val(value.join('\n')) + } + + $textarea.focus() + } + + StringListEditor.prototype.onSubmit = function(ev) { + ev.preventDefault() + + var $form = $(ev.target), + $textarea = $form.find('textarea'), + link = this.getLink(), + value = $.trim($textarea.val()), + arrayValue = [], + resultValue = [] + + if (value.length) { + value = value.replace(/\r\n/g, '\n') + arrayValue = value.split('\n') + + for (var i = 0, len = arrayValue.length; i < len; i++) { + var currentValue = $.trim(arrayValue[i]) + + if (currentValue.length > 0) { + resultValue.push(currentValue) + } + } + } + + this.inspector.setPropertyValue(this.propertyDefinition.property, resultValue) +// TODO: validate here + + this.setLinkText(link) + $(link).popup('hide') + return false + } + + $.oc.inspector.propertyEditors.stringList = StringListEditor +}(window.jQuery); \ No newline at end of file