var SideBlockAdmin = (function () { var SideBlockAdmin = function() { this.initialize.apply(this, arguments); }; SideBlockAdmin.defaultOptions = { formId: undefined, TYPE_HTML: undefined, PAGE_CUSTOM: undefined, LINK_NONE: undefined, LINK_PAGE: undefined, LINK_URL: undefined }; SideBlockAdmin.prototype = { initialize: function (options) { this.options = $.extend({}, SideBlockAdmin.defaultOptions, options); this.initForm(); return this; }, initForm: function () { this.$form = $('#' + this.options.formId); if (!this.$form.size()) { return this; } var rules = { blockType: [{ _value: this.options.TYPE_HTML, toggle: '#content-label, #content-element' }], pageType: [{ _value: this.options.PAGE_CUSTOM, toggle: '#idPage-label, #idPage-element' }], linkType: [ { _value: this.options.LINK_NONE, hide: '#linkTitle-label, #linkTitle-element, #linkPageId-label, #linkPageId-element, #linkUrl-label, #linkUrl-element' }, { _value: this.options.LINK_PAGE, hide: '#linkUrl-label, #linkUrl-element', show: '#linkTitle-label, #linkTitle-element, #linkPageId-label, #linkPageId-element' }, { _value: this.options.LINK_URL, hide: '#linkPageId-label, #linkPageId-element', show: '#linkTitle-label, #linkTitle-element, #linkUrl-label, #linkUrl-element' } ] }; var that = this, name, target; for (name in rules) { if (rules.hasOwnProperty(name)) { target = this.$form.find('#' + name); that.onElementChange(target, name, rules[name]); target.bind('change.SideBlockAdmin', (function (name, rules) { return function (e) { that.onElementChange(e.currentTarget, name, rules); }; })(name, rules[name])); } } return this; }, /** * @param {HTMLElement|jQuery} target * @param {String} name * @param {Array} rules */ onElementChange: function (target, name, rules) { target = (target instanceof jQuery) ? target : $(target); var value = target.val(), rule, match; for (var i = 0, length = rules.length; i < length; ++i) { rule = rules[i]; match = (rule._value === value); if (match || rule.toggle) { this.proceedRules(rule, match); } } }, /** * @param {Object} rule * @param {Boolean} match */ proceedRules: function (rule, match) { var method, target; for (method in rule) { if (!rule.hasOwnProperty(method) || '_' === method.substring(0, 1)) { continue; } target = $(rule[method]); switch (method) { case 'toggle': target[match ? 'show' : 'hide'](); break; case 'show': target.show(); break; case 'hide': target.hide(); break; default: alert('Unknown rule method "' + method + '" passed to SideBlockAdmin.proceedRules()'); } } return this; } }; return SideBlockAdmin; })();