From 1d90ae25f79dddb028afd7d7cb0729fc187564a6 Mon Sep 17 00:00:00 2001 From: Sam Georges Date: Thu, 24 Jul 2014 19:55:27 +1000 Subject: [PATCH] Add support for values to trigger API --- .../backend/assets/js/october.triggerapi.js | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/modules/backend/assets/js/october.triggerapi.js b/modules/backend/assets/js/october.triggerapi.js index 629de2372..aa5b811da 100644 --- a/modules/backend/assets/js/october.triggerapi.js +++ b/modules/backend/assets/js/october.triggerapi.js @@ -8,11 +8,14 @@ * Supported data attributes: * - data-trigger-type, values: display, hide, enable, disable * - data-trigger: a CSS selector for elements that trigger the action (checkboxes) - * - data-trigger-condition, values: checked (more conditions to add later) - determines the condition the elements - * specified in the data-trigger should satisfy in order the condition to be considered as "true". + * - data-trigger-condition, values: + * - checked: determines the condition the elements specified in the data-trigger + * should satisfy in order the condition to be considered as "true". + * - value[somevalue]: determines if the value of data-trigger equals the specified value (somevalue) + * the condition is considered "true". * * Example: * @@ -28,7 +31,7 @@ var TriggerOn = function (element, options) { var $el = this.$el = $(element); - + this.options = options || {}; if (this.options.triggerCondition === false) @@ -40,10 +43,20 @@ if (this.options.triggerType === false) throw new Error('Trigger type is not specified.') - if (this.options.triggerCondition == 'checked') + this.triggerCondition = this.options.triggerCondition + + if (this.options.triggerCondition.indexOf('value') == 0) { + var match = this.options.triggerCondition.match(/[^[\]]+(?=])/g) + if (match) { + this.triggerConditionValue = match + this.triggerCondition = 'value' + } + } + + if (this.triggerCondition == 'checked' || this.triggerCondition == 'value') $(document).on('change', this.options.trigger, $.proxy(this.onConditionChanged, this)) - var self = this; + var self = this $el.on('oc.triggerOn.update', function(e){ e.stopPropagation() self.onConditionChanged() @@ -53,8 +66,12 @@ } TriggerOn.prototype.onConditionChanged = function() { - if (this.options.triggerCondition == 'checked') - this.updateTarget($(this.options.trigger + ':checked').length > 0); + if (this.triggerCondition == 'checked') { + this.updateTarget($(this.options.trigger + ':checked').length > 0) + } + else if (this.triggerCondition == 'value') { + this.updateTarget($(this.options.trigger).val() == this.triggerConditionValue) + } } TriggerOn.prototype.updateTarget = function(status) {