/* =require ../vendor/modernizr/modernizr.js =require ../vendor/mustache/mustache.js =require foundation.js =require popover.js =require tooltip.js =require checkbox.js =require select.js */ /* * Inspector control * * - Documentation: ../docs/inspector.md */ +function ($) { "use strict"; var Base = $.oc.foundation.base, BaseProto = Base.prototype if ($.oc === undefined) $.oc = {} $.oc.inspector = { editors: {}, propertyCounter: 0 } // INSPECTOR CLASS DEFINITION // ============================ var Inspector = function(element, options) { this.options = options this.$el = $(element) this.title = false this.description = false Base.call(this) } Inspector.prototype = Object.create(BaseProto) Inspector.prototype.constructor = Inspector Inspector.prototype.loadConfiguration = function(onSuccess) { var configString = this.$el.data('inspector-config') if (configString !== undefined) { // If the data-inspector-config attribute is provided, // load the configuration from it this.parseConfiguration(configString) if (onSuccess !== undefined) onSuccess(); } else { // If the data-inspector-config attribute is not provided, // request the configuration from the server. var $form = $(this.selector).closest('form'), data = this.$el.data(), self = this $.oc.stripeLoadIndicator.show() var request = $form.request('onGetInspectorConfiguration', { data: data }).done(function(data) { self.parseConfiguration(data.configuration.properties) if (data.configuration.title !== undefined) self.title = data.configuration.title if (data.configuration.description !== undefined) self.description = data.configuration.description $.oc.stripeLoadIndicator.hide() if (onSuccess !== undefined) onSuccess(); }).always(function() { $.oc.stripeLoadIndicator.hide() }) } } Inspector.prototype.parseConfiguration = function(jsonString) { if (jsonString === undefined) throw new Error('The Inspector cannot be initialized because the Inspector configuration ' + 'attribute is not defined on the inspectable element.'); if (!$.isArray(jsonString) && !$.isPlainObject(jsonString)) { try { this.config = $.parseJSON(jsonString) } catch(err) { throw new Error('Error parsing the Inspector field configuration. ' + err) } } else this.config = jsonString this.propertyValuesField = $('input[data-inspector-values]', this.$el) // Inspector saves property values to data-property-xxx attributes if the input[data-inspector-values] // doesn't exist, so the condition is not required. // // if (this.propertyValuesField.length === 0) // throw new Error('Error initializing the Inspector: the inspectable element should contain ' + // 'an hidden input element with the data-inspector-values property.') } Inspector.prototype.getPopoverTemplate = function() { return ' \
{{description}}
\ {{/description}} \ \