/* * Inspector control * * Manages properties of inspectable controls. * * Data attributes: * - data-inspectable - makes a control inspectable. * - data-inspector-title - title for the inspector popup * - data-inspector-description - optional description for the inspector popup * - data-inspector-class - class name of the inspectable object. Required for drop-down * fields with dynamic options. * - data-inspector-container - specifies a CSS selector for the inspector container. The default container * is the document body. The container element should be relative positioned. The 'self' container value * allows to inject the inspector to the inspectable element. * - data-inspector-offset - offset in pixels to add to the calculated position, to make the position more "random" * - data-inspector-placement - top | bottom | left | right. The placement could automatically be changed if the popover doesn't fit into the desired position. * - data-inspector-fallback-placement - The placement to use if the default placement * and all other possible placements do not work. The default value is "bottom". * - data-inspector-config - Configuration of the inspector fields as an array in the JSON format. * Each element in the array is an object with the following properties: * - property * - title * - group (optional) * - type (currently supported types are: string, checkbox, dropdown) * - description (optional) * - validationPattern (regex pattern for for validating the value, supported by the text editor) * - validationMessage (a message to display if the validation fails) * - placeholder - placholder text, for text and dropdown properties * - depends - a list of properties the property depend on, for dropdown lists * - options - an option list for dropdown lists, optional. If not provided the options are loaded with AJAX. * Example of the configuration string (a single property): * [{"property":"max-width","title":"Max width","type":"string"}] * * The Inspector requires the inspectable element to contain a hidden input element with the attribute "data-inspector-values". * The inspector uses this field to read and write field values. The field value is a JSON string, an object with keys matching property * names and values matching property values. * * Events * - change - the event is triggered on the inspectable element when it's properties are updated. * - showing.oc.inspector - triggered before the Inspector is displayed. Allows to cancel the action with e.preventDefault() * - hiding.oc.inspector - triggered before the Inspector is hidden. Allows to cancel the action with e.preventDefault() * - hidden.oc.inspector - triggered after the Inspector is hidden. * * Dependences: * - october.popover.js */ +function ($) { "use strict"; if ($.oc === undefined) $.oc = {} $.oc.inspector = { editors: {}, propertyCounter: 0 } // INSPECTOR CLASS DEFINITION // ============================ var Inspector = function(element, options) { this.options = options this.$el = $(element) this.loadConfiguration() } Inspector.prototype.loadConfiguration = function() { var jsonString = this.$el.data('inspector-config') 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)) { 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) 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}} \ \