diff --git a/dist/observe.js b/dist/observe.js index 5d6f961..51dcb91 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -179,18 +179,23 @@ } function cancelWatchObject(storageId) { if (_watches.hasOwnProperty(storageId)) { - fireCustomTrigger(_watches[storageId].options.onCancel, storageId); - clearTimeout(_watches[storageId].timer); - delete _watches[storageId]; + var watchOptions = _watches[storageId].options; + if (watchOptions.allowCanceling || _watches_Cancel) { + fireCustomTrigger(watchOptions.onCancel, storageId); + clearTimeout(_watches[storageId].timer); + delete _watches[storageId]; + } } } function pauseWatchObject(storageId, milliseconds) { var result = false; if (_watches.hasOwnProperty(storageId)) { var watchOptions = _watches[storageId].options; - watchOptions.starts = new Date(); - watchOptions.starts.setMilliseconds(watchOptions.starts.getMilliseconds() + milliseconds); - result = true; + if (watchOptions.allowPausing) { + watchOptions.starts = new Date(); + watchOptions.starts.setMilliseconds(watchOptions.starts.getMilliseconds() + milliseconds); + result = true; + } } return result; } @@ -204,6 +209,8 @@ options.maximumChangesBeforeCanceling = getDefaultNumber(options.maximumChangesBeforeCanceling, 0); options.pauseTimeoutOnChange = getDefaultNumber(options.pauseTimeoutOnChange, 0); options.propertyNames = getDefaultArray(options.propertyNames, null); + options.allowCanceling = getDefaultBoolean(options.allowCanceling, true); + options.allowPausing = getDefaultBoolean(options.allowPausing, null); options = getWatchOptionsCustomTriggers(options); return options; } @@ -317,6 +324,7 @@ var _parameter_Window = null; var _string = {empty:""}; var _watches = {}; + var _watches_Cancel = false; var _configuration = {}; var _attribute_Name_Watch_Options = "data-observe-watch-options"; this.watch = function(object, options) { @@ -431,6 +439,7 @@ collectDOMObjects(); }); _parameter_Window.addEventListener("unload", function() { + _watches_Cancel = true; cancelWatchesForObjects(); }); if (!isDefined(_parameter_Window.$observe)) { diff --git a/dist/observe.min.js b/dist/observe.min.js index d29555f..6d24f6c 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,13 +1,13 @@ /*! Observe.js v0.6.0 | (c) Bunoon 2024 | MIT License */ -(function(){function I(){for(var a=q.domElementTypes,b=a.length,c=0;c=k.starts){if(d.hasOwnProperty(h)){var e=d[h],g=p(e.domElementId), -m=null;g&&(m=y.getElementById(e.domElementId),r(m)?e.originalObject=m.outerHTML:(e.originalObject=B.empty,u(e.options.onRemove,e.domElementId)));var v=e.cachedObject,n=e.originalObject;n=g?n:JSON.stringify(n);if(v!==n){e.options.reset?g?m.outerHTML=e.cachedObject:e.originalObject=z(v).result:e.cachedObject=n;if(g)u(e.options.onChange,v,n);else if(g=z(v).result,m=z(n).result,w(g)||w(m))u(e.options.onChange,g,m);else{if(w(e.options.propertyNames))for(v=e.options.propertyNames.length,n=0;n=e.options.maximumChangesBeforeCanceling&&x(h)}}A(k.expires)&&l>=k.expires&&x(h)}},k.timeout);d[f]=b}return f}function N(a,b,c){for(var f in a)if(a.hasOwnProperty(f)){var k=a[f], -h=null;b.hasOwnProperty(f)&&(h=b[f]);t(k)&&t(h)?N(k,h,c.options):(!w(c.options.propertyNames)||-1b;b++){8!==b&&12!==b&&16!==b&&20!==b||a.push("-");var c=Math.floor(16*Math.random()).toString(16);a.push(c)}return a.join(B.empty)}function r(a){return null!==a&&void 0!==a&&a!==B.empty}function t(a){return r(a)&&"object"===typeof a}function p(a){return r(a)&&"string"===typeof a}function C(a){return r(a)&&"function"===typeof a}function w(a){return t(a)&&a instanceof Array}function A(a){return t(a)&& -a instanceof Date}function H(a,b){return r(a)&&"boolean"===typeof a?a:b}function E(a,b){return C(a)?a:b}function G(a,b){return r(a)&&"number"===typeof a?a:b}function z(a){var b=!0,c=null;try{p(a)&&(c=JSON.parse(a))}catch(f){try{c=eval("("+a+")"),C(c)&&(c=c())}catch(k){b=Q("Errors in object: "+f.message+", "+k.message),c=null}}return{parsed:b,result:c}}function Q(a){var b=!0;q.safeMode||(console.error(a),b=!1);return b}function P(){q.safeMode=H(q.safeMode,!0);var a=q,b=q.domElementTypes,c=["*"];p(b)? -(b=b.split(B.space),0===b.length&&(b=c)):b=w(b)?b:c;a.domElementTypes=b}var y=null,F=null,B={empty:""},d={},q={};this.watch=function(a,b){return L(a,b)};this.cancelWatch=function(a){var b=!1;if(d.hasOwnProperty(a))x(a),b=!0;else for(var c in d)if(d.hasOwnProperty(c)&&p(d[c].domElementId)&&d[c].domElementId===a){x(c);b=!0;break}return b};this.cancelWatches=function(){O();return this};this.getWatch=function(a){var b=null;if(d.hasOwnProperty(a))b=d[a];else for(var c in d)if(d.hasOwnProperty(c)&&p(d[c].domElementId)&& -d[c].domElementId===a){b=d[c];break}return b};this.getWatches=function(){return d};this.pauseWatch=function(a,b){var c=!1;if(d.hasOwnProperty(a))c=D(a,b);else for(var f in d)if(d.hasOwnProperty(f)&&p(d[f].domElementId)&&d[f].domElementId===a){c=D(f,b);break}return c};this.pauseWatches=function(a){for(var b in d)d.hasOwnProperty(b)&&D(b,a);return this};this.resumeWatch=function(a){var b=!1;if(d.hasOwnProperty(a))d[a].options.starts=null,b=!0;else for(var c in d)if(d.hasOwnProperty(c)&&p(d[c].domElementId)&& -d[c].domElementId===a){d[c].options.starts=null;b=!0;break}return b};this.resumeWatches=function(){for(var a in d)d.hasOwnProperty(a)&&(d[a].options.starts=null);return this};this.searchDomForNewWatches=function(){I();return this};this.setConfiguration=function(a){q=t(a)?a:{};P();return this};this.getVersion=function(){return"0.6.0"};(function(a,b){y=a;F=b;P();y.addEventListener("DOMContentLoaded",function(){I()});F.addEventListener("unload",function(){O()});r(F.$observe)||(F.$observe=this)})(document, -window)})(); \ No newline at end of file +(function(){function I(){for(var a=q.domElementTypes,b=a.length,c=0;c=k.starts){if(d.hasOwnProperty(h)){var e=d[h],g=p(e.domElementId), +m=null;g&&(m=y.getElementById(e.domElementId),r(m)?e.originalObject=m.outerHTML:(e.originalObject=C.empty,u(e.options.onRemove,e.domElementId)));var v=e.cachedObject,n=e.originalObject;n=g?n:JSON.stringify(n);if(v!==n){e.options.reset?g?m.outerHTML=e.cachedObject:e.originalObject=A(v).result:e.cachedObject=n;if(g)u(e.options.onChange,v,n);else if(g=A(v).result,m=A(n).result,w(g)||w(m))u(e.options.onChange,g,m);else{if(w(e.options.propertyNames))for(v=e.options.propertyNames.length,n=0;n=e.options.maximumChangesBeforeCanceling&&x(h)}}B(k.expires)&&l>=k.expires&&x(h)}},k.timeout);d[f]=b}return f}function N(a,b,c){for(var f in a)if(a.hasOwnProperty(f)){var k=a[f], +h=null;b.hasOwnProperty(f)&&(h=b[f]);t(k)&&t(h)?N(k,h,c.options):(!w(c.options.propertyNames)||-1b;b++){8!==b&&12!==b&&16!==b&&20!==b||a.push("-");var c=Math.floor(16*Math.random()).toString(16);a.push(c)}return a.join(C.empty)}function r(a){return null!==a&&void 0!==a&&a!==C.empty}function t(a){return r(a)&&"object"===typeof a}function p(a){return r(a)&& +"string"===typeof a}function D(a){return r(a)&&"function"===typeof a}function w(a){return t(a)&&a instanceof Array}function B(a){return t(a)&&a instanceof Date}function z(a,b){return r(a)&&"boolean"===typeof a?a:b}function F(a,b){return D(a)?a:b}function H(a,b){return r(a)&&"number"===typeof a?a:b}function A(a){var b=!0,c=null;try{p(a)&&(c=JSON.parse(a))}catch(f){try{c=eval("("+a+")"),D(c)&&(c=c())}catch(k){b=R("Errors in object: "+f.message+", "+k.message),c=null}}return{parsed:b,result:c}}function R(a){var b= +!0;q.safeMode||(console.error(a),b=!1);return b}function Q(){q.safeMode=z(q.safeMode,!0);var a=q,b=q.domElementTypes,c=["*"];p(b)?(b=b.split(C.space),0===b.length&&(b=c)):b=w(b)?b:c;a.domElementTypes=b}var y=null,G=null,C={empty:""},d={},P=!1,q={};this.watch=function(a,b){return L(a,b)};this.cancelWatch=function(a){var b=!1;if(d.hasOwnProperty(a))x(a),b=!0;else for(var c in d)if(d.hasOwnProperty(c)&&p(d[c].domElementId)&&d[c].domElementId===a){x(c);b=!0;break}return b};this.cancelWatches=function(){O(); +return this};this.getWatch=function(a){var b=null;if(d.hasOwnProperty(a))b=d[a];else for(var c in d)if(d.hasOwnProperty(c)&&p(d[c].domElementId)&&d[c].domElementId===a){b=d[c];break}return b};this.getWatches=function(){return d};this.pauseWatch=function(a,b){var c=!1;if(d.hasOwnProperty(a))c=E(a,b);else for(var f in d)if(d.hasOwnProperty(f)&&p(d[f].domElementId)&&d[f].domElementId===a){c=E(f,b);break}return c};this.pauseWatches=function(a){for(var b in d)d.hasOwnProperty(b)&&E(b,a);return this};this.resumeWatch= +function(a){var b=!1;if(d.hasOwnProperty(a))d[a].options.starts=null,b=!0;else for(var c in d)if(d.hasOwnProperty(c)&&p(d[c].domElementId)&&d[c].domElementId===a){d[c].options.starts=null;b=!0;break}return b};this.resumeWatches=function(){for(var a in d)d.hasOwnProperty(a)&&(d[a].options.starts=null);return this};this.searchDomForNewWatches=function(){I();return this};this.setConfiguration=function(a){q=t(a)?a:{};Q();return this};this.getVersion=function(){return"0.6.0"};(function(a,b){y=a;G=b;Q(); +y.addEventListener("DOMContentLoaded",function(){I()});G.addEventListener("unload",function(){P=!0;O()});r(G.$observe)||(G.$observe=this)})(document,window)})(); \ No newline at end of file diff --git a/docs/CHANGE_LOG.md b/docs/CHANGE_LOG.md index 45ec6fd..8cfde0d 100644 --- a/docs/CHANGE_LOG.md +++ b/docs/CHANGE_LOG.md @@ -2,6 +2,10 @@ ## Version 0.6.0: +#### **Binding Options / Function Options:** +- Added a new binding/option called "allowCanceling", which states the watch can be canceled (defaults to true). +- Added a new binding/option called "allowPausing", which states the watch can be paused (defaults to true). + #### **Public Functions:** - Added new public function "pauseWatches()", which is used to pause all the watches for a specific number of milliseconds. - Added new public function "resumeWatches()", which is used to resume all the watches currently paused. diff --git a/docs/binding/options/OPTIONS.md b/docs/binding/options/OPTIONS.md index a5daa73..df7fe7e 100644 --- a/docs/binding/options/OPTIONS.md +++ b/docs/binding/options/OPTIONS.md @@ -17,6 +17,8 @@ Below is a list of all the options supported in the "data-observe-watch-options" | *number* | maximumChangesBeforeCanceling | States the total number of changes that are allowed before the watch is cancelled (defaults to 0, which is off) | | *number* | pauseTimeoutOnChange | States the delay (in milliseconds) that should be used before checking for changes again after a change is detected (defaults to 0, which is off) | | *string[]* | propertyNames | States the property names that should be watched for changes (defaults to all). | +| *boolean* | allowCanceling | States if the watch can be canceled (defaults to true). | +| *boolean* | allowPausing | States if the watch can be paused (defaults to true). |
diff --git a/src/observe.js b/src/observe.js index de03587..8a174b1 100644 --- a/src/observe.js +++ b/src/observe.js @@ -23,6 +23,7 @@ // Variables: Watches _watches = {}, + _watches_Cancel = false, // Variables: Configuration _configuration = {}, @@ -269,10 +270,14 @@ function cancelWatchObject( storageId ) { if ( _watches.hasOwnProperty( storageId ) ) { - fireCustomTrigger( _watches[ storageId ].options.onCancel, storageId ); - clearTimeout( _watches[ storageId ].timer ); - - delete _watches[ storageId ]; + var watchOptions = _watches[ storageId ].options; + + if ( watchOptions.allowCanceling || _watches_Cancel ) { + fireCustomTrigger( watchOptions.onCancel, storageId ); + clearTimeout( _watches[ storageId ].timer ); + + delete _watches[ storageId ]; + } } } @@ -282,10 +287,12 @@ if ( _watches.hasOwnProperty( storageId ) ) { var watchOptions = _watches[ storageId ].options; - watchOptions.starts = new Date(); - watchOptions.starts.setMilliseconds( watchOptions.starts.getMilliseconds() + milliseconds ); - - result = true; + if ( watchOptions.allowPausing ) { + watchOptions.starts = new Date(); + watchOptions.starts.setMilliseconds( watchOptions.starts.getMilliseconds() + milliseconds ); + + result = true; + } } return result; @@ -309,7 +316,9 @@ options.maximumChangesBeforeCanceling = getDefaultNumber( options.maximumChangesBeforeCanceling, 0 ); options.pauseTimeoutOnChange = getDefaultNumber( options.pauseTimeoutOnChange, 0 ); options.propertyNames = getDefaultArray( options.propertyNames, null ); - + options.allowCanceling = getDefaultBoolean( options.allowCanceling, true ); + options.allowPausing = getDefaultBoolean( options.allowPausing, null ); + options = getWatchOptionsCustomTriggers( options ); return options; @@ -779,6 +788,8 @@ } ); _parameter_Window.addEventListener( "unload", function() { + _watches_Cancel = true; + cancelWatchesForObjects(); } );