Added string list Inspector editor

This commit is contained in:
alekseybobkov 2015-10-04 15:00:59 -07:00
parent ae2f3bb62e
commit b0fa402cbe
1 changed files with 104 additions and 0 deletions

View File

@ -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);