From 7e3cf98a7532cdcf249d1c8dcba0dcf2443e8c46 Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Sat, 7 Mar 2015 09:47:44 +1100 Subject: [PATCH] Adds data-trigger-closest-parent to Trigger API Test code:
--- modules/backend/assets/js/october-min.js | 13 ++++++------- .../backend/assets/js/october.inputpreset.js | 6 +++--- modules/backend/assets/js/october.triggerapi.js | 17 +++++++++++------ modules/backend/classes/FormField.php | 3 ++- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/modules/backend/assets/js/october-min.js b/modules/backend/assets/js/october-min.js index c96d93792..aca096f16 100644 --- a/modules/backend/assets/js/october-min.js +++ b/modules/backend/assets/js/october-min.js @@ -69,8 +69,7 @@ if($.oc===undefined) $.oc={} $.oc.escapeHtmlString=function(string){var htmlEscapes={'&':'&','<':'<','>':'>','"':'"',"'":''','/':'/'},htmlEscaper=/[&<>"'\/]/g return(''+string).replace(htmlEscaper,function(match){return htmlEscapes[match];})} -+function($){"use strict";var TriggerOn=function(element,options){var $el=this.$el=$(element);this.options=options||{};if(this.options.triggerType!==false&&this.options.triggerAction===false)this.options.triggerAction=this.options.triggerType -if(this.options.triggerCondition===false) ++function($){"use strict";var TriggerOn=function(element,options){var $el=this.$el=$(element);this.options=options||{};if(this.options.triggerCondition===false) throw new Error('Trigger condition is not specified.') if(this.options.trigger===false) throw new Error('Trigger selector is not specified.') @@ -80,14 +79,14 @@ this.triggerCondition=this.options.triggerCondition if(this.options.triggerCondition.indexOf('value')==0){var match=this.options.triggerCondition.match(/[^[\]]+(?=])/g) this.triggerCondition='value' this.triggerConditionValue=(match)?match:""} -if(this.triggerCondition=='checked'||this.triggerCondition=='value') -$(document).on('change',this.options.trigger,$.proxy(this.onConditionChanged,this)) +this.triggerParent=this.options.triggerClosestParent!==undefined?$el.closest(this.options.triggerClosestParent):undefined +if(this.triggerCondition=='checked'||this.triggerCondition=='value'){$(document).on('change',this.options.trigger,$.proxy(this.onConditionChanged,this))} var self=this $el.on('oc.triggerOn.update',function(e){e.stopPropagation() self.onConditionChanged()}) self.onConditionChanged()} -TriggerOn.prototype.onConditionChanged=function(){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.onConditionChanged=function(){if(this.triggerCondition=='checked'){this.updateTarget($(this.options.trigger+':checked',this.triggerParent).length>0)} +else if(this.triggerCondition=='value'){this.updateTarget($(this.options.trigger,this.triggerParent).val()==this.triggerConditionValue)}} TriggerOn.prototype.updateTarget=function(status){if(this.options.triggerAction=='show') this.$el.toggleClass('hide',!status).trigger('hide',[!status]) else if(this.options.triggerAction=='hide') @@ -104,7 +103,7 @@ $(window).trigger('resize')} TriggerOn.prototype.fixButtonClasses=function(){var group=this.$el.closest('.btn-group') if(group.length>0&&this.$el.is(':last-child')) this.$el.prev().toggleClass('last',this.$el.hasClass('hide'))} -TriggerOn.DEFAULTS={triggerAction:false,triggerCondition:false,trigger:false} +TriggerOn.DEFAULTS={triggerAction:false,triggerCondition:false,triggerClosestParent:undefined,trigger:false} var old=$.fn.triggerOn $.fn.triggerOn=function(option){return this.each(function(){var $this=$(this) var data=$this.data('oc.triggerOn') diff --git a/modules/backend/assets/js/october.inputpreset.js b/modules/backend/assets/js/october.inputpreset.js index 11ab61b83..33d4bdd1b 100644 --- a/modules/backend/assets/js/october.inputpreset.js +++ b/modules/backend/assets/js/october.inputpreset.js @@ -191,9 +191,9 @@ this.options = options || {} this.cancelled = false - var parent = options.inputPresetClosestParent !== undefined ? - $el.closest(options.inputPresetClosestParent) : - undefined, + var parent = options.inputPresetClosestParent !== undefined + ? $el.closest(options.inputPresetClosestParent) + : undefined, self = this, prefix = '' diff --git a/modules/backend/assets/js/october.triggerapi.js b/modules/backend/assets/js/october.triggerapi.js index 608d7f903..498d8fdab 100644 --- a/modules/backend/assets/js/october.triggerapi.js +++ b/modules/backend/assets/js/october.triggerapi.js @@ -13,6 +13,8 @@ * 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". + * - data-trigger-closest-parent: optional, specifies a CSS selector for a closest common parent + * for the source and destination input elements. * * Example: = 2016 - if (this.options.triggerType !== false && this.options.triggerAction === false) this.options.triggerAction = this.options.triggerType - if (this.options.triggerCondition === false) throw new Error('Trigger condition is not specified.') @@ -54,8 +53,13 @@ this.triggerConditionValue = (match) ? match : "" } - if (this.triggerCondition == 'checked' || this.triggerCondition == 'value') + this.triggerParent = this.options.triggerClosestParent !== undefined + ? $el.closest(this.options.triggerClosestParent) + : undefined + + if (this.triggerCondition == 'checked' || this.triggerCondition == 'value') { $(document).on('change', this.options.trigger, $.proxy(this.onConditionChanged, this)) + } var self = this $el.on('oc.triggerOn.update', function(e){ @@ -68,10 +72,10 @@ TriggerOn.prototype.onConditionChanged = function() { if (this.triggerCondition == 'checked') { - this.updateTarget($(this.options.trigger + ':checked').length > 0) + this.updateTarget($(this.options.trigger + ':checked', this.triggerParent).length > 0) } else if (this.triggerCondition == 'value') { - this.updateTarget($(this.options.trigger).val() == this.triggerConditionValue) + this.updateTarget($(this.options.trigger, this.triggerParent).val() == this.triggerConditionValue) } } @@ -103,6 +107,7 @@ TriggerOn.DEFAULTS = { triggerAction: false, triggerCondition: false, + triggerClosestParent: undefined, trigger: false } diff --git a/modules/backend/classes/FormField.php b/modules/backend/classes/FormField.php index daf6bc03c..2d8cab757 100644 --- a/modules/backend/classes/FormField.php +++ b/modules/backend/classes/FormField.php @@ -417,7 +417,8 @@ class FormField $newAttributes = [ 'data-trigger' => '[name="'.$fullTriggerField.'"]', 'data-trigger-action' => $triggerAction, - 'data-trigger-condition' => $triggerCondition + 'data-trigger-condition' => $triggerCondition, + 'data-trigger-closest-parent' => 'form' ]; $attributes = $attributes + $newAttributes;