123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548 |
- (function ($) {
- var states = Drupal.states = {
-
- postponed: []
- };
- Drupal.behaviors.states = {
- attach: function (context, settings) {
- var $context = $(context);
- for (var selector in settings.states) {
- for (var state in settings.states[selector]) {
- new states.Dependent({
- element: $context.find(selector),
- state: states.State.sanitize(state),
- constraints: settings.states[selector][state]
- });
- }
- }
-
- while (states.postponed.length) {
- (states.postponed.shift())();
- }
- }
- };
- states.Dependent = function (args) {
- $.extend(this, { values: {}, oldValue: null }, args);
- this.dependees = this.getDependees();
- for (var selector in this.dependees) {
- this.initializeDependee(selector, this.dependees[selector]);
- }
- };
- states.Dependent.comparisons = {
- 'RegExp': function (reference, value) {
- return reference.test(value);
- },
- 'Function': function (reference, value) {
-
- return reference(value);
- },
- 'Number': function (reference, value) {
-
-
-
-
- return (typeof value === 'string') ? compare(reference.toString(), value) : compare(reference, value);
- }
- };
- states.Dependent.prototype = {
-
- initializeDependee: function (selector, dependeeStates) {
- var state;
-
- this.values[selector] = {};
- for (var i in dependeeStates) {
- if (dependeeStates.hasOwnProperty(i)) {
- state = dependeeStates[i];
-
- if ($.inArray(state, dependeeStates) === -1) {
- continue;
- }
- state = states.State.sanitize(state);
-
- this.values[selector][state.name] = null;
-
- $(selector).bind('state:' + state, $.proxy(function (e) {
- this.update(selector, state, e.value);
- }, this));
-
- new states.Trigger({ selector: selector, state: state });
- }
- }
- },
-
- compare: function (reference, selector, state) {
- var value = this.values[selector][state.name];
- if (reference.constructor.name in states.Dependent.comparisons) {
-
- return states.Dependent.comparisons[reference.constructor.name](reference, value);
- }
- else {
-
- return compare(reference, value);
- }
- },
-
- update: function (selector, state, value) {
-
- if (value !== this.values[selector][state.name]) {
- this.values[selector][state.name] = value;
- this.reevaluate();
- }
- },
-
- reevaluate: function () {
-
- var value = this.verifyConstraints(this.constraints);
-
- if (value !== this.oldValue) {
-
-
- this.oldValue = value;
-
- value = invert(value, this.state.invert);
-
-
- this.element.trigger({ type: 'state:' + this.state, value: value, trigger: true });
- }
- },
-
- verifyConstraints: function(constraints, selector) {
- var result;
- if ($.isArray(constraints)) {
-
- var hasXor = $.inArray('xor', constraints) === -1;
- for (var i = 0, len = constraints.length; i < len; i++) {
- if (constraints[i] != 'xor') {
- var constraint = this.checkConstraints(constraints[i], selector, i);
-
-
- if (constraint && (hasXor || result)) {
- return hasXor;
- }
- result = result || constraint;
- }
- }
- }
-
-
-
- else if ($.isPlainObject(constraints)) {
-
- for (var n in constraints) {
- if (constraints.hasOwnProperty(n)) {
- result = ternary(result, this.checkConstraints(constraints[n], selector, n));
-
-
- if (result === false) { return false; }
- }
- }
- }
- return result;
- },
-
- checkConstraints: function(value, selector, state) {
-
-
- if (typeof state !== 'string' || (/[0-9]/).test(state[0])) {
- state = null;
- }
- else if (typeof selector === 'undefined') {
-
- selector = state;
- state = null;
- }
- if (state !== null) {
-
- state = states.State.sanitize(state);
- return invert(this.compare(value, selector, state), state.invert);
- }
- else {
-
- return this.verifyConstraints(value, selector);
- }
- },
-
- getDependees: function() {
- var cache = {};
-
-
- var _compare = this.compare;
- this.compare = function(reference, selector, state) {
- (cache[selector] || (cache[selector] = [])).push(state.name);
-
-
- };
-
-
-
-
-
-
- this.verifyConstraints(this.constraints);
-
- this.compare = _compare;
- return cache;
- }
- };
- states.Trigger = function (args) {
- $.extend(this, args);
- if (this.state in states.Trigger.states) {
- this.element = $(this.selector);
-
-
- if (!this.element.data('trigger:' + this.state)) {
- this.initialize();
- }
- }
- };
- states.Trigger.prototype = {
- initialize: function () {
- var trigger = states.Trigger.states[this.state];
- if (typeof trigger == 'function') {
-
- trigger.call(window, this.element);
- }
- else {
- for (var event in trigger) {
- if (trigger.hasOwnProperty(event)) {
- this.defaultTrigger(event, trigger[event]);
- }
- }
- }
-
- this.element.data('trigger:' + this.state, true);
- },
- defaultTrigger: function (event, valueFn) {
- var oldValue = valueFn.call(this.element);
-
- this.element.bind(event, $.proxy(function (e) {
- var value = valueFn.call(this.element, e);
-
- if (oldValue !== value) {
- this.element.trigger({ type: 'state:' + this.state, value: value, oldValue: oldValue });
- oldValue = value;
- }
- }, this));
- states.postponed.push($.proxy(function () {
-
- this.element.trigger({ type: 'state:' + this.state, value: oldValue, oldValue: null });
- }, this));
- }
- };
- states.Trigger.states = {
-
- empty: {
-
- 'keyup': function () {
-
-
- return this.val() == '';
- }
- },
- checked: {
- 'change': function () {
- return this.is(':checked');
- }
- },
-
- value: {
- 'keyup': function () {
-
- if (this.length > 1) {
-
- return this.filter(':checked').val() || false;
- }
- return this.val();
- },
- 'change': function () {
-
- if (this.length > 1) {
-
- return this.filter(':checked').val() || false;
- }
- return this.val();
- }
- },
- collapsed: {
- 'collapsed': function(e) {
- return (typeof e !== 'undefined' && 'value' in e) ? e.value : this.is('.collapsed');
- }
- }
- };
- states.State = function(state) {
-
- this.pristine = this.name = state;
-
- while (true) {
-
- while (this.name.charAt(0) == '!') {
- this.name = this.name.substring(1);
- this.invert = !this.invert;
- }
-
- if (this.name in states.State.aliases) {
- this.name = states.State.aliases[this.name];
- }
- else {
- break;
- }
- }
- };
- states.State.sanitize = function (state) {
- if (state instanceof states.State) {
- return state;
- }
- else {
- return new states.State(state);
- }
- };
- states.State.aliases = {
- 'enabled': '!disabled',
- 'invisible': '!visible',
- 'invalid': '!valid',
- 'untouched': '!touched',
- 'optional': '!required',
- 'filled': '!empty',
- 'unchecked': '!checked',
- 'irrelevant': '!relevant',
- 'expanded': '!collapsed',
- 'readwrite': '!readonly'
- };
- states.State.prototype = {
- invert: false,
-
- toString: function() {
- return this.name;
- }
- };
- $(document).bind('state:disabled', function(e) {
-
-
- if (e.trigger) {
- $(e.target)
- .attr('disabled', e.value)
- .closest('.form-item, .form-submit, .form-wrapper').toggleClass('form-disabled', e.value)
- .find('select, input, textarea').attr('disabled', e.value);
-
-
- }
- });
- $(document).bind('state:required', function(e) {
- if (e.trigger) {
- if (e.value) {
- var $label = $(e.target).closest('.form-item, .form-wrapper').find('label');
-
- if (!$label.find('.form-required').length) {
- $label.append('<span class="form-required">*</span>');
- }
- }
- else {
- $(e.target).closest('.form-item, .form-wrapper').find('label .form-required').remove();
- }
- }
- });
- $(document).bind('state:visible', function(e) {
- if (e.trigger) {
- $(e.target).closest('.form-item, .form-submit, .form-wrapper').toggle(e.value);
- }
- });
- $(document).bind('state:checked', function(e) {
- if (e.trigger) {
- $(e.target).attr('checked', e.value);
- }
- });
- $(document).bind('state:collapsed', function(e) {
- if (e.trigger) {
- if ($(e.target).is('.collapsed') !== e.value) {
- $('> legend a', e.target).click();
- }
- }
- });
- function ternary (a, b) {
- return typeof a === 'undefined' ? b : (typeof b === 'undefined' ? a : a && b);
- }
- function invert (a, invert) {
- return (invert && typeof a !== 'undefined') ? !a : a;
- }
- function compare (a, b) {
- return (a === b) ? (typeof a === 'undefined' ? a : true) : (typeof a === 'undefined' || typeof b === 'undefined');
- }
- })(jQuery);
|