/* * 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 - PHP class name of the inspectable object. Required for drop-down * fields with dynamic options. * - data-inspector-css-class - CSS class name to apply to the Inspector container element. * - 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 * - default - default value * - 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. * - showExternalParam - specifies the visibility of the external parameter feature for the property. Default: true. * 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. * * Any HTML element that wraps the inspectable element can have the data-inspector-external-parameters property that enables the external * parameters support. External parameters saved with the special syntax {{ paramName }}. The external parameter feature can be toggled * on per-property basis with the showExternalParam option, visible by default. * * 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.title = false this.description = false } 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}} \ \