From 27b94a5c4fd73841c3bf3be1f99dcf1239ac9fe3 Mon Sep 17 00:00:00 2001 From: William Troup Date: Mon, 1 Jan 2024 22:23:01 +0000 Subject: [PATCH 01/20] Updated to v0.2.0. --- README.md | 6 +++--- README_NUGET.md | 6 +++--- dist/observe.js | 4 ++-- dist/observe.min.js | 4 ++-- docs/CHANGE_LOG.md | 5 +++++ observe.js.nuspec | 2 +- package.json | 2 +- src/observe.js | 4 ++-- 8 files changed, 19 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 40266d9..4df83b2 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,15 @@ Observe.js [![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Observe.js%2C%20a%20free%20JavaScript%observe%builder&url=https://github.com/williamtroup/Observe.js&hashtags=javascript,html,observe) -[![npm](https://img.shields.io/badge/npmjs-v0.1.0-blue)](https://www.npmjs.com/package/jobserve.js) -[![nuget](https://img.shields.io/badge/nuget-v0.1.0-purple)](https://www.nuget.org/packages/jObserve.js/) +[![npm](https://img.shields.io/badge/npmjs-v0.2.0-blue)](https://www.npmjs.com/package/jobserve.js) +[![nuget](https://img.shields.io/badge/nuget-v0.2.0-purple)](https://www.nuget.org/packages/jObserve.js/) [![license](https://img.shields.io/badge/license-MIT-green)](https://github.com/williamtroup/Observe.js/blob/main/LICENSE.txt) [![discussions Welcome](https://img.shields.io/badge/discussions-Welcome-red)](https://github.com/williamtroup/Observe.js/discussions) [![coded by William Troup](https://img.shields.io/badge/coded_by-William_Troup-yellow)](https://github.com/williamtroup) >

A lightweight, and easy-to-use, JavaScript library for observing any kind of JS object, or HTML DOM element, to detect changes!

->

v0.1.0

+>

v0.2.0



diff --git a/README_NUGET.md b/README_NUGET.md index d5642fe..139a519 100644 --- a/README_NUGET.md +++ b/README_NUGET.md @@ -1,8 +1,8 @@ -# Observe.js v0.1.0 +# Observe.js v0.2.0 [![Tweet](https://img.shields.io/twitter/url/http/shields.io.svg?style=social)](https://twitter.com/intent/tweet?text=Observe.js%2C%20a%20free%20JavaScript%observe%builder&url=https://github.com/williamtroup/Observe.js&hashtags=javascript,html,observe) -[![npm](https://img.shields.io/badge/npmjs-v0.1.0-blue)](https://www.npmjs.com/package/jobserve.js) -[![nuget](https://img.shields.io/badge/nuget-v0.1.0-purple)](https://www.nuget.org/packages/jObserve.js/) +[![npm](https://img.shields.io/badge/npmjs-v0.2.0-blue)](https://www.npmjs.com/package/jobserve.js) +[![nuget](https://img.shields.io/badge/nuget-v0.2.0-purple)](https://www.nuget.org/packages/jObserve.js/) [![license](https://img.shields.io/badge/license-MIT-green)](https://github.com/williamtroup/Observe.js/blob/main/LICENSE.txt) [![discussions Welcome](https://img.shields.io/badge/discussions-Welcome-red)](https://github.com/williamtroup/Observe.js/discussions) [![coded by William Troup](https://img.shields.io/badge/coded_by-William_Troup-yellow)](https://github.com/williamtroup) diff --git a/dist/observe.js b/dist/observe.js index c593292..c2dce5b 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -1,4 +1,4 @@ -/*! Observe.js v0.1.0 | (c) Bunoon | MIT License */ +/*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ (function() { function collectDOMObjects() { var tagTypes = _configuration.domElementTypes; @@ -261,7 +261,7 @@ return this; }; this.getVersion = function() { - return "0.1.0"; + return "0.2.0"; }; (function(documentObject, windowObject) { _parameter_Document = documentObject; diff --git a/dist/observe.min.js b/dist/observe.min.js index 542abe1..9a00e10 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,8 +1,8 @@ -/*! Observe.js v0.1.0 | (c) Bunoon | MIT License */ +/*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ (function(){function A(a,b,c){var e=null;if(q(a)){e=B();var g=C(b);d[e]={};d[e].options=g;d[e].domElementId=c;t(c)?(a=u.getElementById(c),n(a)&&(d[e].cachedObject=a.innerHTML,d[e].originalObject=a.innerHTML)):(d[e].cachedObject=JSON.stringify(a),d[e].originalObject=a);d[e].timer=setInterval(function(){var k=new Date,h=e,l=t(d[h].domElementId);if(l){var f=u.getElementById(d[h].domElementId);n(f)&&(d[h].originalObject=f.innerHTML)}f=d[h].cachedObject;var m=d[h].originalObject;m=l?m:JSON.stringify(m); f!==m&&(d[h].cachedObject=m,h=d[h].options,l?w(h.onChange,f,m):(l=x(f).result,f=x(m).result,w(h.onChange,l,f),v(h.onPropertyChange)&&!D(l)&&E(l,f,h)));F(g.expires)&&k>g.expires&&(clearTimeout(d[e].timer),delete d[e])},g.observeTimeout)}return e}function E(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var g=a[e],k=null;b.hasOwnProperty(e)&&(k=b[e]);q(g)&&q(k)?E(g,k,c):JSON.stringify(g)!==JSON.stringify(k)&&w(c.onPropertyChange,e,g,k)}}function C(a){a=q(a)?a:{};var b=a.observeTimeout;var c=n(b)&&"number"=== typeof b;a.observeTimeout=c?b:250;b=a.expires;b=F(b)?b:null;a.expires=b;b=a.onChange;b=v(b)?b:null;a.onChange=b;b=a.onPropertyChange;b=v(b)?b:null;a.onPropertyChange=b;return a}function w(a){v(a)&&a.apply(null,[].slice.call(arguments,1))}function B(){for(var a=[],b=0;32>b;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(y.empty)}function n(a){return null!==a&&void 0!==a&&a!==y.empty}function q(a){return n(a)&&"object"===typeof a} function t(a){return n(a)&&"string"===typeof a}function v(a){return n(a)&&"function"===typeof a}function D(a){return q(a)&&a instanceof Array}function F(a){return q(a)&&a instanceof Date}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),v(c)&&(c=c())}catch(g){b=H("Errors in object: "+e.message+", "+g.message),c=null}}return{parsed:b,result:c}}function H(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function G(){var a=p,b=p.safeMode;var c=n(b)&&"boolean"=== typeof b;a.safeMode=c?b:!0;a=p;c=p.domElementTypes;b=["*"];t(c)?(c=c.split(y.space),0===c.length&&(c=b)):c=D(c)?c:b;a.domElementTypes=c}var u=null,z=null,y={empty:""},d={},p={};this.watchObject=function(a,b){return A(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(clearTimeout(d[a].timer),delete d[a],b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){clearTimeout(d[c].timer);delete d[c]; -b=!0;break}return b};this.setConfiguration=function(a){p=q(a)?a:{};G();return this};this.getVersion=function(){return"0.1.0"};(function(a,b){u=a;z=b;G();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,g=0;g + ## Version 0.1.0: - Everything :) \ No newline at end of file diff --git a/observe.js.nuspec b/observe.js.nuspec index 921bbca..108c71c 100644 --- a/observe.js.nuspec +++ b/observe.js.nuspec @@ -2,7 +2,7 @@ jObserve.js - 0.1.0 + 0.2.0 Observe.js A lightweight, and easy-to-use, JavaScript library for observing any kind of JS object, or HTML DOM element, to detect changes. William Troup diff --git a/package.json b/package.json index fb48759..a2b4c3c 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "jobserve.js", "title": "Observe.js", "description": "A lightweight, and easy-to-use, JavaScript library for observing any kind of JS object, or HTML DOM element, to detect changes.", - "version": "0.1.0", + "version": "0.2.0", "main": "dist/observe.js", "author": { "name": "Bunoon" diff --git a/src/observe.js b/src/observe.js index c948c7f..c002e33 100644 --- a/src/observe.js +++ b/src/observe.js @@ -4,7 +4,7 @@ * A lightweight, and easy-to-use, JavaScript library for observing any kind of JS object, or HTML DOM element, to detect changes. * * @file observe.js - * @version v0.1.0 + * @version v0.2.0 * @author Bunoon * @license MIT License * @copyright Bunoon 2023 @@ -493,7 +493,7 @@ * @returns {string} The version number. */ this.getVersion = function() { - return "0.1.0"; + return "0.2.0"; }; From 8928edd7cdb9f6b99c1927971698199c2734e8a0 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 09:54:13 +0000 Subject: [PATCH 02/20] Added "reset" support, which will force any object to be reset back to its original value when a change is detected. --- dist/observe.js | 18 ++++++++++++++++-- dist/observe.min.js | 14 +++++++------- docs/CHANGE_LOG.md | 7 ++++++- docs/binding/options/OPTIONS.md | 1 + src/observe.js | 26 ++++++++++++++++++++++---- test/dist/observe.js.basic.html | 14 ++++++++++---- test/dist/observe.js.min.html | 14 ++++++++++---- test/src/observe.js.basic.html | 14 ++++++++++---- 8 files changed, 82 insertions(+), 26 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index c2dce5b..293ab06 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -74,8 +74,9 @@ } function observeObject(storageId) { var isDomElement = isDefinedString(_observables[storageId].domElementId); + var domElement = null; if (isDomElement) { - var domElement = _parameter_Document.getElementById(_observables[storageId].domElementId); + domElement = _parameter_Document.getElementById(_observables[storageId].domElementId); if (isDefined(domElement)) { _observables[storageId].originalObject = domElement.innerHTML; } @@ -84,8 +85,16 @@ var originalObject = _observables[storageId].originalObject; var originalObjectJson = !isDomElement ? JSON.stringify(originalObject) : originalObject; if (cachedObject !== originalObjectJson) { - _observables[storageId].cachedObject = originalObjectJson; var options = _observables[storageId].options; + if (options.reset) { + if (isDomElement) { + domElement.innerHTML = _observables[storageId].cachedObject; + } else { + _observables[storageId].originalObject = getObjectFromString(cachedObject).result; + } + } else { + _observables[storageId].cachedObject = originalObjectJson; + } if (isDomElement) { fireCustomTrigger(options.onChange, cachedObject, originalObjectJson); } else { @@ -121,6 +130,11 @@ var options = !isDefinedObject(newOptions) ? {} : newOptions; options.observeTimeout = getDefaultNumber(options.observeTimeout, 250); options.expires = getDefaultDate(options.expires, null); + options.reset = getDefaultBoolean(options.reset, false); + options = getObserveOptionsCustomTriggers(options); + return options; + } + function getObserveOptionsCustomTriggers(options) { options.onChange = getDefaultFunction(options.onChange, null); options.onPropertyChange = getDefaultFunction(options.onPropertyChange, null); return options; diff --git a/dist/observe.min.js b/dist/observe.min.js index 9a00e10..3763c21 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,8 +1,8 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ -(function(){function A(a,b,c){var e=null;if(q(a)){e=B();var g=C(b);d[e]={};d[e].options=g;d[e].domElementId=c;t(c)?(a=u.getElementById(c),n(a)&&(d[e].cachedObject=a.innerHTML,d[e].originalObject=a.innerHTML)):(d[e].cachedObject=JSON.stringify(a),d[e].originalObject=a);d[e].timer=setInterval(function(){var k=new Date,h=e,l=t(d[h].domElementId);if(l){var f=u.getElementById(d[h].domElementId);n(f)&&(d[h].originalObject=f.innerHTML)}f=d[h].cachedObject;var m=d[h].originalObject;m=l?m:JSON.stringify(m); -f!==m&&(d[h].cachedObject=m,h=d[h].options,l?w(h.onChange,f,m):(l=x(f).result,f=x(m).result,w(h.onChange,l,f),v(h.onPropertyChange)&&!D(l)&&E(l,f,h)));F(g.expires)&&k>g.expires&&(clearTimeout(d[e].timer),delete d[e])},g.observeTimeout)}return e}function E(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var g=a[e],k=null;b.hasOwnProperty(e)&&(k=b[e]);q(g)&&q(k)?E(g,k,c):JSON.stringify(g)!==JSON.stringify(k)&&w(c.onPropertyChange,e,g,k)}}function C(a){a=q(a)?a:{};var b=a.observeTimeout;var c=n(b)&&"number"=== -typeof b;a.observeTimeout=c?b:250;b=a.expires;b=F(b)?b:null;a.expires=b;b=a.onChange;b=v(b)?b:null;a.onChange=b;b=a.onPropertyChange;b=v(b)?b:null;a.onPropertyChange=b;return a}function w(a){v(a)&&a.apply(null,[].slice.call(arguments,1))}function B(){for(var a=[],b=0;32>b;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(y.empty)}function n(a){return null!==a&&void 0!==a&&a!==y.empty}function q(a){return n(a)&&"object"===typeof a} -function t(a){return n(a)&&"string"===typeof a}function v(a){return n(a)&&"function"===typeof a}function D(a){return q(a)&&a instanceof Array}function F(a){return q(a)&&a instanceof Date}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),v(c)&&(c=c())}catch(g){b=H("Errors in object: "+e.message+", "+g.message),c=null}}return{parsed:b,result:c}}function H(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function G(){var a=p,b=p.safeMode;var c=n(b)&&"boolean"=== -typeof b;a.safeMode=c?b:!0;a=p;c=p.domElementTypes;b=["*"];t(c)?(c=c.split(y.space),0===c.length&&(c=b)):c=D(c)?c:b;a.domElementTypes=c}var u=null,z=null,y={empty:""},d={},p={};this.watchObject=function(a,b){return A(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(clearTimeout(d[a].timer),delete d[a],b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){clearTimeout(d[c].timer);delete d[c]; -b=!0;break}return b};this.setConfiguration=function(a){p=q(a)?a:{};G();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){u=a;z=b;G();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,g=0;gg.expires&&(clearTimeout(d[e].timer),delete d[e])},g.observeTimeout)}return e}function F(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var g=a[e],k=null;b.hasOwnProperty(e)&&(k=b[e]);q(g)&&q(k)?F(g,k,c):JSON.stringify(g)!==JSON.stringify(k)&&y(c.onPropertyChange, + e,g,k)}}function D(a){var b=a=q(a)?a:{},c=a.observeTimeout;var e=n(c)&&"number"===typeof c;b.observeTimeout=e?c:250;b=a;c=a.expires;c=G(c)?c:null;b.expires=c;b=a;c=a.reset;c=H(c)?c:!1;b.reset=c;b=a.onChange;b=v(b)?b:null;a.onChange=b;b=a.onPropertyChange;b=v(b)?b:null;a.onPropertyChange=b;return a}function y(a){v(a)&&a.apply(null,[].slice.call(arguments,1))}function C(){for(var a=[],b=0;32>b;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(z.empty)} + function n(a){return null!==a&&void 0!==a&&a!==z.empty}function q(a){return n(a)&&"object"===typeof a}function H(a){return n(a)&&"boolean"===typeof a}function t(a){return n(a)&&"string"===typeof a}function v(a){return n(a)&&"function"===typeof a}function E(a){return q(a)&&a instanceof Array}function G(a){return q(a)&&a instanceof Date}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),v(c)&&(c=c())}catch(g){b=J("Errors in object: "+e.message+", "+g.message),c= + null}}return{parsed:b,result:c}}function J(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function I(){var a=p;var b=p.safeMode;b=H(b)?b:!0;a.safeMode=b;a=p;var c=p.domElementTypes;b=["*"];t(c)?(c=c.split(z.space),0===c.length&&(c=b)):c=E(c)?c:b;a.domElementTypes=c}var u=null,A=null,z={empty:""},d={},p={};this.watchObject=function(a,b){return B(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(clearTimeout(d[a].timer),delete d[a],b=!0);return b};this.cancelDomElementWatch= + function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){clearTimeout(d[c].timer);delete d[c];b=!0;break}return b};this.setConfiguration=function(a){p=q(a)?a:{};I();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){u=a;A=b;I();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,g=0;g diff --git a/docs/binding/options/OPTIONS.md b/docs/binding/options/OPTIONS.md index d6e18d1..704f8c0 100644 --- a/docs/binding/options/OPTIONS.md +++ b/docs/binding/options/OPTIONS.md @@ -12,6 +12,7 @@ Below is a list of all the options supported in the "data-observe-options" bindi | --- | --- | --- | | *number* | observeTimeout | States the delay that should be waited before checking the object for changes (defaults to 250 milliseconds). | | *date* | expires | States a date/time when the watch should expire (defaults to null). | +| *boolean* | reset | States if the original value of the object should be reset back to its original value when a change is detected (defaults to false). |
diff --git a/src/observe.js b/src/observe.js index c002e33..8ea62f6 100644 --- a/src/observe.js +++ b/src/observe.js @@ -139,10 +139,11 @@ } function observeObject( storageId ) { - var isDomElement = isDefinedString( _observables[ storageId ].domElementId ); + var isDomElement = isDefinedString( _observables[ storageId ].domElementId ), + domElement = null; if ( isDomElement ) { - var domElement = _parameter_Document.getElementById( _observables[ storageId ].domElementId ); + domElement = _parameter_Document.getElementById( _observables[ storageId ].domElementId ); if ( isDefined( domElement ) ) { _observables[ storageId ].originalObject = domElement.innerHTML; @@ -154,10 +155,19 @@ originalObjectJson = !isDomElement ? JSON.stringify( originalObject ) : originalObject; if ( cachedObject !== originalObjectJson ) { - _observables[ storageId ].cachedObject = originalObjectJson; - var options = _observables[ storageId ].options; + if ( options.reset ) { + if ( isDomElement ) { + domElement.innerHTML = _observables[ storageId ].cachedObject; + } else { + _observables[ storageId ].originalObject = getObjectFromString( cachedObject ).result; + } + + } else { + _observables[ storageId ].cachedObject = originalObjectJson; + } + if ( isDomElement ) { fireCustomTrigger( options.onChange, cachedObject, originalObjectJson ); } else { @@ -208,6 +218,14 @@ options.observeTimeout = getDefaultNumber( options.observeTimeout, 250 ); options.expires = getDefaultDate( options.expires, null ); + options.reset = getDefaultBoolean( options.reset, false ); + + options = getObserveOptionsCustomTriggers( options ); + + return options; + } + + function getObserveOptionsCustomTriggers( options ) { options.onChange = getDefaultFunction( options.onChange, null ); options.onPropertyChange = getDefaultFunction( options.onPropertyChange, null ); diff --git a/test/dist/observe.js.basic.html b/test/dist/observe.js.basic.html index 7b2ebd3..bee67bf 100644 --- a/test/dist/observe.js.basic.html +++ b/test/dist/observe.js.basic.html @@ -14,15 +14,21 @@

Observe.js - Basic

This is a basic example of how to use Observe.js an HTML DOM element.

-
- When changed, this element should fire a custom trigger. +
+
+ When changed, this element should fire a custom trigger. +
+ +
+ When changed, this element should fire a custom trigger (and reset its value back to the original). +

Watch Object:

- +

Change Object Values:

@@ -30,7 +36,7 @@

Change Object Values:


- +

Configuration:

diff --git a/test/dist/observe.js.min.html b/test/dist/observe.js.min.html index 1bbb1c7..e657f6c 100644 --- a/test/dist/observe.js.min.html +++ b/test/dist/observe.js.min.html @@ -14,15 +14,21 @@

Observe.js - Basic

This is a basic example of how to use Observe.js an HTML DOM element.

-
- When changed, this element should fire a custom trigger. +
+
+ When changed, this element should fire a custom trigger. +
+ +
+ When changed, this element should fire a custom trigger (and reset its value back to the original). +

Watch Object:

- +

Change Object Values:

@@ -30,7 +36,7 @@

Change Object Values:


- +

Configuration:

diff --git a/test/src/observe.js.basic.html b/test/src/observe.js.basic.html index ba22d91..5da942e 100644 --- a/test/src/observe.js.basic.html +++ b/test/src/observe.js.basic.html @@ -14,15 +14,21 @@

Observe.js - Basic

This is a basic example of how to use Observe.js an HTML DOM element.

-
- When changed, this element should fire a custom trigger. +
+
+ When changed, this element should fire a custom trigger. +
+ +
+ When changed, this element should fire a custom trigger (and reset its value back to the original). +

Watch Object:

- +

Change Object Values:

@@ -30,7 +36,7 @@

Change Object Values:


- +

Configuration:

From 6957887294b8da7c05e11542e2f6852da955cb09 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 10:06:24 +0000 Subject: [PATCH 03/20] Added "cancelOnChange" support, which will force a watch to cancel when the first change is detected in an object. --- dist/observe.js | 17 +++++++++++------ dist/observe.min.js | 14 +++++++------- docs/CHANGE_LOG.md | 2 ++ docs/binding/options/OPTIONS.md | 1 + src/observe.js | 19 +++++++++++++------ 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 293ab06..16f0423 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -65,8 +65,7 @@ var currentDateTime = new Date(); observeObject(storageId); if (isDefinedDate(observeOptions.expires) && currentDateTime > observeOptions.expires) { - clearTimeout(_observables[storageId].timer); - delete _observables[storageId]; + cancelWatchObject(storageId); } }, observeOptions.observeTimeout); } @@ -105,6 +104,9 @@ compareObservableObjectProperties(oldValue, newValue, options); } } + if (options.cancelOnChange) { + cancelWatchObject(storageId); + } } } function compareObservableObjectProperties(oldObject, newObject, options) { @@ -126,11 +128,16 @@ } } } + function cancelWatchObject(storageId) { + clearTimeout(_observables[storageId].timer); + delete _observables[storageId]; + } function getObserveOptions(newOptions) { var options = !isDefinedObject(newOptions) ? {} : newOptions; options.observeTimeout = getDefaultNumber(options.observeTimeout, 250); options.expires = getDefaultDate(options.expires, null); options.reset = getDefaultBoolean(options.reset, false); + options.cancelOnChange = getDefaultBoolean(options.cancelOnChange, false); options = getObserveOptionsCustomTriggers(options); return options; } @@ -250,8 +257,7 @@ this.cancelWatch = function(id) { var result = false; if (_observables.hasOwnProperty(id)) { - clearTimeout(_observables[id].timer); - delete _observables[id]; + cancelWatchObject(id); result = true; } return result; @@ -261,8 +267,7 @@ var storageId; for (storageId in _observables) { if (_observables.hasOwnProperty(storageId) && isDefinedString(_observables[storageId].domElementId) && _observables[storageId].domElementId === elementId) { - clearTimeout(_observables[storageId].timer); - delete _observables[storageId]; + cancelWatchObject(storageId); result = true; break; } diff --git a/dist/observe.min.js b/dist/observe.min.js index 3763c21..5ff13ce 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,8 +1,8 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ -(function(){function B(a,b,c){var e=null;if(q(a)){e=C();var g=D(b);d[e]={};d[e].options=g;d[e].domElementId=c;t(c)?(a=u.getElementById(c),n(a)&&(d[e].cachedObject=a.innerHTML,d[e].originalObject=a.innerHTML)):(d[e].cachedObject=JSON.stringify(a),d[e].originalObject=a);d[e].timer=setInterval(function(){var k=new Date,h=e,m=t(d[h].domElementId),l=null;m&&(l=u.getElementById(d[h].domElementId),n(l)&&(d[h].originalObject=l.innerHTML));var r=d[h].cachedObject,f=d[h].originalObject,w=m?f:JSON.stringify(f); - r!==w&&(f=d[h].options,f.reset?m?l.innerHTML=d[h].cachedObject:d[h].originalObject=x(r).result:d[h].cachedObject=w,m?y(f.onChange,r,w):(h=x(r).result,m=x(w).result,y(f.onChange,h,m),v(f.onPropertyChange)&&!E(h)&&F(h,m,f)));G(g.expires)&&k>g.expires&&(clearTimeout(d[e].timer),delete d[e])},g.observeTimeout)}return e}function F(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var g=a[e],k=null;b.hasOwnProperty(e)&&(k=b[e]);q(g)&&q(k)?F(g,k,c):JSON.stringify(g)!==JSON.stringify(k)&&y(c.onPropertyChange, - e,g,k)}}function D(a){var b=a=q(a)?a:{},c=a.observeTimeout;var e=n(c)&&"number"===typeof c;b.observeTimeout=e?c:250;b=a;c=a.expires;c=G(c)?c:null;b.expires=c;b=a;c=a.reset;c=H(c)?c:!1;b.reset=c;b=a.onChange;b=v(b)?b:null;a.onChange=b;b=a.onPropertyChange;b=v(b)?b:null;a.onPropertyChange=b;return a}function y(a){v(a)&&a.apply(null,[].slice.call(arguments,1))}function C(){for(var a=[],b=0;32>b;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(z.empty)} - function n(a){return null!==a&&void 0!==a&&a!==z.empty}function q(a){return n(a)&&"object"===typeof a}function H(a){return n(a)&&"boolean"===typeof a}function t(a){return n(a)&&"string"===typeof a}function v(a){return n(a)&&"function"===typeof a}function E(a){return q(a)&&a instanceof Array}function G(a){return q(a)&&a instanceof Date}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),v(c)&&(c=c())}catch(g){b=J("Errors in object: "+e.message+", "+g.message),c= - null}}return{parsed:b,result:c}}function J(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function I(){var a=p;var b=p.safeMode;b=H(b)?b:!0;a.safeMode=b;a=p;var c=p.domElementTypes;b=["*"];t(c)?(c=c.split(z.space),0===c.length&&(c=b)):c=E(c)?c:b;a.domElementTypes=c}var u=null,A=null,z={empty:""},d={},p={};this.watchObject=function(a,b){return B(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(clearTimeout(d[a].timer),delete d[a],b=!0);return b};this.cancelDomElementWatch= - function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){clearTimeout(d[c].timer);delete d[c];b=!0;break}return b};this.setConfiguration=function(a){p=q(a)?a:{};I();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){u=a;A=b;I();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,g=0;gg.expires&&y(d)},g.observeTimeout)}return d}function H(a,b,c){for(var d in a)if(a.hasOwnProperty(d)){var g=a[d],k=null;b.hasOwnProperty(d)&&(k=b[d]);q(g)&&q(k)?H(g,k,c):JSON.stringify(g)!==JSON.stringify(k)&&z(c.onPropertyChange,d,g,k)}}function y(a){clearTimeout(e[a].timer); +delete e[a]}function F(a){var b=a=q(a)?a:{},c=a.observeTimeout;var d=n(c)&&"number"===typeof c;b.observeTimeout=d?c:250;b=a;c=a.expires;c=I(c)?c:null;b.expires=c;a.reset=A(a.reset,!1);a.cancelOnChange=A(a.cancelOnChange,!1);b=a.onChange;b=v(b)?b:null;a.onChange=b;b=a.onPropertyChange;b=v(b)?b:null;a.onPropertyChange=b;return a}function z(a){v(a)&&a.apply(null,[].slice.call(arguments,1))}function E(){for(var a=[],b=0;32>b;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 n(a){return null!==a&&void 0!==a&&a!==B.empty}function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function v(a){return n(a)&&"function"===typeof a}function G(a){return q(a)&&a instanceof Array}function I(a){return q(a)&&a instanceof Date}function A(a,b){return n(a)&&"boolean"===typeof a?a:b}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(d){try{c=eval("("+a+")"),v(c)&&(c=c())}catch(g){b=K("Errors in object: "+ +d.message+", "+g.message),c=null}}return{parsed:b,result:c}}function K(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function J(){p.safeMode=A(p.safeMode,!0);var a=p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(B.space),0===b.length&&(b=c)):b=G(b)?b:c;a.domElementTypes=b}var u=null,C=null,B={empty:""},e={},p={};this.watchObject=function(a,b){return D(a,b)};this.cancelWatch=function(a){var b=!1;e.hasOwnProperty(a)&&(y(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in e)if(e.hasOwnProperty(c)&& +t(e[c].domElementId)&&e[c].domElementId===a){y(c);b=!0;break}return b};this.setConfiguration=function(a){p=q(a)?a:{};J();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){u=a;C=b;J();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,d=c.length,g=0;g diff --git a/docs/binding/options/OPTIONS.md b/docs/binding/options/OPTIONS.md index 704f8c0..fe9a6e8 100644 --- a/docs/binding/options/OPTIONS.md +++ b/docs/binding/options/OPTIONS.md @@ -13,6 +13,7 @@ Below is a list of all the options supported in the "data-observe-options" bindi | *number* | observeTimeout | States the delay that should be waited before checking the object for changes (defaults to 250 milliseconds). | | *date* | expires | States a date/time when the watch should expire (defaults to null). | | *boolean* | reset | States if the original value of the object should be reset back to its original value when a change is detected (defaults to false). | +| *boolean* | cancelOnChange | States if the watch should be canceled when te first change is detected (defaults to false). |
diff --git a/src/observe.js b/src/observe.js index 8ea62f6..0d6a9d3 100644 --- a/src/observe.js +++ b/src/observe.js @@ -128,8 +128,7 @@ observeObject( storageId ); if ( isDefinedDate( observeOptions.expires ) && currentDateTime > observeOptions.expires ) { - clearTimeout( _observables[ storageId ].timer ); - delete _observables[ storageId ]; + cancelWatchObject( storageId ); } }, observeOptions.observeTimeout ); @@ -181,6 +180,10 @@ compareObservableObjectProperties( oldValue, newValue, options ); } } + + if ( options.cancelOnChange ) { + cancelWatchObject( storageId ); + } } } @@ -206,6 +209,11 @@ } } + function cancelWatchObject( storageId ) { + clearTimeout( _observables[ storageId ].timer ); + delete _observables[ storageId ]; + } + /* * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -219,6 +227,7 @@ options.observeTimeout = getDefaultNumber( options.observeTimeout, 250 ); options.expires = getDefaultDate( options.expires, null ); options.reset = getDefaultBoolean( options.reset, false ); + options.cancelOnChange = getDefaultBoolean( options.cancelOnChange, false ); options = getObserveOptionsCustomTriggers( options ); @@ -427,8 +436,7 @@ var result = false; if ( _observables.hasOwnProperty( id ) ) { - clearTimeout( _observables[ id ].timer ); - delete _observables[ id ]; + cancelWatchObject( id ); result = true; } @@ -452,8 +460,7 @@ for ( var storageId in _observables ) { if ( _observables.hasOwnProperty( storageId ) && isDefinedString( _observables[ storageId ].domElementId ) && _observables[ storageId ].domElementId === elementId ) { - clearTimeout( _observables[ storageId ].timer ); - delete _observables[ storageId ]; + cancelWatchObject( storageId ); result = true; break; From 1f2a3b9b75a911b960730bc9a3a30fceafaccd78 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 10:29:22 +0000 Subject: [PATCH 04/20] Changes are now detected for HTML DOM element objects when attributes are updated. --- dist/observe.js | 8 ++++---- dist/observe.min.js | 4 ++-- docs/CHANGE_LOG.md | 3 +++ src/observe.js | 8 ++++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 16f0423..7a3c9a7 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -54,8 +54,8 @@ if (isDefinedString(domElementId)) { var domElement = _parameter_Document.getElementById(domElementId); if (isDefined(domElement)) { - _observables[storageId].cachedObject = domElement.innerHTML; - _observables[storageId].originalObject = domElement.innerHTML; + _observables[storageId].cachedObject = domElement.outerHTML; + _observables[storageId].originalObject = domElement.outerHTML; } } else { _observables[storageId].cachedObject = JSON.stringify(object); @@ -77,7 +77,7 @@ if (isDomElement) { domElement = _parameter_Document.getElementById(_observables[storageId].domElementId); if (isDefined(domElement)) { - _observables[storageId].originalObject = domElement.innerHTML; + _observables[storageId].originalObject = domElement.outerHTML; } } var cachedObject = _observables[storageId].cachedObject; @@ -87,7 +87,7 @@ var options = _observables[storageId].options; if (options.reset) { if (isDomElement) { - domElement.innerHTML = _observables[storageId].cachedObject; + domElement.outerHTML = _observables[storageId].cachedObject; } else { _observables[storageId].originalObject = getObjectFromString(cachedObject).result; } diff --git a/dist/observe.min.js b/dist/observe.min.js index 5ff13ce..5be0851 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,6 +1,6 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ -(function(){function D(a,b,c){var d=null;if(q(a)){d=E();var g=F(b);e[d]={};e[d].options=g;e[d].domElementId=c;t(c)?(a=u.getElementById(c),n(a)&&(e[d].cachedObject=a.innerHTML,e[d].originalObject=a.innerHTML)):(e[d].cachedObject=JSON.stringify(a),e[d].originalObject=a);e[d].timer=setInterval(function(){var k=new Date,l=d,m=t(e[l].domElementId),h=null;m&&(h=u.getElementById(e[l].domElementId),n(h)&&(e[l].originalObject=h.innerHTML));var r=e[l].cachedObject,f=e[l].originalObject,w=m?f:JSON.stringify(f); -r!==w&&(f=e[l].options,f.reset?m?h.innerHTML=e[l].cachedObject:e[l].originalObject=x(r).result:e[l].cachedObject=w,m?z(f.onChange,r,w):(m=x(r).result,h=x(w).result,z(f.onChange,m,h),v(f.onPropertyChange)&&!G(m)&&H(m,h,f)),f.cancelOnChange&&y(l));I(g.expires)&&k>g.expires&&y(d)},g.observeTimeout)}return d}function H(a,b,c){for(var d in a)if(a.hasOwnProperty(d)){var g=a[d],k=null;b.hasOwnProperty(d)&&(k=b[d]);q(g)&&q(k)?H(g,k,c):JSON.stringify(g)!==JSON.stringify(k)&&z(c.onPropertyChange,d,g,k)}}function y(a){clearTimeout(e[a].timer); +(function(){function D(a,b,c){var d=null;if(q(a)){d=E();var g=F(b);e[d]={};e[d].options=g;e[d].domElementId=c;t(c)?(a=u.getElementById(c),n(a)&&(e[d].cachedObject=a.outerHTML,e[d].originalObject=a.outerHTML)):(e[d].cachedObject=JSON.stringify(a),e[d].originalObject=a);e[d].timer=setInterval(function(){var k=new Date,l=d,m=t(e[l].domElementId),h=null;m&&(h=u.getElementById(e[l].domElementId),n(h)&&(e[l].originalObject=h.outerHTML));var r=e[l].cachedObject,f=e[l].originalObject,w=m?f:JSON.stringify(f); +r!==w&&(f=e[l].options,f.reset?m?h.outerHTML=e[l].cachedObject:e[l].originalObject=x(r).result:e[l].cachedObject=w,m?z(f.onChange,r,w):(m=x(r).result,h=x(w).result,z(f.onChange,m,h),v(f.onPropertyChange)&&!G(m)&&H(m,h,f)),f.cancelOnChange&&y(l));I(g.expires)&&k>g.expires&&y(d)},g.observeTimeout)}return d}function H(a,b,c){for(var d in a)if(a.hasOwnProperty(d)){var g=a[d],k=null;b.hasOwnProperty(d)&&(k=b[d]);q(g)&&q(k)?H(g,k,c):JSON.stringify(g)!==JSON.stringify(k)&&z(c.onPropertyChange,d,g,k)}}function y(a){clearTimeout(e[a].timer); delete e[a]}function F(a){var b=a=q(a)?a:{},c=a.observeTimeout;var d=n(c)&&"number"===typeof c;b.observeTimeout=d?c:250;b=a;c=a.expires;c=I(c)?c:null;b.expires=c;a.reset=A(a.reset,!1);a.cancelOnChange=A(a.cancelOnChange,!1);b=a.onChange;b=v(b)?b:null;a.onChange=b;b=a.onPropertyChange;b=v(b)?b:null;a.onPropertyChange=b;return a}function z(a){v(a)&&a.apply(null,[].slice.call(arguments,1))}function E(){for(var a=[],b=0;32>b;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 n(a){return null!==a&&void 0!==a&&a!==B.empty}function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function v(a){return n(a)&&"function"===typeof a}function G(a){return q(a)&&a instanceof Array}function I(a){return q(a)&&a instanceof Date}function A(a,b){return n(a)&&"boolean"===typeof a?a:b}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(d){try{c=eval("("+a+")"),v(c)&&(c=c())}catch(g){b=K("Errors in object: "+ d.message+", "+g.message),c=null}}return{parsed:b,result:c}}function K(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function J(){p.safeMode=A(p.safeMode,!0);var a=p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(B.space),0===b.length&&(b=c)):b=G(b)?b:c;a.domElementTypes=b}var u=null,C=null,B={empty:""},e={},p={};this.watchObject=function(a,b){return D(a,b)};this.cancelWatch=function(a){var b=!1;e.hasOwnProperty(a)&&(y(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in e)if(e.hasOwnProperty(c)&& diff --git a/docs/CHANGE_LOG.md b/docs/CHANGE_LOG.md index 682a21e..e4d06c5 100644 --- a/docs/CHANGE_LOG.md +++ b/docs/CHANGE_LOG.md @@ -10,6 +10,9 @@ - Added a new binding/option called "reset", which states if an object should be reset back to its original value when a change is detected (defaults to false). - Added a new binding/option called "cancelOnChange", which states if a watch should be canceled when the first change is detected (defaults to false). +#### **Watching:** +- Changes are now detected for HTML DOM element objects when attributes are updated. +
diff --git a/src/observe.js b/src/observe.js index 0d6a9d3..4281da4 100644 --- a/src/observe.js +++ b/src/observe.js @@ -113,8 +113,8 @@ var domElement = _parameter_Document.getElementById( domElementId ); if ( isDefined( domElement ) ) { - _observables[ storageId ].cachedObject = domElement.innerHTML; - _observables[ storageId ].originalObject = domElement.innerHTML; + _observables[ storageId ].cachedObject = domElement.outerHTML; + _observables[ storageId ].originalObject = domElement.outerHTML; } } else { @@ -145,7 +145,7 @@ domElement = _parameter_Document.getElementById( _observables[ storageId ].domElementId ); if ( isDefined( domElement ) ) { - _observables[ storageId ].originalObject = domElement.innerHTML; + _observables[ storageId ].originalObject = domElement.outerHTML; } } @@ -158,7 +158,7 @@ if ( options.reset ) { if ( isDomElement ) { - domElement.innerHTML = _observables[ storageId ].cachedObject; + domElement.outerHTML = _observables[ storageId ].cachedObject; } else { _observables[ storageId ].originalObject = getObjectFromString( cachedObject ).result; } From e6dd8b4b34a6460b69d3e0f672a3a06c97d74d65 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 10:31:59 +0000 Subject: [PATCH 05/20] Fixed the "expires" binding/option not accurately checking the expired date and time. --- dist/observe.js | 2 +- dist/observe.min.js | 2 +- docs/CHANGE_LOG.md | 3 +++ src/observe.js | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 7a3c9a7..45e323f 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -64,7 +64,7 @@ _observables[storageId].timer = setInterval(function() { var currentDateTime = new Date(); observeObject(storageId); - if (isDefinedDate(observeOptions.expires) && currentDateTime > observeOptions.expires) { + if (isDefinedDate(observeOptions.expires) && currentDateTime >= observeOptions.expires) { cancelWatchObject(storageId); } }, observeOptions.observeTimeout); diff --git a/dist/observe.min.js b/dist/observe.min.js index 5be0851..d243d4d 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,6 +1,6 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ (function(){function D(a,b,c){var d=null;if(q(a)){d=E();var g=F(b);e[d]={};e[d].options=g;e[d].domElementId=c;t(c)?(a=u.getElementById(c),n(a)&&(e[d].cachedObject=a.outerHTML,e[d].originalObject=a.outerHTML)):(e[d].cachedObject=JSON.stringify(a),e[d].originalObject=a);e[d].timer=setInterval(function(){var k=new Date,l=d,m=t(e[l].domElementId),h=null;m&&(h=u.getElementById(e[l].domElementId),n(h)&&(e[l].originalObject=h.outerHTML));var r=e[l].cachedObject,f=e[l].originalObject,w=m?f:JSON.stringify(f); -r!==w&&(f=e[l].options,f.reset?m?h.outerHTML=e[l].cachedObject:e[l].originalObject=x(r).result:e[l].cachedObject=w,m?z(f.onChange,r,w):(m=x(r).result,h=x(w).result,z(f.onChange,m,h),v(f.onPropertyChange)&&!G(m)&&H(m,h,f)),f.cancelOnChange&&y(l));I(g.expires)&&k>g.expires&&y(d)},g.observeTimeout)}return d}function H(a,b,c){for(var d in a)if(a.hasOwnProperty(d)){var g=a[d],k=null;b.hasOwnProperty(d)&&(k=b[d]);q(g)&&q(k)?H(g,k,c):JSON.stringify(g)!==JSON.stringify(k)&&z(c.onPropertyChange,d,g,k)}}function y(a){clearTimeout(e[a].timer); +r!==w&&(f=e[l].options,f.reset?m?h.outerHTML=e[l].cachedObject:e[l].originalObject=x(r).result:e[l].cachedObject=w,m?z(f.onChange,r,w):(m=x(r).result,h=x(w).result,z(f.onChange,m,h),v(f.onPropertyChange)&&!G(m)&&H(m,h,f)),f.cancelOnChange&&y(l));I(g.expires)&&k>=g.expires&&y(d)},g.observeTimeout)}return d}function H(a,b,c){for(var d in a)if(a.hasOwnProperty(d)){var g=a[d],k=null;b.hasOwnProperty(d)&&(k=b[d]);q(g)&&q(k)?H(g,k,c):JSON.stringify(g)!==JSON.stringify(k)&&z(c.onPropertyChange,d,g,k)}}function y(a){clearTimeout(e[a].timer); delete e[a]}function F(a){var b=a=q(a)?a:{},c=a.observeTimeout;var d=n(c)&&"number"===typeof c;b.observeTimeout=d?c:250;b=a;c=a.expires;c=I(c)?c:null;b.expires=c;a.reset=A(a.reset,!1);a.cancelOnChange=A(a.cancelOnChange,!1);b=a.onChange;b=v(b)?b:null;a.onChange=b;b=a.onPropertyChange;b=v(b)?b:null;a.onPropertyChange=b;return a}function z(a){v(a)&&a.apply(null,[].slice.call(arguments,1))}function E(){for(var a=[],b=0;32>b;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 n(a){return null!==a&&void 0!==a&&a!==B.empty}function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function v(a){return n(a)&&"function"===typeof a}function G(a){return q(a)&&a instanceof Array}function I(a){return q(a)&&a instanceof Date}function A(a,b){return n(a)&&"boolean"===typeof a?a:b}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(d){try{c=eval("("+a+")"),v(c)&&(c=c())}catch(g){b=K("Errors in object: "+ d.message+", "+g.message),c=null}}return{parsed:b,result:c}}function K(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function J(){p.safeMode=A(p.safeMode,!0);var a=p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(B.space),0===b.length&&(b=c)):b=G(b)?b:c;a.domElementTypes=b}var u=null,C=null,B={empty:""},e={},p={};this.watchObject=function(a,b){return D(a,b)};this.cancelWatch=function(a){var b=!1;e.hasOwnProperty(a)&&(y(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in e)if(e.hasOwnProperty(c)&& diff --git a/docs/CHANGE_LOG.md b/docs/CHANGE_LOG.md index e4d06c5..32ded77 100644 --- a/docs/CHANGE_LOG.md +++ b/docs/CHANGE_LOG.md @@ -13,6 +13,9 @@ #### **Watching:** - Changes are now detected for HTML DOM element objects when attributes are updated. +#### **Fixes:** +- Fixed the "expires" binding/option not accurately checking the expired date and time. +
diff --git a/src/observe.js b/src/observe.js index 4281da4..0e5062f 100644 --- a/src/observe.js +++ b/src/observe.js @@ -127,7 +127,7 @@ observeObject( storageId ); - if ( isDefinedDate( observeOptions.expires ) && currentDateTime > observeOptions.expires ) { + if ( isDefinedDate( observeOptions.expires ) && currentDateTime >= observeOptions.expires ) { cancelWatchObject( storageId ); } From 6dbfc7d52d5427d6ad7796fde23905f52abc788e Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 10:39:09 +0000 Subject: [PATCH 06/20] Added "Maximum Changes Before Canceling" support, which states how many changes are allowed before a watch is canceled. --- dist/observe.js | 12 ++++++++++-- dist/observe.min.js | 15 ++++++++------- docs/CHANGE_LOG.md | 6 ++++-- docs/binding/options/OPTIONS.md | 1 + src/observe.js | 14 ++++++++++++-- 5 files changed, 35 insertions(+), 13 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 45e323f..7dc6293 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -51,6 +51,7 @@ _observables[storageId] = {}; _observables[storageId].options = observeOptions; _observables[storageId].domElementId = domElementId; + _observables[storageId].totalChanges = 0; if (isDefinedString(domElementId)) { var domElement = _parameter_Document.getElementById(domElementId); if (isDefined(domElement)) { @@ -107,6 +108,10 @@ if (options.cancelOnChange) { cancelWatchObject(storageId); } + _observables[storageId].totalChanges++; + if (options.maximumChangesBeforeCanceling > 0 && _observables[storageId].totalChanges >= options.maximumChangesBeforeCanceling) { + cancelWatchObject(storageId); + } } } function compareObservableObjectProperties(oldObject, newObject, options) { @@ -129,8 +134,10 @@ } } function cancelWatchObject(storageId) { - clearTimeout(_observables[storageId].timer); - delete _observables[storageId]; + if (_observables.hasOwnProperty(storageId)) { + clearTimeout(_observables[storageId].timer); + delete _observables[storageId]; + } } function getObserveOptions(newOptions) { var options = !isDefinedObject(newOptions) ? {} : newOptions; @@ -138,6 +145,7 @@ options.expires = getDefaultDate(options.expires, null); options.reset = getDefaultBoolean(options.reset, false); options.cancelOnChange = getDefaultBoolean(options.cancelOnChange, false); + options.maximumChangesBeforeCanceling = getDefaultNumber(options.maximumChangesBeforeCanceling, 0); options = getObserveOptionsCustomTriggers(options); return options; } diff --git a/dist/observe.min.js b/dist/observe.min.js index d243d4d..9a89f5c 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,8 +1,9 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ -(function(){function D(a,b,c){var d=null;if(q(a)){d=E();var g=F(b);e[d]={};e[d].options=g;e[d].domElementId=c;t(c)?(a=u.getElementById(c),n(a)&&(e[d].cachedObject=a.outerHTML,e[d].originalObject=a.outerHTML)):(e[d].cachedObject=JSON.stringify(a),e[d].originalObject=a);e[d].timer=setInterval(function(){var k=new Date,l=d,m=t(e[l].domElementId),h=null;m&&(h=u.getElementById(e[l].domElementId),n(h)&&(e[l].originalObject=h.outerHTML));var r=e[l].cachedObject,f=e[l].originalObject,w=m?f:JSON.stringify(f); -r!==w&&(f=e[l].options,f.reset?m?h.outerHTML=e[l].cachedObject:e[l].originalObject=x(r).result:e[l].cachedObject=w,m?z(f.onChange,r,w):(m=x(r).result,h=x(w).result,z(f.onChange,m,h),v(f.onPropertyChange)&&!G(m)&&H(m,h,f)),f.cancelOnChange&&y(l));I(g.expires)&&k>=g.expires&&y(d)},g.observeTimeout)}return d}function H(a,b,c){for(var d in a)if(a.hasOwnProperty(d)){var g=a[d],k=null;b.hasOwnProperty(d)&&(k=b[d]);q(g)&&q(k)?H(g,k,c):JSON.stringify(g)!==JSON.stringify(k)&&z(c.onPropertyChange,d,g,k)}}function y(a){clearTimeout(e[a].timer); -delete e[a]}function F(a){var b=a=q(a)?a:{},c=a.observeTimeout;var d=n(c)&&"number"===typeof c;b.observeTimeout=d?c:250;b=a;c=a.expires;c=I(c)?c:null;b.expires=c;a.reset=A(a.reset,!1);a.cancelOnChange=A(a.cancelOnChange,!1);b=a.onChange;b=v(b)?b:null;a.onChange=b;b=a.onPropertyChange;b=v(b)?b:null;a.onPropertyChange=b;return a}function z(a){v(a)&&a.apply(null,[].slice.call(arguments,1))}function E(){for(var a=[],b=0;32>b;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 n(a){return null!==a&&void 0!==a&&a!==B.empty}function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function v(a){return n(a)&&"function"===typeof a}function G(a){return q(a)&&a instanceof Array}function I(a){return q(a)&&a instanceof Date}function A(a,b){return n(a)&&"boolean"===typeof a?a:b}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(d){try{c=eval("("+a+")"),v(c)&&(c=c())}catch(g){b=K("Errors in object: "+ -d.message+", "+g.message),c=null}}return{parsed:b,result:c}}function K(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function J(){p.safeMode=A(p.safeMode,!0);var a=p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(B.space),0===b.length&&(b=c)):b=G(b)?b:c;a.domElementTypes=b}var u=null,C=null,B={empty:""},e={},p={};this.watchObject=function(a,b){return D(a,b)};this.cancelWatch=function(a){var b=!1;e.hasOwnProperty(a)&&(y(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in e)if(e.hasOwnProperty(c)&& -t(e[c].domElementId)&&e[c].domElementId===a){y(c);b=!0;break}return b};this.setConfiguration=function(a){p=q(a)?a:{};J();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){u=a;C=b;J();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,d=c.length,g=0;g=f.maximumChangesBeforeCanceling&&w(h));I(g.expires)&&l>=g.expires&&w(e)},g.observeTimeout)}return e}function H(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var g= +a[e],l=null;b.hasOwnProperty(e)&&(l=b[e]);q(g)&&q(l)?H(g,l,c):JSON.stringify(g)!==JSON.stringify(l)&&z(c.onPropertyChange,e,g,l)}}function w(a){d.hasOwnProperty(a)&&(clearTimeout(d[a].timer),delete d[a])}function F(a){var b=a=q(a)?a:{};var c=a.observeTimeout;c=J(c)?c:250;b.observeTimeout=c;b=a;c=a.expires;c=I(c)?c:null;b.expires=c;a.reset=A(a.reset,!1);a.cancelOnChange=A(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c=J(c)?c:0;b.maximumChangesBeforeCanceling=c;b=a.onChange;b=v(b)?b:null; +a.onChange=b;b=a.onPropertyChange;b=v(b)?b:null;a.onPropertyChange=b;return a}function z(a){v(a)&&a.apply(null,[].slice.call(arguments,1))}function E(){for(var a=[],b=0;32>b;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 n(a){return null!==a&&void 0!==a&&a!==B.empty}function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function v(a){return n(a)&&"function"===typeof a} +function J(a){return n(a)&&"number"===typeof a}function G(a){return q(a)&&a instanceof Array}function I(a){return q(a)&&a instanceof Date}function A(a,b){return n(a)&&"boolean"===typeof a?a:b}function y(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),v(c)&&(c=c())}catch(g){b=L("Errors in object: "+e.message+", "+g.message),c=null}}return{parsed:b,result:c}}function L(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function K(){p.safeMode=A(p.safeMode,!0);var a= +p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(B.space),0===b.length&&(b=c)):b=G(b)?b:c;a.domElementTypes=b}var u=null,C=null,B={empty:""},d={},p={};this.watchObject=function(a,b){return D(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(w(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){w(c);b=!0;break}return b};this.setConfiguration=function(a){p=q(a)?a:{};K();return this};this.getVersion= +function(){return"0.2.0"};(function(a,b){u=a;C=b;K();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,g=0;g diff --git a/src/observe.js b/src/observe.js index 0e5062f..73794bd 100644 --- a/src/observe.js +++ b/src/observe.js @@ -108,6 +108,7 @@ _observables[ storageId ] = {}; _observables[ storageId ].options = observeOptions; _observables[ storageId ].domElementId = domElementId; + _observables[ storageId ].totalChanges = 0; if ( isDefinedString( domElementId ) ) { var domElement = _parameter_Document.getElementById( domElementId ); @@ -184,6 +185,12 @@ if ( options.cancelOnChange ) { cancelWatchObject( storageId ); } + + _observables[ storageId ].totalChanges++; + + if ( options.maximumChangesBeforeCanceling > 0 && _observables[ storageId ].totalChanges >= options.maximumChangesBeforeCanceling ) { + cancelWatchObject( storageId ); + } } } @@ -210,8 +217,10 @@ } function cancelWatchObject( storageId ) { - clearTimeout( _observables[ storageId ].timer ); - delete _observables[ storageId ]; + if ( _observables.hasOwnProperty( storageId ) ) { + clearTimeout( _observables[ storageId ].timer ); + delete _observables[ storageId ]; + } } @@ -228,6 +237,7 @@ options.expires = getDefaultDate( options.expires, null ); options.reset = getDefaultBoolean( options.reset, false ); options.cancelOnChange = getDefaultBoolean( options.cancelOnChange, false ); + options.maximumChangesBeforeCanceling = getDefaultNumber( options.maximumChangesBeforeCanceling, 0 ); options = getObserveOptionsCustomTriggers( options ); From 107a65bad886f1876dca6e83d0b1afceb4ac492c Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 10:40:09 +0000 Subject: [PATCH 07/20] Improved HTML testing files. --- test/dist/observe.js.basic.html | 3 ++- test/dist/observe.js.min.html | 3 ++- test/src/observe.js.basic.html | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/test/dist/observe.js.basic.html b/test/dist/observe.js.basic.html index bee67bf..e90660e 100644 --- a/test/dist/observe.js.basic.html +++ b/test/dist/observe.js.basic.html @@ -36,7 +36,8 @@

Change Object Values:


- + +

Configuration:

diff --git a/test/dist/observe.js.min.html b/test/dist/observe.js.min.html index e657f6c..ed822b1 100644 --- a/test/dist/observe.js.min.html +++ b/test/dist/observe.js.min.html @@ -36,7 +36,8 @@

Change Object Values:


- + +

Configuration:

diff --git a/test/src/observe.js.basic.html b/test/src/observe.js.basic.html index 5da942e..634126b 100644 --- a/test/src/observe.js.basic.html +++ b/test/src/observe.js.basic.html @@ -36,7 +36,8 @@

Change Object Values:


- + +

Configuration:

From 7c98316e861c315fa74962ae690519a59fdb7885 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 10:49:32 +0000 Subject: [PATCH 08/20] Fixed some of the documentation referencing the wrong properties. --- docs/CHANGE_LOG.md | 3 +++ docs/binding/options/OPTIONS.md | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/docs/CHANGE_LOG.md b/docs/CHANGE_LOG.md index 438a8a7..6777376 100644 --- a/docs/CHANGE_LOG.md +++ b/docs/CHANGE_LOG.md @@ -15,6 +15,9 @@ #### **Watching:** - Changes are now detected for HTML DOM element objects when attributes are updated. +#### **Documentation:** +- Fixed some of the documentation referencing the wrong properties. + #### **Fixes:** - Fixed the "expires" binding/option not accurately checking the expired date and time. diff --git a/docs/binding/options/OPTIONS.md b/docs/binding/options/OPTIONS.md index c8282d1..86b6a16 100644 --- a/docs/binding/options/OPTIONS.md +++ b/docs/binding/options/OPTIONS.md @@ -22,7 +22,7 @@ Below is a list of all the options supported in the "data-observe-options" bindi
```markdown -
+
Your HTML.
``` @@ -36,7 +36,7 @@ Below is a list of all the options supported in the "data-observe-options" bindi ```markdown ``` \ No newline at end of file From 565540607baf941f856b21a17dcbf26f74465898 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 10:53:27 +0000 Subject: [PATCH 09/20] More HTML updates. --- test/dist/observe.js.basic.html | 2 +- test/dist/observe.js.min.html | 2 +- test/src/observe.js.basic.html | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/dist/observe.js.basic.html b/test/dist/observe.js.basic.html index e90660e..07a4b63 100644 --- a/test/dist/observe.js.basic.html +++ b/test/dist/observe.js.basic.html @@ -11,7 +11,7 @@

Observe.js - Basic

-

This is a basic example of how to use Observe.js an HTML DOM element.

+

This is a basic example of how to use Observe.js to watch HTML DOM elements, and JS objects.

diff --git a/test/dist/observe.js.min.html b/test/dist/observe.js.min.html index ed822b1..1058fa5 100644 --- a/test/dist/observe.js.min.html +++ b/test/dist/observe.js.min.html @@ -11,7 +11,7 @@

Observe.js - Basic

-

This is a basic example of how to use Observe.js an HTML DOM element.

+

This is a basic example of how to use Observe.js to watch HTML DOM elements, and JS objects.

diff --git a/test/src/observe.js.basic.html b/test/src/observe.js.basic.html index 634126b..8327a78 100644 --- a/test/src/observe.js.basic.html +++ b/test/src/observe.js.basic.html @@ -11,7 +11,7 @@

Observe.js - Basic

-

This is a basic example of how to use Observe.js an HTML DOM element.

+

This is a basic example of how to use Observe.js to watch HTML DOM elements, and JS objects.

From 9886e76a0e3faf100ae5b63565d1ac63c11379b1 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 10:59:38 +0000 Subject: [PATCH 10/20] Added a new binding/option custom trigger called "onCancel", which states an event that should be triggered when a watch is canceled. --- dist/observe.js | 3 +++ dist/observe.min.js | 16 ++++++++-------- docs/CHANGE_LOG.md | 3 +++ docs/binding/options/CUSTOM_TRIGGERS.md | 5 +++++ src/observe.js | 5 +++++ test/dist/observe.js.basic.html | 12 +++++++++--- test/dist/observe.js.min.html | 12 +++++++++--- test/src/observe.js.basic.html | 12 +++++++++--- 8 files changed, 51 insertions(+), 17 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 7dc6293..e9bebc2 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -135,6 +135,8 @@ } function cancelWatchObject(storageId) { if (_observables.hasOwnProperty(storageId)) { + var options = _observables[storageId].options; + fireCustomTrigger(options.onCancel, storageId); clearTimeout(_observables[storageId].timer); delete _observables[storageId]; } @@ -152,6 +154,7 @@ function getObserveOptionsCustomTriggers(options) { options.onChange = getDefaultFunction(options.onChange, null); options.onPropertyChange = getDefaultFunction(options.onPropertyChange, null); + options.onCancel = getDefaultFunction(options.onCancel, null); return options; } function fireCustomTrigger(triggerFunction) { diff --git a/dist/observe.min.js b/dist/observe.min.js index 9a89f5c..32e000b 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,9 +1,9 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ -(function(){function D(a,b,c){var e=null;if(q(a)){e=E();var g=F(b);d[e]={};d[e].options=g;d[e].domElementId=c;d[e].totalChanges=0;t(c)?(a=u.getElementById(c),n(a)&&(d[e].cachedObject=a.outerHTML,d[e].originalObject=a.outerHTML)):(d[e].cachedObject=JSON.stringify(a),d[e].originalObject=a);d[e].timer=setInterval(function(){var l=new Date,h=e,m=t(d[h].domElementId),k=null;m&&(k=u.getElementById(d[h].domElementId),n(k)&&(d[h].originalObject=k.outerHTML));var r=d[h].cachedObject,f=d[h].originalObject, -x=m?f:JSON.stringify(f);r!==x&&(f=d[h].options,f.reset?m?k.outerHTML=d[h].cachedObject:d[h].originalObject=y(r).result:d[h].cachedObject=x,m?z(f.onChange,r,x):(m=y(r).result,k=y(x).result,z(f.onChange,m,k),v(f.onPropertyChange)&&!G(m)&&H(m,k,f)),f.cancelOnChange&&w(h),d[h].totalChanges++,0=f.maximumChangesBeforeCanceling&&w(h));I(g.expires)&&l>=g.expires&&w(e)},g.observeTimeout)}return e}function H(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var g= -a[e],l=null;b.hasOwnProperty(e)&&(l=b[e]);q(g)&&q(l)?H(g,l,c):JSON.stringify(g)!==JSON.stringify(l)&&z(c.onPropertyChange,e,g,l)}}function w(a){d.hasOwnProperty(a)&&(clearTimeout(d[a].timer),delete d[a])}function F(a){var b=a=q(a)?a:{};var c=a.observeTimeout;c=J(c)?c:250;b.observeTimeout=c;b=a;c=a.expires;c=I(c)?c:null;b.expires=c;a.reset=A(a.reset,!1);a.cancelOnChange=A(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c=J(c)?c:0;b.maximumChangesBeforeCanceling=c;b=a.onChange;b=v(b)?b:null; -a.onChange=b;b=a.onPropertyChange;b=v(b)?b:null;a.onPropertyChange=b;return a}function z(a){v(a)&&a.apply(null,[].slice.call(arguments,1))}function E(){for(var a=[],b=0;32>b;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 n(a){return null!==a&&void 0!==a&&a!==B.empty}function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function v(a){return n(a)&&"function"===typeof a} -function J(a){return n(a)&&"number"===typeof a}function G(a){return q(a)&&a instanceof Array}function I(a){return q(a)&&a instanceof Date}function A(a,b){return n(a)&&"boolean"===typeof a?a:b}function y(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),v(c)&&(c=c())}catch(g){b=L("Errors in object: "+e.message+", "+g.message),c=null}}return{parsed:b,result:c}}function L(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function K(){p.safeMode=A(p.safeMode,!0);var a= -p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(B.space),0===b.length&&(b=c)):b=G(b)?b:c;a.domElementTypes=b}var u=null,C=null,B={empty:""},d={},p={};this.watchObject=function(a,b){return D(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(w(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){w(c);b=!0;break}return b};this.setConfiguration=function(a){p=q(a)?a:{};K();return this};this.getVersion= -function(){return"0.2.0"};(function(a,b){u=a;C=b;K();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,g=0;g=f.maximumChangesBeforeCanceling&&v(h));J(g.expires)&&l>=g.expires&&v(e)},g.observeTimeout)}return e}function I(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var g= +a[e],l=null;b.hasOwnProperty(e)&&(l=b[e]);q(g)&&q(l)?I(g,l,c):JSON.stringify(g)!==JSON.stringify(l)&&y(c.onPropertyChange,e,g,l)}}function v(a){d.hasOwnProperty(a)&&(y(d[a].options.onCancel,a),clearTimeout(d[a].timer),delete d[a])}function G(a){var b=a=q(a)?a:{};var c=a.observeTimeout;c=K(c)?c:250;b.observeTimeout=c;b=a;c=a.expires;c=J(c)?c:null;b.expires=c;a.reset=A(a.reset,!1);a.cancelOnChange=A(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c=K(c)?c:0;b.maximumChangesBeforeCanceling= +c;a.onChange=B(a.onChange,null);a.onPropertyChange=B(a.onPropertyChange,null);a.onCancel=B(a.onCancel,null);return a}function y(a){z(a)&&a.apply(null,[].slice.call(arguments,1))}function F(){for(var a=[],b=0;32>b;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 n(a){return null!==a&&void 0!==a&&a!==C.empty}function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function z(a){return n(a)&& +"function"===typeof a}function K(a){return n(a)&&"number"===typeof a}function H(a){return q(a)&&a instanceof Array}function J(a){return q(a)&&a instanceof Date}function A(a,b){return n(a)&&"boolean"===typeof a?a:b}function B(a,b){return z(a)?a:b}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),z(c)&&(c=c())}catch(g){b=M("Errors in object: "+e.message+", "+g.message),c=null}}return{parsed:b,result:c}}function M(a){var b=!0;p.safeMode||(console.error(a),b=!1); +return b}function L(){p.safeMode=A(p.safeMode,!0);var a=p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(C.space),0===b.length&&(b=c)):b=H(b)?b:c;a.domElementTypes=b}var u=null,D=null,C={empty:""},d={},p={};this.watchObject=function(a,b){return E(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(v(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){v(c);b=!0;break}return b};this.setConfiguration= +function(a){p=q(a)?a:{};L();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){u=a;D=b;L();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,g=0;g ***Parameter:*** newValue: '*object*' - The new value. +### options.onCancel( *id* ): +Fires when a watch has been canceled. +
+***Parameter:*** id: '*string*' - The ID of the watch that has been canceled. +
diff --git a/src/observe.js b/src/observe.js index 73794bd..1046131 100644 --- a/src/observe.js +++ b/src/observe.js @@ -218,6 +218,10 @@ function cancelWatchObject( storageId ) { if ( _observables.hasOwnProperty( storageId ) ) { + var options = _observables[ storageId ].options; + + fireCustomTrigger( options.onCancel, storageId ); + clearTimeout( _observables[ storageId ].timer ); delete _observables[ storageId ]; } @@ -247,6 +251,7 @@ function getObserveOptionsCustomTriggers( options ) { options.onChange = getDefaultFunction( options.onChange, null ); options.onPropertyChange = getDefaultFunction( options.onPropertyChange, null ); + options.onCancel = getDefaultFunction( options.onCancel, null ); return options; } diff --git a/test/dist/observe.js.basic.html b/test/dist/observe.js.basic.html index 07a4b63..f9677e4 100644 --- a/test/dist/observe.js.basic.html +++ b/test/dist/observe.js.basic.html @@ -15,11 +15,11 @@

Observe.js - Basic

-
+
When changed, this element should fire a custom trigger.
-
+
When changed, this element should fire a custom trigger (and reset its value back to the original).
@@ -63,7 +63,8 @@

Additional Data:

function watchNewObject() { watchObjectId = $observe.watchObject( window.observeObject, { onChange: onObservableObjectChange, - onPropertyChange: onObservableObjectPropertyChange + onPropertyChange: onObservableObjectPropertyChange, + onCancel: onCancelWatch } ); } @@ -85,5 +86,10 @@

Additional Data:

console.log( "New Value: " + JSON.stringify( newValue ) ); console.log( "" ); } + + function onCancelWatch( id ) { + console.log( "Watch " + id + " canceled." ); + console.log( "" ); + } \ No newline at end of file diff --git a/test/dist/observe.js.min.html b/test/dist/observe.js.min.html index 1058fa5..dd6c8ba 100644 --- a/test/dist/observe.js.min.html +++ b/test/dist/observe.js.min.html @@ -15,11 +15,11 @@

Observe.js - Basic

-
+
When changed, this element should fire a custom trigger.
-
+
When changed, this element should fire a custom trigger (and reset its value back to the original).
@@ -63,7 +63,8 @@

Additional Data:

function watchNewObject() { watchObjectId = $observe.watchObject( window.observeObject, { onChange: onObservableObjectChange, - onPropertyChange: onObservableObjectPropertyChange + onPropertyChange: onObservableObjectPropertyChange, + onCancel: onCancelWatch } ); } @@ -85,5 +86,10 @@

Additional Data:

console.log( "New Value: " + JSON.stringify( newValue ) ); console.log( "" ); } + + function onCancelWatch( id ) { + console.log( "Watch " + id + " canceled." ); + console.log( "" ); + } \ No newline at end of file diff --git a/test/src/observe.js.basic.html b/test/src/observe.js.basic.html index 8327a78..587ddc4 100644 --- a/test/src/observe.js.basic.html +++ b/test/src/observe.js.basic.html @@ -15,11 +15,11 @@

Observe.js - Basic

-
+
When changed, this element should fire a custom trigger.
-
+
When changed, this element should fire a custom trigger (and reset its value back to the original).
@@ -63,7 +63,8 @@

Additional Data:

function watchNewObject() { watchObjectId = $observe.watchObject( window.observeObject, { onChange: onObservableObjectChange, - onPropertyChange: onObservableObjectPropertyChange + onPropertyChange: onObservableObjectPropertyChange, + onCancel: onCancelWatch } ); } @@ -85,5 +86,10 @@

Additional Data:

console.log( "New Value: " + JSON.stringify( newValue ) ); console.log( "" ); } + + function onCancelWatch( id ) { + console.log( "Watch " + id + " canceled." ); + console.log( "" ); + } \ No newline at end of file From b3e7526d3bd488147c5c6e2f660c70a50fc24178 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 11:04:16 +0000 Subject: [PATCH 11/20] Fixed a script error that occurred sometimes when a watch is canceled using the public functions "cancelWatch()" and "cancelDomElementWatch()". --- dist/observe.js | 68 +++++++++++++++++++++--------------------- dist/observe.min.js | 10 +++---- docs/CHANGE_LOG.md | 1 + src/observe.js | 72 +++++++++++++++++++++++---------------------- 4 files changed, 78 insertions(+), 73 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index e9bebc2..3a0bb8c 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -73,44 +73,46 @@ return storageId; } function observeObject(storageId) { - var isDomElement = isDefinedString(_observables[storageId].domElementId); - var domElement = null; - if (isDomElement) { - domElement = _parameter_Document.getElementById(_observables[storageId].domElementId); - if (isDefined(domElement)) { - _observables[storageId].originalObject = domElement.outerHTML; + if (_observables.hasOwnProperty(storageId)) { + var isDomElement = isDefinedString(_observables[storageId].domElementId); + var domElement = null; + if (isDomElement) { + domElement = _parameter_Document.getElementById(_observables[storageId].domElementId); + if (isDefined(domElement)) { + _observables[storageId].originalObject = domElement.outerHTML; + } } - } - var cachedObject = _observables[storageId].cachedObject; - var originalObject = _observables[storageId].originalObject; - var originalObjectJson = !isDomElement ? JSON.stringify(originalObject) : originalObject; - if (cachedObject !== originalObjectJson) { - var options = _observables[storageId].options; - if (options.reset) { + var cachedObject = _observables[storageId].cachedObject; + var originalObject = _observables[storageId].originalObject; + var originalObjectJson = !isDomElement ? JSON.stringify(originalObject) : originalObject; + if (cachedObject !== originalObjectJson) { + var options = _observables[storageId].options; + if (options.reset) { + if (isDomElement) { + domElement.outerHTML = _observables[storageId].cachedObject; + } else { + _observables[storageId].originalObject = getObjectFromString(cachedObject).result; + } + } else { + _observables[storageId].cachedObject = originalObjectJson; + } if (isDomElement) { - domElement.outerHTML = _observables[storageId].cachedObject; + fireCustomTrigger(options.onChange, cachedObject, originalObjectJson); } else { - _observables[storageId].originalObject = getObjectFromString(cachedObject).result; + var oldValue = getObjectFromString(cachedObject).result; + var newValue = getObjectFromString(originalObjectJson).result; + fireCustomTrigger(options.onChange, oldValue, newValue); + if (isDefinedFunction(options.onPropertyChange) && !isDefinedArray(oldValue)) { + compareObservableObjectProperties(oldValue, newValue, options); + } } - } else { - _observables[storageId].cachedObject = originalObjectJson; - } - if (isDomElement) { - fireCustomTrigger(options.onChange, cachedObject, originalObjectJson); - } else { - var oldValue = getObjectFromString(cachedObject).result; - var newValue = getObjectFromString(originalObjectJson).result; - fireCustomTrigger(options.onChange, oldValue, newValue); - if (isDefinedFunction(options.onPropertyChange) && !isDefinedArray(oldValue)) { - compareObservableObjectProperties(oldValue, newValue, options); + if (options.cancelOnChange) { + cancelWatchObject(storageId); + } + _observables[storageId].totalChanges++; + if (options.maximumChangesBeforeCanceling > 0 && _observables[storageId].totalChanges >= options.maximumChangesBeforeCanceling) { + cancelWatchObject(storageId); } - } - if (options.cancelOnChange) { - cancelWatchObject(storageId); - } - _observables[storageId].totalChanges++; - if (options.maximumChangesBeforeCanceling > 0 && _observables[storageId].totalChanges >= options.maximumChangesBeforeCanceling) { - cancelWatchObject(storageId); } } } diff --git a/dist/observe.min.js b/dist/observe.min.js index 32e000b..af65804 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,9 +1,9 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ -(function(){function E(a,b,c){var e=null;if(q(a)){e=F();var g=G(b);d[e]={};d[e].options=g;d[e].domElementId=c;d[e].totalChanges=0;t(c)?(a=u.getElementById(c),n(a)&&(d[e].cachedObject=a.outerHTML,d[e].originalObject=a.outerHTML)):(d[e].cachedObject=JSON.stringify(a),d[e].originalObject=a);d[e].timer=setInterval(function(){var l=new Date,h=e,m=t(d[h].domElementId),k=null;m&&(k=u.getElementById(d[h].domElementId),n(k)&&(d[h].originalObject=k.outerHTML));var r=d[h].cachedObject,f=d[h].originalObject, -w=m?f:JSON.stringify(f);r!==w&&(f=d[h].options,f.reset?m?k.outerHTML=d[h].cachedObject:d[h].originalObject=x(r).result:d[h].cachedObject=w,m?y(f.onChange,r,w):(m=x(r).result,k=x(w).result,y(f.onChange,m,k),z(f.onPropertyChange)&&!H(m)&&I(m,k,f)),f.cancelOnChange&&v(h),d[h].totalChanges++,0=f.maximumChangesBeforeCanceling&&v(h));J(g.expires)&&l>=g.expires&&v(e)},g.observeTimeout)}return e}function I(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var g= -a[e],l=null;b.hasOwnProperty(e)&&(l=b[e]);q(g)&&q(l)?I(g,l,c):JSON.stringify(g)!==JSON.stringify(l)&&y(c.onPropertyChange,e,g,l)}}function v(a){d.hasOwnProperty(a)&&(y(d[a].options.onCancel,a),clearTimeout(d[a].timer),delete d[a])}function G(a){var b=a=q(a)?a:{};var c=a.observeTimeout;c=K(c)?c:250;b.observeTimeout=c;b=a;c=a.expires;c=J(c)?c:null;b.expires=c;a.reset=A(a.reset,!1);a.cancelOnChange=A(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c=K(c)?c:0;b.maximumChangesBeforeCanceling= +(function(){function E(a,b,c){var e=null;if(q(a)){e=F();var h=G(b);d[e]={};d[e].options=h;d[e].domElementId=c;d[e].totalChanges=0;t(c)?(a=u.getElementById(c),n(a)&&(d[e].cachedObject=a.outerHTML,d[e].originalObject=a.outerHTML)):(d[e].cachedObject=JSON.stringify(a),d[e].originalObject=a);d[e].timer=setInterval(function(){var l=new Date,g=e;if(d.hasOwnProperty(g)){var m=t(d[g].domElementId),k=null;m&&(k=u.getElementById(d[g].domElementId),n(k)&&(d[g].originalObject=k.outerHTML));var r=d[g].cachedObject, +f=d[g].originalObject,w=m?f:JSON.stringify(f);r!==w&&(f=d[g].options,f.reset?m?k.outerHTML=d[g].cachedObject:d[g].originalObject=x(r).result:d[g].cachedObject=w,m?y(f.onChange,r,w):(m=x(r).result,k=x(w).result,y(f.onChange,m,k),z(f.onPropertyChange)&&!H(m)&&I(m,k,f)),f.cancelOnChange&&v(g),d[g].totalChanges++,0=f.maximumChangesBeforeCanceling&&v(g))}J(h.expires)&&l>=h.expires&&v(e)},h.observeTimeout)}return e}function I(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var h= +a[e],l=null;b.hasOwnProperty(e)&&(l=b[e]);q(h)&&q(l)?I(h,l,c):JSON.stringify(h)!==JSON.stringify(l)&&y(c.onPropertyChange,e,h,l)}}function v(a){d.hasOwnProperty(a)&&(y(d[a].options.onCancel,a),clearTimeout(d[a].timer),delete d[a])}function G(a){var b=a=q(a)?a:{};var c=a.observeTimeout;c=K(c)?c:250;b.observeTimeout=c;b=a;c=a.expires;c=J(c)?c:null;b.expires=c;a.reset=A(a.reset,!1);a.cancelOnChange=A(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c=K(c)?c:0;b.maximumChangesBeforeCanceling= c;a.onChange=B(a.onChange,null);a.onPropertyChange=B(a.onPropertyChange,null);a.onCancel=B(a.onCancel,null);return a}function y(a){z(a)&&a.apply(null,[].slice.call(arguments,1))}function F(){for(var a=[],b=0;32>b;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 n(a){return null!==a&&void 0!==a&&a!==C.empty}function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function z(a){return n(a)&& -"function"===typeof a}function K(a){return n(a)&&"number"===typeof a}function H(a){return q(a)&&a instanceof Array}function J(a){return q(a)&&a instanceof Date}function A(a,b){return n(a)&&"boolean"===typeof a?a:b}function B(a,b){return z(a)?a:b}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),z(c)&&(c=c())}catch(g){b=M("Errors in object: "+e.message+", "+g.message),c=null}}return{parsed:b,result:c}}function M(a){var b=!0;p.safeMode||(console.error(a),b=!1); +"function"===typeof a}function K(a){return n(a)&&"number"===typeof a}function H(a){return q(a)&&a instanceof Array}function J(a){return q(a)&&a instanceof Date}function A(a,b){return n(a)&&"boolean"===typeof a?a:b}function B(a,b){return z(a)?a:b}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),z(c)&&(c=c())}catch(h){b=M("Errors in object: "+e.message+", "+h.message),c=null}}return{parsed:b,result:c}}function M(a){var b=!0;p.safeMode||(console.error(a),b=!1); return b}function L(){p.safeMode=A(p.safeMode,!0);var a=p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(C.space),0===b.length&&(b=c)):b=H(b)?b:c;a.domElementTypes=b}var u=null,D=null,C={empty:""},d={},p={};this.watchObject=function(a,b){return E(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(v(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){v(c);b=!0;break}return b};this.setConfiguration= -function(a){p=q(a)?a:{};L();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){u=a;D=b;L();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,g=0;g diff --git a/src/observe.js b/src/observe.js index 1046131..74d3238 100644 --- a/src/observe.js +++ b/src/observe.js @@ -139,57 +139,59 @@ } function observeObject( storageId ) { - var isDomElement = isDefinedString( _observables[ storageId ].domElementId ), - domElement = null; + if ( _observables.hasOwnProperty( storageId ) ) { + var isDomElement = isDefinedString( _observables[ storageId ].domElementId ), + domElement = null; - if ( isDomElement ) { - domElement = _parameter_Document.getElementById( _observables[ storageId ].domElementId ); + if ( isDomElement ) { + domElement = _parameter_Document.getElementById( _observables[ storageId ].domElementId ); - if ( isDefined( domElement ) ) { - _observables[ storageId ].originalObject = domElement.outerHTML; + if ( isDefined( domElement ) ) { + _observables[ storageId ].originalObject = domElement.outerHTML; + } } - } - var cachedObject = _observables[ storageId ].cachedObject, - originalObject = _observables[ storageId ].originalObject, - originalObjectJson = !isDomElement ? JSON.stringify( originalObject ) : originalObject; + var cachedObject = _observables[ storageId ].cachedObject, + originalObject = _observables[ storageId ].originalObject, + originalObjectJson = !isDomElement ? JSON.stringify( originalObject ) : originalObject; - if ( cachedObject !== originalObjectJson ) { - var options = _observables[ storageId ].options; + if ( cachedObject !== originalObjectJson ) { + var options = _observables[ storageId ].options; + + if ( options.reset ) { + if ( isDomElement ) { + domElement.outerHTML = _observables[ storageId ].cachedObject; + } else { + _observables[ storageId ].originalObject = getObjectFromString( cachedObject ).result; + } - if ( options.reset ) { - if ( isDomElement ) { - domElement.outerHTML = _observables[ storageId ].cachedObject; } else { - _observables[ storageId ].originalObject = getObjectFromString( cachedObject ).result; + _observables[ storageId ].cachedObject = originalObjectJson; } - } else { - _observables[ storageId ].cachedObject = originalObjectJson; - } - - if ( isDomElement ) { - fireCustomTrigger( options.onChange, cachedObject, originalObjectJson ); - } else { + if ( isDomElement ) { + fireCustomTrigger( options.onChange, cachedObject, originalObjectJson ); + } else { - var oldValue = getObjectFromString( cachedObject ).result, - newValue = getObjectFromString( originalObjectJson ).result; + var oldValue = getObjectFromString( cachedObject ).result, + newValue = getObjectFromString( originalObjectJson ).result; - fireCustomTrigger( options.onChange, oldValue, newValue ); + fireCustomTrigger( options.onChange, oldValue, newValue ); - if ( isDefinedFunction( options.onPropertyChange ) && !isDefinedArray( oldValue ) ) { - compareObservableObjectProperties( oldValue, newValue, options ); + if ( isDefinedFunction( options.onPropertyChange ) && !isDefinedArray( oldValue ) ) { + compareObservableObjectProperties( oldValue, newValue, options ); + } } - } - if ( options.cancelOnChange ) { - cancelWatchObject( storageId ); - } + if ( options.cancelOnChange ) { + cancelWatchObject( storageId ); + } - _observables[ storageId ].totalChanges++; + _observables[ storageId ].totalChanges++; - if ( options.maximumChangesBeforeCanceling > 0 && _observables[ storageId ].totalChanges >= options.maximumChangesBeforeCanceling ) { - cancelWatchObject( storageId ); + if ( options.maximumChangesBeforeCanceling > 0 && _observables[ storageId ].totalChanges >= options.maximumChangesBeforeCanceling ) { + cancelWatchObject( storageId ); + } } } } From 30b2d0d7ac9b33322947eb1e72745c7c26c23483 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 11:10:45 +0000 Subject: [PATCH 12/20] More documentation fixes. --- README.md | 3 ++- README_NUGET.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4df83b2..83e283f 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,8 @@ Observe.js - Zero-dependencies and extremely lightweight! - JS Object, and HTML DOM Element watching! - Full API available via public functions. -- Full configurable! +- Fully configurable! +- Fully configurable per watch! - Custom triggers for actions (when changes are detected, etc).

diff --git a/README_NUGET.md b/README_NUGET.md index 139a519..9baab97 100644 --- a/README_NUGET.md +++ b/README_NUGET.md @@ -15,7 +15,8 @@ - Zero-dependencies and extremely lightweight! - JS Object, and HTML DOM Element watching! - Full API available via public functions. -- Full configurable! +- Fully configurable! +- Fully configurable per watch! - Custom triggers for actions (when changes are detected, etc). From 6031b0ba36408d942a8f238f1772adfdfc7d8b97 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 12:59:03 +0000 Subject: [PATCH 13/20] Added "Starts" support, which states a date/time a watch should be started. --- dist/observe.js | 23 +++++++++++++---------- dist/observe.min.js | 16 ++++++++-------- docs/CHANGE_LOG.md | 2 ++ docs/binding/options/OPTIONS.md | 1 + src/observe.js | 25 ++++++++++++++----------- 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 3a0bb8c..16510f6 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -27,7 +27,7 @@ if (!isDefinedString(element.id)) { element.id = newGuid(); } - createObservableObject(element, bindingOptions, element.id); + createWatch(element, bindingOptions, element.id); } else { if (!_configuration.safeMode) { console.error("The attribute '" + _attribute_Name_Options + "' is not a valid object."); @@ -43,7 +43,7 @@ } return result; } - function createObservableObject(object, options, domElementId) { + function createWatch(object, options, domElementId) { var storageId = null; if (isDefinedObject(object)) { storageId = newGuid(); @@ -64,15 +64,17 @@ } _observables[storageId].timer = setInterval(function() { var currentDateTime = new Date(); - observeObject(storageId); - if (isDefinedDate(observeOptions.expires) && currentDateTime >= observeOptions.expires) { - cancelWatchObject(storageId); + if (!isDefinedDate(observeOptions.starts) || currentDateTime >= observeOptions.starts) { + watchObjectForChanges(storageId); + if (isDefinedDate(observeOptions.expires) && currentDateTime >= observeOptions.expires) { + cancelWatchObject(storageId); + } } }, observeOptions.observeTimeout); } return storageId; } - function observeObject(storageId) { + function watchObjectForChanges(storageId) { if (_observables.hasOwnProperty(storageId)) { var isDomElement = isDefinedString(_observables[storageId].domElementId); var domElement = null; @@ -103,7 +105,7 @@ var newValue = getObjectFromString(originalObjectJson).result; fireCustomTrigger(options.onChange, oldValue, newValue); if (isDefinedFunction(options.onPropertyChange) && !isDefinedArray(oldValue)) { - compareObservableObjectProperties(oldValue, newValue, options); + compareWatchObjectProperties(oldValue, newValue, options); } } if (options.cancelOnChange) { @@ -116,7 +118,7 @@ } } } - function compareObservableObjectProperties(oldObject, newObject, options) { + function compareWatchObjectProperties(oldObject, newObject, options) { var propertyName; for (propertyName in oldObject) { if (oldObject.hasOwnProperty(propertyName)) { @@ -126,7 +128,7 @@ propertyNewValue = newObject[propertyName]; } if (isDefinedObject(propertyOldValue) && isDefinedObject(propertyNewValue)) { - compareObservableObjectProperties(propertyOldValue, propertyNewValue, options); + compareWatchObjectProperties(propertyOldValue, propertyNewValue, options); } else { if (JSON.stringify(propertyOldValue) !== JSON.stringify(propertyNewValue)) { fireCustomTrigger(options.onPropertyChange, propertyName, propertyOldValue, propertyNewValue); @@ -146,6 +148,7 @@ function getObserveOptions(newOptions) { var options = !isDefinedObject(newOptions) ? {} : newOptions; options.observeTimeout = getDefaultNumber(options.observeTimeout, 250); + options.starts = getDefaultDate(options.starts, null); options.expires = getDefaultDate(options.expires, null); options.reset = getDefaultBoolean(options.reset, false); options.cancelOnChange = getDefaultBoolean(options.cancelOnChange, false); @@ -265,7 +268,7 @@ var _configuration = {}; var _attribute_Name_Options = "data-observe-options"; this.watchObject = function(object, options) { - return createObservableObject(object, options); + return createWatch(object, options); }; this.cancelWatch = function(id) { var result = false; diff --git a/dist/observe.min.js b/dist/observe.min.js index af65804..14045e9 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,9 +1,9 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ -(function(){function E(a,b,c){var e=null;if(q(a)){e=F();var h=G(b);d[e]={};d[e].options=h;d[e].domElementId=c;d[e].totalChanges=0;t(c)?(a=u.getElementById(c),n(a)&&(d[e].cachedObject=a.outerHTML,d[e].originalObject=a.outerHTML)):(d[e].cachedObject=JSON.stringify(a),d[e].originalObject=a);d[e].timer=setInterval(function(){var l=new Date,g=e;if(d.hasOwnProperty(g)){var m=t(d[g].domElementId),k=null;m&&(k=u.getElementById(d[g].domElementId),n(k)&&(d[g].originalObject=k.outerHTML));var r=d[g].cachedObject, -f=d[g].originalObject,w=m?f:JSON.stringify(f);r!==w&&(f=d[g].options,f.reset?m?k.outerHTML=d[g].cachedObject:d[g].originalObject=x(r).result:d[g].cachedObject=w,m?y(f.onChange,r,w):(m=x(r).result,k=x(w).result,y(f.onChange,m,k),z(f.onPropertyChange)&&!H(m)&&I(m,k,f)),f.cancelOnChange&&v(g),d[g].totalChanges++,0=f.maximumChangesBeforeCanceling&&v(g))}J(h.expires)&&l>=h.expires&&v(e)},h.observeTimeout)}return e}function I(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var h= -a[e],l=null;b.hasOwnProperty(e)&&(l=b[e]);q(h)&&q(l)?I(h,l,c):JSON.stringify(h)!==JSON.stringify(l)&&y(c.onPropertyChange,e,h,l)}}function v(a){d.hasOwnProperty(a)&&(y(d[a].options.onCancel,a),clearTimeout(d[a].timer),delete d[a])}function G(a){var b=a=q(a)?a:{};var c=a.observeTimeout;c=K(c)?c:250;b.observeTimeout=c;b=a;c=a.expires;c=J(c)?c:null;b.expires=c;a.reset=A(a.reset,!1);a.cancelOnChange=A(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c=K(c)?c:0;b.maximumChangesBeforeCanceling= -c;a.onChange=B(a.onChange,null);a.onPropertyChange=B(a.onPropertyChange,null);a.onCancel=B(a.onCancel,null);return a}function y(a){z(a)&&a.apply(null,[].slice.call(arguments,1))}function F(){for(var a=[],b=0;32>b;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 n(a){return null!==a&&void 0!==a&&a!==C.empty}function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function z(a){return n(a)&& -"function"===typeof a}function K(a){return n(a)&&"number"===typeof a}function H(a){return q(a)&&a instanceof Array}function J(a){return q(a)&&a instanceof Date}function A(a,b){return n(a)&&"boolean"===typeof a?a:b}function B(a,b){return z(a)?a:b}function x(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),z(c)&&(c=c())}catch(h){b=M("Errors in object: "+e.message+", "+h.message),c=null}}return{parsed:b,result:c}}function M(a){var b=!0;p.safeMode||(console.error(a),b=!1); -return b}function L(){p.safeMode=A(p.safeMode,!0);var a=p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(C.space),0===b.length&&(b=c)):b=H(b)?b:c;a.domElementTypes=b}var u=null,D=null,C={empty:""},d={},p={};this.watchObject=function(a,b){return E(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(v(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){v(c);b=!0;break}return b};this.setConfiguration= -function(a){p=q(a)?a:{};L();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){u=a;D=b;L();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,h=0;h=g.starts){var h=e;if(d.hasOwnProperty(h)){var m=t(d[h].domElementId),k=null;m&&(k=u.getElementById(d[h].domElementId),n(k)&&(d[h].originalObject= + k.outerHTML));var r=d[h].cachedObject,f=d[h].originalObject,x=m?f:JSON.stringify(f);r!==x&&(f=d[h].options,f.reset?m?k.outerHTML=d[h].cachedObject:d[h].originalObject=y(r).result:d[h].cachedObject=x,m?z(f.onChange,r,x):(m=y(r).result,k=y(x).result,z(f.onChange,m,k),A(f.onPropertyChange)&&!I(m)&&J(m,k,f)),f.cancelOnChange&&v(h),d[h].totalChanges++,0=f.maximumChangesBeforeCanceling&&v(h))}w(g.expires)&&l>=g.expires&&v(e)}},g.observeTimeout)}return e} + function J(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var g=a[e],l=null;b.hasOwnProperty(e)&&(l=b[e]);q(g)&&q(l)?J(g,l,c):JSON.stringify(g)!==JSON.stringify(l)&&z(c.onPropertyChange,e,g,l)}}function v(a){d.hasOwnProperty(a)&&(z(d[a].options.onCancel,a),clearTimeout(d[a].timer),delete d[a])}function H(a){var b=a=q(a)?a:{};var c=a.observeTimeout;c=K(c)?c:250;b.observeTimeout=c;b=a;c=a.starts;c=w(c)?c:null;b.starts=c;b=a;c=a.expires;c=w(c)?c:null;b.expires=c;a.reset=B(a.reset,!1);a.cancelOnChange= + B(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c=K(c)?c:0;b.maximumChangesBeforeCanceling=c;a.onChange=C(a.onChange,null);a.onPropertyChange=C(a.onPropertyChange,null);a.onCancel=C(a.onCancel,null);return a}function z(a){A(a)&&a.apply(null,[].slice.call(arguments,1))}function G(){for(var a=[],b=0;32>b;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(D.empty)}function n(a){return null!==a&&void 0!==a&&a!==D.empty} + function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function A(a){return n(a)&&"function"===typeof a}function K(a){return n(a)&&"number"===typeof a}function I(a){return q(a)&&a instanceof Array}function w(a){return q(a)&&a instanceof Date}function B(a,b){return n(a)&&"boolean"===typeof a?a:b}function C(a,b){return A(a)?a:b}function y(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),A(c)&&(c=c())}catch(g){b=M("Errors in object: "+ + e.message+", "+g.message),c=null}}return{parsed:b,result:c}}function M(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function L(){p.safeMode=B(p.safeMode,!0);var a=p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(D.space),0===b.length&&(b=c)):b=I(b)?b:c;a.domElementTypes=b}var u=null,E=null,D={empty:""},d={},p={};this.watchObject=function(a,b){return F(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(v(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&& + t(d[c].domElementId)&&d[c].domElementId===a){v(c);b=!0;break}return b};this.setConfiguration=function(a){p=q(a)?a:{};L();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){u=a;E=b;L();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,g=0;g= observeOptions.starts ) { + watchObjectForChanges( storageId ); - if ( isDefinedDate( observeOptions.expires ) && currentDateTime >= observeOptions.expires ) { - cancelWatchObject( storageId ); + if ( isDefinedDate( observeOptions.expires ) && currentDateTime >= observeOptions.expires ) { + cancelWatchObject( storageId ); + } } }, observeOptions.observeTimeout ); @@ -138,7 +140,7 @@ return storageId; } - function observeObject( storageId ) { + function watchObjectForChanges( storageId ) { if ( _observables.hasOwnProperty( storageId ) ) { var isDomElement = isDefinedString( _observables[ storageId ].domElementId ), domElement = null; @@ -179,7 +181,7 @@ fireCustomTrigger( options.onChange, oldValue, newValue ); if ( isDefinedFunction( options.onPropertyChange ) && !isDefinedArray( oldValue ) ) { - compareObservableObjectProperties( oldValue, newValue, options ); + compareWatchObjectProperties( oldValue, newValue, options ); } } @@ -196,7 +198,7 @@ } } - function compareObservableObjectProperties( oldObject, newObject, options ) { + function compareWatchObjectProperties( oldObject, newObject, options ) { for ( var propertyName in oldObject ) { if ( oldObject.hasOwnProperty( propertyName ) ) { var propertyOldValue = oldObject[ propertyName ], @@ -207,7 +209,7 @@ } if ( isDefinedObject( propertyOldValue ) && isDefinedObject( propertyNewValue ) ) { - compareObservableObjectProperties( propertyOldValue, propertyNewValue, options ); + compareWatchObjectProperties( propertyOldValue, propertyNewValue, options ); } else { if ( JSON.stringify( propertyOldValue ) !== JSON.stringify( propertyNewValue ) ) { @@ -240,6 +242,7 @@ var options = !isDefinedObject( newOptions ) ? {} : newOptions; options.observeTimeout = getDefaultNumber( options.observeTimeout, 250 ); + options.starts = getDefaultDate( options.starts, null ); options.expires = getDefaultDate( options.expires, null ); options.reset = getDefaultBoolean( options.reset, false ); options.cancelOnChange = getDefaultBoolean( options.cancelOnChange, false ); @@ -435,7 +438,7 @@ * @returns {string} The ID that object watch is stored under. */ this.watchObject = function( object, options ) { - return createObservableObject( object, options ); + return createWatch( object, options ); }; /** From 69b46f6c1fc80df8666891b2c2c3d02bca58f12d Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 13:36:15 +0000 Subject: [PATCH 14/20] Updated documentation. --- README.md | 3 ++- README_NUGET.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 83e283f..e5a93b8 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,11 @@ Observe.js - Zero-dependencies and extremely lightweight! - JS Object, and HTML DOM Element watching! +- Cancellation support! - Full API available via public functions. - Fully configurable! - Fully configurable per watch! -- Custom triggers for actions (when changes are detected, etc). +- Custom triggers for actions (when changes are detected, on cancellation, etc).

diff --git a/README_NUGET.md b/README_NUGET.md index 9baab97..2b6b335 100644 --- a/README_NUGET.md +++ b/README_NUGET.md @@ -14,10 +14,11 @@ - Zero-dependencies and extremely lightweight! - JS Object, and HTML DOM Element watching! +- Cancellation support! - Full API available via public functions. - Fully configurable! - Fully configurable per watch! -- Custom triggers for actions (when changes are detected, etc). +- Custom triggers for actions (when changes are detected, on cancellation, etc). ## What browsers are supported? From 43f57f4d003eeeecd62c851ca11ef36964eaefa1 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 13:54:40 +0000 Subject: [PATCH 15/20] Internal refactoring --- dist/observe.js | 42 ++++++++++++++++++++++-------------------- dist/observe.min.js | 16 ++++++++-------- src/observe.js | 45 ++++++++++++++++++++++++--------------------- 3 files changed, 54 insertions(+), 49 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 16510f6..6ecf6ca 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -48,21 +48,21 @@ if (isDefinedObject(object)) { storageId = newGuid(); var observeOptions = getObserveOptions(options); - _observables[storageId] = {}; - _observables[storageId].options = observeOptions; - _observables[storageId].domElementId = domElementId; - _observables[storageId].totalChanges = 0; + var observable = {}; + observable.options = observeOptions; + observable.domElementId = domElementId; + observable.totalChanges = 0; if (isDefinedString(domElementId)) { var domElement = _parameter_Document.getElementById(domElementId); if (isDefined(domElement)) { - _observables[storageId].cachedObject = domElement.outerHTML; - _observables[storageId].originalObject = domElement.outerHTML; + observable.cachedObject = domElement.outerHTML; + observable.originalObject = domElement.outerHTML; } } else { - _observables[storageId].cachedObject = JSON.stringify(object); - _observables[storageId].originalObject = object; + observable.cachedObject = JSON.stringify(object); + observable.originalObject = object; } - _observables[storageId].timer = setInterval(function() { + observable.timer = setInterval(function() { var currentDateTime = new Date(); if (!isDefinedDate(observeOptions.starts) || currentDateTime >= observeOptions.starts) { watchObjectForChanges(storageId); @@ -71,32 +71,34 @@ } } }, observeOptions.observeTimeout); + _observables[storageId] = observable; } return storageId; } function watchObjectForChanges(storageId) { if (_observables.hasOwnProperty(storageId)) { - var isDomElement = isDefinedString(_observables[storageId].domElementId); + var observable = _observables[storageId]; + var isDomElement = isDefinedString(observable.domElementId); var domElement = null; if (isDomElement) { - domElement = _parameter_Document.getElementById(_observables[storageId].domElementId); + domElement = _parameter_Document.getElementById(observable.domElementId); if (isDefined(domElement)) { - _observables[storageId].originalObject = domElement.outerHTML; + observable.originalObject = domElement.outerHTML; } } - var cachedObject = _observables[storageId].cachedObject; - var originalObject = _observables[storageId].originalObject; + var cachedObject = observable.cachedObject; + var originalObject = observable.originalObject; var originalObjectJson = !isDomElement ? JSON.stringify(originalObject) : originalObject; if (cachedObject !== originalObjectJson) { - var options = _observables[storageId].options; + var options = observable.options; if (options.reset) { if (isDomElement) { - domElement.outerHTML = _observables[storageId].cachedObject; + domElement.outerHTML = observable.cachedObject; } else { - _observables[storageId].originalObject = getObjectFromString(cachedObject).result; + observable.originalObject = getObjectFromString(cachedObject).result; } } else { - _observables[storageId].cachedObject = originalObjectJson; + observable.cachedObject = originalObjectJson; } if (isDomElement) { fireCustomTrigger(options.onChange, cachedObject, originalObjectJson); @@ -111,8 +113,8 @@ if (options.cancelOnChange) { cancelWatchObject(storageId); } - _observables[storageId].totalChanges++; - if (options.maximumChangesBeforeCanceling > 0 && _observables[storageId].totalChanges >= options.maximumChangesBeforeCanceling) { + observable.totalChanges++; + if (options.maximumChangesBeforeCanceling > 0 && observable.totalChanges >= options.maximumChangesBeforeCanceling) { cancelWatchObject(storageId); } } diff --git a/dist/observe.min.js b/dist/observe.min.js index 14045e9..42336a2 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,9 +1,9 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ -(function(){function F(a,b,c){var e=null;if(q(a)){e=G();var g=H(b);d[e]={};d[e].options=g;d[e].domElementId=c;d[e].totalChanges=0;t(c)?(a=u.getElementById(c),n(a)&&(d[e].cachedObject=a.outerHTML,d[e].originalObject=a.outerHTML)):(d[e].cachedObject=JSON.stringify(a),d[e].originalObject=a);d[e].timer=setInterval(function(){var l=new Date;if(!w(g.starts)||l>=g.starts){var h=e;if(d.hasOwnProperty(h)){var m=t(d[h].domElementId),k=null;m&&(k=u.getElementById(d[h].domElementId),n(k)&&(d[h].originalObject= - k.outerHTML));var r=d[h].cachedObject,f=d[h].originalObject,x=m?f:JSON.stringify(f);r!==x&&(f=d[h].options,f.reset?m?k.outerHTML=d[h].cachedObject:d[h].originalObject=y(r).result:d[h].cachedObject=x,m?z(f.onChange,r,x):(m=y(r).result,k=y(x).result,z(f.onChange,m,k),A(f.onPropertyChange)&&!I(m)&&J(m,k,f)),f.cancelOnChange&&v(h),d[h].totalChanges++,0=f.maximumChangesBeforeCanceling&&v(h))}w(g.expires)&&l>=g.expires&&v(e)}},g.observeTimeout)}return e} - function J(a,b,c){for(var e in a)if(a.hasOwnProperty(e)){var g=a[e],l=null;b.hasOwnProperty(e)&&(l=b[e]);q(g)&&q(l)?J(g,l,c):JSON.stringify(g)!==JSON.stringify(l)&&z(c.onPropertyChange,e,g,l)}}function v(a){d.hasOwnProperty(a)&&(z(d[a].options.onCancel,a),clearTimeout(d[a].timer),delete d[a])}function H(a){var b=a=q(a)?a:{};var c=a.observeTimeout;c=K(c)?c:250;b.observeTimeout=c;b=a;c=a.starts;c=w(c)?c:null;b.starts=c;b=a;c=a.expires;c=w(c)?c:null;b.expires=c;a.reset=B(a.reset,!1);a.cancelOnChange= - B(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c=K(c)?c:0;b.maximumChangesBeforeCanceling=c;a.onChange=C(a.onChange,null);a.onPropertyChange=C(a.onPropertyChange,null);a.onCancel=C(a.onCancel,null);return a}function z(a){A(a)&&a.apply(null,[].slice.call(arguments,1))}function G(){for(var a=[],b=0;32>b;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(D.empty)}function n(a){return null!==a&&void 0!==a&&a!==D.empty} - function q(a){return n(a)&&"object"===typeof a}function t(a){return n(a)&&"string"===typeof a}function A(a){return n(a)&&"function"===typeof a}function K(a){return n(a)&&"number"===typeof a}function I(a){return q(a)&&a instanceof Array}function w(a){return q(a)&&a instanceof Date}function B(a,b){return n(a)&&"boolean"===typeof a?a:b}function C(a,b){return A(a)?a:b}function y(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(e){try{c=eval("("+a+")"),A(c)&&(c=c())}catch(g){b=M("Errors in object: "+ - e.message+", "+g.message),c=null}}return{parsed:b,result:c}}function M(a){var b=!0;p.safeMode||(console.error(a),b=!1);return b}function L(){p.safeMode=B(p.safeMode,!0);var a=p,b=p.domElementTypes,c=["*"];t(b)?(b=b.split(D.space),0===b.length&&(b=c)):b=I(b)?b:c;a.domElementTypes=b}var u=null,E=null,D={empty:""},d={},p={};this.watchObject=function(a,b){return F(a,b)};this.cancelWatch=function(a){var b=!1;d.hasOwnProperty(a)&&(v(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in d)if(d.hasOwnProperty(c)&& - t(d[c].domElementId)&&d[c].domElementId===a){v(c);b=!0;break}return b};this.setConfiguration=function(a){p=q(a)?a:{};L();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){u=a;E=b;L();u.addEventListener("DOMContentLoaded",function(){for(var c=p.domElementTypes,e=c.length,g=0;g=d.starts){var u=f;if(l.hasOwnProperty(u)){var g=l[u],e=t(g.domElementId),m=null;e&&(m=v.getElementById(g.domElementId),p(m)&&(g.originalObject=m.outerHTML));var k=g.cachedObject, +n=g.originalObject,y=e?n:JSON.stringify(n);k!==y&&(n=g.options,n.reset?e?m.outerHTML=g.cachedObject:g.originalObject=z(k).result:g.cachedObject=y,e?A(n.onChange,k,y):(e=z(k).result,m=z(y).result,A(n.onChange,e,m),B(n.onPropertyChange)&&!J(e)&&K(e,m,n)),n.cancelOnChange&&w(u),g.totalChanges++,0=n.maximumChangesBeforeCanceling&&w(u))}x(d.expires)&&h>=d.expires&&w(f)}},d.observeTimeout);l[f]=b}return f}function K(a,b,c){for(var f in a)if(a.hasOwnProperty(f)){var d= +a[f],h=null;b.hasOwnProperty(f)&&(h=b[f]);r(d)&&r(h)?K(d,h,c):JSON.stringify(d)!==JSON.stringify(h)&&A(c.onPropertyChange,f,d,h)}}function w(a){l.hasOwnProperty(a)&&(A(l[a].options.onCancel,a),clearTimeout(l[a].timer),delete l[a])}function I(a){var b=a=r(a)?a:{};var c=a.observeTimeout;c=L(c)?c:250;b.observeTimeout=c;b=a;c=a.starts;c=x(c)?c:null;b.starts=c;b=a;c=a.expires;c=x(c)?c:null;b.expires=c;a.reset=C(a.reset,!1);a.cancelOnChange=C(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c= +L(c)?c:0;b.maximumChangesBeforeCanceling=c;a.onChange=D(a.onChange,null);a.onPropertyChange=D(a.onPropertyChange,null);a.onCancel=D(a.onCancel,null);return a}function A(a){B(a)&&a.apply(null,[].slice.call(arguments,1))}function H(){for(var a=[],b=0;32>b;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(E.empty)}function p(a){return null!==a&&void 0!==a&&a!==E.empty}function r(a){return p(a)&&"object"===typeof a}function t(a){return p(a)&& +"string"===typeof a}function B(a){return p(a)&&"function"===typeof a}function L(a){return p(a)&&"number"===typeof a}function J(a){return r(a)&&a instanceof Array}function x(a){return r(a)&&a instanceof Date}function C(a,b){return p(a)&&"boolean"===typeof a?a:b}function D(a,b){return B(a)?a:b}function z(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(f){try{c=eval("("+a+")"),B(c)&&(c=c())}catch(d){b=N("Errors in object: "+f.message+", "+d.message),c=null}}return{parsed:b,result:c}}function N(a){var b= +!0;q.safeMode||(console.error(a),b=!1);return b}function M(){q.safeMode=C(q.safeMode,!0);var a=q,b=q.domElementTypes,c=["*"];t(b)?(b=b.split(E.space),0===b.length&&(b=c)):b=J(b)?b:c;a.domElementTypes=b}var v=null,F=null,E={empty:""},l={},q={};this.watchObject=function(a,b){return G(a,b)};this.cancelWatch=function(a){var b=!1;l.hasOwnProperty(a)&&(w(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in l)if(l.hasOwnProperty(c)&&t(l[c].domElementId)&&l[c].domElementId===a){w(c); +b=!0;break}return b};this.setConfiguration=function(a){q=r(a)?a:{};M();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){v=a;F=b;M();v.addEventListener("DOMContentLoaded",function(){for(var c=q.domElementTypes,f=c.length,d=0;d= observeOptions.starts ) { @@ -135,6 +135,8 @@ } }, observeOptions.observeTimeout ); + + _observables[ storageId ] = observable; } return storageId; @@ -142,33 +144,34 @@ function watchObjectForChanges( storageId ) { if ( _observables.hasOwnProperty( storageId ) ) { - var isDomElement = isDefinedString( _observables[ storageId ].domElementId ), + var observable = _observables[ storageId ], + isDomElement = isDefinedString( observable.domElementId ), domElement = null; if ( isDomElement ) { - domElement = _parameter_Document.getElementById( _observables[ storageId ].domElementId ); + domElement = _parameter_Document.getElementById( observable.domElementId ); if ( isDefined( domElement ) ) { - _observables[ storageId ].originalObject = domElement.outerHTML; + observable.originalObject = domElement.outerHTML; } } - var cachedObject = _observables[ storageId ].cachedObject, - originalObject = _observables[ storageId ].originalObject, + var cachedObject = observable.cachedObject, + originalObject = observable.originalObject, originalObjectJson = !isDomElement ? JSON.stringify( originalObject ) : originalObject; if ( cachedObject !== originalObjectJson ) { - var options = _observables[ storageId ].options; + var options = observable.options; if ( options.reset ) { if ( isDomElement ) { - domElement.outerHTML = _observables[ storageId ].cachedObject; + domElement.outerHTML = observable.cachedObject; } else { - _observables[ storageId ].originalObject = getObjectFromString( cachedObject ).result; + observable.originalObject = getObjectFromString( cachedObject ).result; } } else { - _observables[ storageId ].cachedObject = originalObjectJson; + observable.cachedObject = originalObjectJson; } if ( isDomElement ) { @@ -189,9 +192,9 @@ cancelWatchObject( storageId ); } - _observables[ storageId ].totalChanges++; + observable.totalChanges++; - if ( options.maximumChangesBeforeCanceling > 0 && _observables[ storageId ].totalChanges >= options.maximumChangesBeforeCanceling ) { + if ( options.maximumChangesBeforeCanceling > 0 && observable.totalChanges >= options.maximumChangesBeforeCanceling ) { cancelWatchObject( storageId ); } } From e0d7a36ef22d2ac4a6215e3f80eb880a8c601ea6 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 13:59:36 +0000 Subject: [PATCH 16/20] Refactored lots of the internal code to align with the naming of the areas. --- dist/observe.js | 92 ++++++++++++++++++++--------------------- docs/CHANGE_LOG.md | 3 ++ src/observe.js | 100 ++++++++++++++++++++++----------------------- 3 files changed, 99 insertions(+), 96 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 6ecf6ca..8571018 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -23,7 +23,7 @@ if (isDefinedString(bindingOptionsData)) { var bindingOptions = getObjectFromString(bindingOptionsData); if (bindingOptions.parsed && isDefinedObject(bindingOptions.result)) { - bindingOptions = getObserveOptions(bindingOptions.result); + bindingOptions = getWatchOptions(bindingOptions.result); if (!isDefinedString(element.id)) { element.id = newGuid(); } @@ -47,74 +47,74 @@ var storageId = null; if (isDefinedObject(object)) { storageId = newGuid(); - var observeOptions = getObserveOptions(options); - var observable = {}; - observable.options = observeOptions; - observable.domElementId = domElementId; - observable.totalChanges = 0; + var watchOptions = getWatchOptions(options); + var watch = {}; + watch.options = watchOptions; + watch.domElementId = domElementId; + watch.totalChanges = 0; if (isDefinedString(domElementId)) { var domElement = _parameter_Document.getElementById(domElementId); if (isDefined(domElement)) { - observable.cachedObject = domElement.outerHTML; - observable.originalObject = domElement.outerHTML; + watch.cachedObject = domElement.outerHTML; + watch.originalObject = domElement.outerHTML; } } else { - observable.cachedObject = JSON.stringify(object); - observable.originalObject = object; + watch.cachedObject = JSON.stringify(object); + watch.originalObject = object; } - observable.timer = setInterval(function() { + watch.timer = setInterval(function() { var currentDateTime = new Date(); - if (!isDefinedDate(observeOptions.starts) || currentDateTime >= observeOptions.starts) { + if (!isDefinedDate(watchOptions.starts) || currentDateTime >= watchOptions.starts) { watchObjectForChanges(storageId); - if (isDefinedDate(observeOptions.expires) && currentDateTime >= observeOptions.expires) { + if (isDefinedDate(watchOptions.expires) && currentDateTime >= watchOptions.expires) { cancelWatchObject(storageId); } } - }, observeOptions.observeTimeout); - _observables[storageId] = observable; + }, watchOptions.observeTimeout); + _watches[storageId] = watch; } return storageId; } function watchObjectForChanges(storageId) { - if (_observables.hasOwnProperty(storageId)) { - var observable = _observables[storageId]; - var isDomElement = isDefinedString(observable.domElementId); + if (_watches.hasOwnProperty(storageId)) { + var watch = _watches[storageId]; + var isDomElement = isDefinedString(watch.domElementId); var domElement = null; if (isDomElement) { - domElement = _parameter_Document.getElementById(observable.domElementId); + domElement = _parameter_Document.getElementById(watch.domElementId); if (isDefined(domElement)) { - observable.originalObject = domElement.outerHTML; + watch.originalObject = domElement.outerHTML; } } - var cachedObject = observable.cachedObject; - var originalObject = observable.originalObject; + var cachedObject = watch.cachedObject; + var originalObject = watch.originalObject; var originalObjectJson = !isDomElement ? JSON.stringify(originalObject) : originalObject; if (cachedObject !== originalObjectJson) { - var options = observable.options; - if (options.reset) { + var watchOptions = watch.options; + if (watchOptions.reset) { if (isDomElement) { - domElement.outerHTML = observable.cachedObject; + domElement.outerHTML = watch.cachedObject; } else { - observable.originalObject = getObjectFromString(cachedObject).result; + watch.originalObject = getObjectFromString(cachedObject).result; } } else { - observable.cachedObject = originalObjectJson; + watch.cachedObject = originalObjectJson; } if (isDomElement) { - fireCustomTrigger(options.onChange, cachedObject, originalObjectJson); + fireCustomTrigger(watchOptions.onChange, cachedObject, originalObjectJson); } else { var oldValue = getObjectFromString(cachedObject).result; var newValue = getObjectFromString(originalObjectJson).result; - fireCustomTrigger(options.onChange, oldValue, newValue); - if (isDefinedFunction(options.onPropertyChange) && !isDefinedArray(oldValue)) { - compareWatchObjectProperties(oldValue, newValue, options); + fireCustomTrigger(watchOptions.onChange, oldValue, newValue); + if (isDefinedFunction(watchOptions.onPropertyChange) && !isDefinedArray(oldValue)) { + compareWatchObjectProperties(oldValue, newValue, watchOptions); } } - if (options.cancelOnChange) { + if (watchOptions.cancelOnChange) { cancelWatchObject(storageId); } - observable.totalChanges++; - if (options.maximumChangesBeforeCanceling > 0 && observable.totalChanges >= options.maximumChangesBeforeCanceling) { + watch.totalChanges++; + if (watchOptions.maximumChangesBeforeCanceling > 0 && watch.totalChanges >= watchOptions.maximumChangesBeforeCanceling) { cancelWatchObject(storageId); } } @@ -140,14 +140,14 @@ } } function cancelWatchObject(storageId) { - if (_observables.hasOwnProperty(storageId)) { - var options = _observables[storageId].options; - fireCustomTrigger(options.onCancel, storageId); - clearTimeout(_observables[storageId].timer); - delete _observables[storageId]; + if (_watches.hasOwnProperty(storageId)) { + var watchOptions = _watches[storageId].options; + fireCustomTrigger(watchOptions.onCancel, storageId); + clearTimeout(_watches[storageId].timer); + delete _watches[storageId]; } } - function getObserveOptions(newOptions) { + function getWatchOptions(newOptions) { var options = !isDefinedObject(newOptions) ? {} : newOptions; options.observeTimeout = getDefaultNumber(options.observeTimeout, 250); options.starts = getDefaultDate(options.starts, null); @@ -155,10 +155,10 @@ options.reset = getDefaultBoolean(options.reset, false); options.cancelOnChange = getDefaultBoolean(options.cancelOnChange, false); options.maximumChangesBeforeCanceling = getDefaultNumber(options.maximumChangesBeforeCanceling, 0); - options = getObserveOptionsCustomTriggers(options); + options = getWatchOptionsCustomTriggers(options); return options; } - function getObserveOptionsCustomTriggers(options) { + function getWatchOptionsCustomTriggers(options) { options.onChange = getDefaultFunction(options.onChange, null); options.onPropertyChange = getDefaultFunction(options.onPropertyChange, null); options.onCancel = getDefaultFunction(options.onCancel, null); @@ -266,7 +266,7 @@ var _parameter_Document = null; var _parameter_Window = null; var _string = {empty:""}; - var _observables = {}; + var _watches = {}; var _configuration = {}; var _attribute_Name_Options = "data-observe-options"; this.watchObject = function(object, options) { @@ -274,7 +274,7 @@ }; this.cancelWatch = function(id) { var result = false; - if (_observables.hasOwnProperty(id)) { + if (_watches.hasOwnProperty(id)) { cancelWatchObject(id); result = true; } @@ -283,8 +283,8 @@ this.cancelDomElementWatch = function(elementId) { var result = false; var storageId; - for (storageId in _observables) { - if (_observables.hasOwnProperty(storageId) && isDefinedString(_observables[storageId].domElementId) && _observables[storageId].domElementId === elementId) { + for (storageId in _watches) { + if (_watches.hasOwnProperty(storageId) && isDefinedString(_watches[storageId].domElementId) && _watches[storageId].domElementId === elementId) { cancelWatchObject(storageId); result = true; break; diff --git a/docs/CHANGE_LOG.md b/docs/CHANGE_LOG.md index 93a1962..8981ab1 100644 --- a/docs/CHANGE_LOG.md +++ b/docs/CHANGE_LOG.md @@ -20,6 +20,9 @@ #### **Watching:** - Changes are now detected for HTML DOM element objects when attributes are updated. +#### **General Improvements:** +- Refactored lots of the internal code to align with the naming of the areas. + #### **Documentation:** - Fixed some of the documentation referencing the wrong properties. diff --git a/src/observe.js b/src/observe.js index 5f58d47..415ff34 100644 --- a/src/observe.js +++ b/src/observe.js @@ -21,8 +21,8 @@ empty: "" }, - // Variables: Observables - _observables = {}, + // Variables: Watches + _watches = {}, // Variables: Configuration _configuration = {}, @@ -64,7 +64,7 @@ var bindingOptions = getObjectFromString( bindingOptionsData ); if ( bindingOptions.parsed && isDefinedObject( bindingOptions.result ) ) { - bindingOptions = getObserveOptions( bindingOptions.result ); + bindingOptions = getWatchOptions( bindingOptions.result ); if ( !isDefinedString( element.id ) ) { element.id = newGuid(); @@ -93,7 +93,7 @@ /* * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - * Observable Watch Object Creation / Handling + * Watch Object Creation / Handling * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */ @@ -103,98 +103,98 @@ if ( isDefinedObject( object ) ) { storageId = newGuid(); - var observeOptions = getObserveOptions( options ), - observable = {}; + var watchOptions = getWatchOptions( options ), + watch = {}; - observable.options = observeOptions; - observable.domElementId = domElementId; - observable.totalChanges = 0; + watch.options = watchOptions; + watch.domElementId = domElementId; + watch.totalChanges = 0; if ( isDefinedString( domElementId ) ) { var domElement = _parameter_Document.getElementById( domElementId ); if ( isDefined( domElement ) ) { - observable.cachedObject = domElement.outerHTML; - observable.originalObject = domElement.outerHTML; + watch.cachedObject = domElement.outerHTML; + watch.originalObject = domElement.outerHTML; } } else { - observable.cachedObject = JSON.stringify( object ); - observable.originalObject = object; + watch.cachedObject = JSON.stringify( object ); + watch.originalObject = object; } - observable.timer = setInterval( function() { + watch.timer = setInterval( function() { var currentDateTime = new Date(); - if ( !isDefinedDate( observeOptions.starts ) || currentDateTime >= observeOptions.starts ) { + if ( !isDefinedDate( watchOptions.starts ) || currentDateTime >= watchOptions.starts ) { watchObjectForChanges( storageId ); - if ( isDefinedDate( observeOptions.expires ) && currentDateTime >= observeOptions.expires ) { + if ( isDefinedDate( watchOptions.expires ) && currentDateTime >= watchOptions.expires ) { cancelWatchObject( storageId ); } } - }, observeOptions.observeTimeout ); + }, watchOptions.observeTimeout ); - _observables[ storageId ] = observable; + _watches[ storageId ] = watch; } return storageId; } function watchObjectForChanges( storageId ) { - if ( _observables.hasOwnProperty( storageId ) ) { - var observable = _observables[ storageId ], - isDomElement = isDefinedString( observable.domElementId ), + if ( _watches.hasOwnProperty( storageId ) ) { + var watch = _watches[ storageId ], + isDomElement = isDefinedString( watch.domElementId ), domElement = null; if ( isDomElement ) { - domElement = _parameter_Document.getElementById( observable.domElementId ); + domElement = _parameter_Document.getElementById( watch.domElementId ); if ( isDefined( domElement ) ) { - observable.originalObject = domElement.outerHTML; + watch.originalObject = domElement.outerHTML; } } - var cachedObject = observable.cachedObject, - originalObject = observable.originalObject, + var cachedObject = watch.cachedObject, + originalObject = watch.originalObject, originalObjectJson = !isDomElement ? JSON.stringify( originalObject ) : originalObject; if ( cachedObject !== originalObjectJson ) { - var options = observable.options; + var watchOptions = watch.options; - if ( options.reset ) { + if ( watchOptions.reset ) { if ( isDomElement ) { - domElement.outerHTML = observable.cachedObject; + domElement.outerHTML = watch.cachedObject; } else { - observable.originalObject = getObjectFromString( cachedObject ).result; + watch.originalObject = getObjectFromString( cachedObject ).result; } } else { - observable.cachedObject = originalObjectJson; + watch.cachedObject = originalObjectJson; } if ( isDomElement ) { - fireCustomTrigger( options.onChange, cachedObject, originalObjectJson ); + fireCustomTrigger( watchOptions.onChange, cachedObject, originalObjectJson ); } else { var oldValue = getObjectFromString( cachedObject ).result, newValue = getObjectFromString( originalObjectJson ).result; - fireCustomTrigger( options.onChange, oldValue, newValue ); + fireCustomTrigger( watchOptions.onChange, oldValue, newValue ); - if ( isDefinedFunction( options.onPropertyChange ) && !isDefinedArray( oldValue ) ) { - compareWatchObjectProperties( oldValue, newValue, options ); + if ( isDefinedFunction( watchOptions.onPropertyChange ) && !isDefinedArray( oldValue ) ) { + compareWatchObjectProperties( oldValue, newValue, watchOptions ); } } - if ( options.cancelOnChange ) { + if ( watchOptions.cancelOnChange ) { cancelWatchObject( storageId ); } - observable.totalChanges++; + watch.totalChanges++; - if ( options.maximumChangesBeforeCanceling > 0 && observable.totalChanges >= options.maximumChangesBeforeCanceling ) { + if ( watchOptions.maximumChangesBeforeCanceling > 0 && watch.totalChanges >= watchOptions.maximumChangesBeforeCanceling ) { cancelWatchObject( storageId ); } } @@ -224,24 +224,24 @@ } function cancelWatchObject( storageId ) { - if ( _observables.hasOwnProperty( storageId ) ) { - var options = _observables[ storageId ].options; + if ( _watches.hasOwnProperty( storageId ) ) { + var watchOptions = _watches[ storageId ].options; - fireCustomTrigger( options.onCancel, storageId ); + fireCustomTrigger( watchOptions.onCancel, storageId ); - clearTimeout( _observables[ storageId ].timer ); - delete _observables[ storageId ]; + clearTimeout( _watches[ storageId ].timer ); + delete _watches[ storageId ]; } } /* * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - * Observe Options + * Watch Options * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */ - function getObserveOptions( newOptions ) { + function getWatchOptions( newOptions ) { var options = !isDefinedObject( newOptions ) ? {} : newOptions; options.observeTimeout = getDefaultNumber( options.observeTimeout, 250 ); @@ -251,12 +251,12 @@ options.cancelOnChange = getDefaultBoolean( options.cancelOnChange, false ); options.maximumChangesBeforeCanceling = getDefaultNumber( options.maximumChangesBeforeCanceling, 0 ); - options = getObserveOptionsCustomTriggers( options ); + options = getWatchOptionsCustomTriggers( options ); return options; } - function getObserveOptionsCustomTriggers( options ) { + function getWatchOptionsCustomTriggers( options ) { options.onChange = getDefaultFunction( options.onChange, null ); options.onPropertyChange = getDefaultFunction( options.onPropertyChange, null ); options.onCancel = getDefaultFunction( options.onCancel, null ); @@ -424,7 +424,7 @@ /* * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - * Public Functions: Observables + * Public Functions: Watching Objects * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- */ @@ -458,7 +458,7 @@ this.cancelWatch = function( id ) { var result = false; - if ( _observables.hasOwnProperty( id ) ) { + if ( _watches.hasOwnProperty( id ) ) { cancelWatchObject( id ); result = true; @@ -481,8 +481,8 @@ this.cancelDomElementWatch = function( elementId ) { var result = false; - for ( var storageId in _observables ) { - if ( _observables.hasOwnProperty( storageId ) && isDefinedString( _observables[ storageId ].domElementId ) && _observables[ storageId ].domElementId === elementId ) { + for ( var storageId in _watches ) { + if ( _watches.hasOwnProperty( storageId ) && isDefinedString( _watches[ storageId ].domElementId ) && _watches[ storageId ].domElementId === elementId ) { cancelWatchObject( storageId ); result = true; From 816a8aab8f5756f0dcdddfaddd14197a86f97f93 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 14:08:17 +0000 Subject: [PATCH 17/20] Added new public function "getWatch()", which is used to return all the watch properties for an object being watched. --- dist/observe.js | 7 +++++++ dist/observe.min.js | 12 ++++++------ docs/CHANGE_LOG.md | 3 +++ docs/PUBLIC_FUNCTIONS.md | 8 ++++++++ src/observe.js | 21 +++++++++++++++++++++ test/dist/observe.js.basic.html | 4 ++++ test/dist/observe.js.min.html | 4 ++++ test/src/observe.js.basic.html | 4 ++++ 8 files changed, 57 insertions(+), 6 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 8571018..99bf2fa 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -292,6 +292,13 @@ } return result; }; + this.getWatch = function(id) { + var result = null; + if (_watches.hasOwnProperty(id)) { + result = _watches[id]; + } + return result; + }; this.setConfiguration = function(newOptions) { _configuration = !isDefinedObject(newOptions) ? {} : newOptions; buildDefaultConfiguration(); diff --git a/dist/observe.min.js b/dist/observe.min.js index 42336a2..e38bac4 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,9 +1,9 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ -(function(){function G(a,b,c){var f=null;if(r(a)){f=H();var d=I(b);b={};b.options=d;b.domElementId=c;b.totalChanges=0;t(c)?(a=v.getElementById(c),p(a)&&(b.cachedObject=a.outerHTML,b.originalObject=a.outerHTML)):(b.cachedObject=JSON.stringify(a),b.originalObject=a);b.timer=setInterval(function(){var h=new Date;if(!x(d.starts)||h>=d.starts){var u=f;if(l.hasOwnProperty(u)){var g=l[u],e=t(g.domElementId),m=null;e&&(m=v.getElementById(g.domElementId),p(m)&&(g.originalObject=m.outerHTML));var k=g.cachedObject, -n=g.originalObject,y=e?n:JSON.stringify(n);k!==y&&(n=g.options,n.reset?e?m.outerHTML=g.cachedObject:g.originalObject=z(k).result:g.cachedObject=y,e?A(n.onChange,k,y):(e=z(k).result,m=z(y).result,A(n.onChange,e,m),B(n.onPropertyChange)&&!J(e)&&K(e,m,n)),n.cancelOnChange&&w(u),g.totalChanges++,0=n.maximumChangesBeforeCanceling&&w(u))}x(d.expires)&&h>=d.expires&&w(f)}},d.observeTimeout);l[f]=b}return f}function K(a,b,c){for(var f in a)if(a.hasOwnProperty(f)){var d= -a[f],h=null;b.hasOwnProperty(f)&&(h=b[f]);r(d)&&r(h)?K(d,h,c):JSON.stringify(d)!==JSON.stringify(h)&&A(c.onPropertyChange,f,d,h)}}function w(a){l.hasOwnProperty(a)&&(A(l[a].options.onCancel,a),clearTimeout(l[a].timer),delete l[a])}function I(a){var b=a=r(a)?a:{};var c=a.observeTimeout;c=L(c)?c:250;b.observeTimeout=c;b=a;c=a.starts;c=x(c)?c:null;b.starts=c;b=a;c=a.expires;c=x(c)?c:null;b.expires=c;a.reset=C(a.reset,!1);a.cancelOnChange=C(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c= +(function(){function G(a,b,c){var f=null;if(r(a)){f=H();var d=I(b);b={};b.options=d;b.domElementId=c;b.totalChanges=0;t(c)?(a=v.getElementById(c),p(a)&&(b.cachedObject=a.outerHTML,b.originalObject=a.outerHTML)):(b.cachedObject=JSON.stringify(a),b.originalObject=a);b.timer=setInterval(function(){var k=new Date;if(!x(d.starts)||k>=d.starts){var u=f;if(h.hasOwnProperty(u)){var g=h[u],e=t(g.domElementId),m=null;e&&(m=v.getElementById(g.domElementId),p(m)&&(g.originalObject=m.outerHTML));var l=g.cachedObject, +n=g.originalObject,y=e?n:JSON.stringify(n);l!==y&&(n=g.options,n.reset?e?m.outerHTML=g.cachedObject:g.originalObject=z(l).result:g.cachedObject=y,e?A(n.onChange,l,y):(e=z(l).result,m=z(y).result,A(n.onChange,e,m),B(n.onPropertyChange)&&!J(e)&&K(e,m,n)),n.cancelOnChange&&w(u),g.totalChanges++,0=n.maximumChangesBeforeCanceling&&w(u))}x(d.expires)&&k>=d.expires&&w(f)}},d.observeTimeout);h[f]=b}return f}function K(a,b,c){for(var f in a)if(a.hasOwnProperty(f)){var d= +a[f],k=null;b.hasOwnProperty(f)&&(k=b[f]);r(d)&&r(k)?K(d,k,c):JSON.stringify(d)!==JSON.stringify(k)&&A(c.onPropertyChange,f,d,k)}}function w(a){h.hasOwnProperty(a)&&(A(h[a].options.onCancel,a),clearTimeout(h[a].timer),delete h[a])}function I(a){var b=a=r(a)?a:{};var c=a.observeTimeout;c=L(c)?c:250;b.observeTimeout=c;b=a;c=a.starts;c=x(c)?c:null;b.starts=c;b=a;c=a.expires;c=x(c)?c:null;b.expires=c;a.reset=C(a.reset,!1);a.cancelOnChange=C(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c= L(c)?c:0;b.maximumChangesBeforeCanceling=c;a.onChange=D(a.onChange,null);a.onPropertyChange=D(a.onPropertyChange,null);a.onCancel=D(a.onCancel,null);return a}function A(a){B(a)&&a.apply(null,[].slice.call(arguments,1))}function H(){for(var a=[],b=0;32>b;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(E.empty)}function p(a){return null!==a&&void 0!==a&&a!==E.empty}function r(a){return p(a)&&"object"===typeof a}function t(a){return p(a)&& "string"===typeof a}function B(a){return p(a)&&"function"===typeof a}function L(a){return p(a)&&"number"===typeof a}function J(a){return r(a)&&a instanceof Array}function x(a){return r(a)&&a instanceof Date}function C(a,b){return p(a)&&"boolean"===typeof a?a:b}function D(a,b){return B(a)?a:b}function z(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(f){try{c=eval("("+a+")"),B(c)&&(c=c())}catch(d){b=N("Errors in object: "+f.message+", "+d.message),c=null}}return{parsed:b,result:c}}function N(a){var b= -!0;q.safeMode||(console.error(a),b=!1);return b}function M(){q.safeMode=C(q.safeMode,!0);var a=q,b=q.domElementTypes,c=["*"];t(b)?(b=b.split(E.space),0===b.length&&(b=c)):b=J(b)?b:c;a.domElementTypes=b}var v=null,F=null,E={empty:""},l={},q={};this.watchObject=function(a,b){return G(a,b)};this.cancelWatch=function(a){var b=!1;l.hasOwnProperty(a)&&(w(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in l)if(l.hasOwnProperty(c)&&t(l[c].domElementId)&&l[c].domElementId===a){w(c); -b=!0;break}return b};this.setConfiguration=function(a){q=r(a)?a:{};M();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){v=a;F=b;M();v.addEventListener("DOMContentLoaded",function(){for(var c=q.domElementTypes,f=c.length,d=0;d ***Returns***: '*boolean*' - States if the DOM element object being watched has been canceled.
+ +### **getWatch( *id* )**: +Returns the properties for an active watch. +
+***Parameter: id***: '*string*' - The Id of the object being watched. +
+***Returns***: '*Object*' - The watch properties for an object (null if not found). +

diff --git a/src/observe.js b/src/observe.js index 415ff34..d625c14 100644 --- a/src/observe.js +++ b/src/observe.js @@ -493,6 +493,27 @@ return result; }; + /** + * getWatch(). + * + * Returns the properties for an active watch. + * + * @public + * + * @param {string} id The Id of the object being watched. + * + * @returns {Object} The watch properties for an object (null if not found). + */ + this.getWatch = function( id ) { + var result = null; + + if ( _watches.hasOwnProperty( id ) ) { + result = _watches[ id ]; + } + + return result; + }; + /* * ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/test/dist/observe.js.basic.html b/test/dist/observe.js.basic.html index f9677e4..f6db4de 100644 --- a/test/dist/observe.js.basic.html +++ b/test/dist/observe.js.basic.html @@ -40,6 +40,10 @@

Change Object Values:


+

Get Watch Object:

+ +
+

Configuration:


diff --git a/test/dist/observe.js.min.html b/test/dist/observe.js.min.html index dd6c8ba..834e3c6 100644 --- a/test/dist/observe.js.min.html +++ b/test/dist/observe.js.min.html @@ -40,6 +40,10 @@

Change Object Values:


+

Get Watch Object:

+ +
+

Configuration:


diff --git a/test/src/observe.js.basic.html b/test/src/observe.js.basic.html index 587ddc4..cd75648 100644 --- a/test/src/observe.js.basic.html +++ b/test/src/observe.js.basic.html @@ -40,6 +40,10 @@

Change Object Values:


+

Get Watch Object:

+ +
+

Configuration:


From 7146ba980d16c3fcacd33178bcf0a2f5f1363c17 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 14:17:47 +0000 Subject: [PATCH 18/20] Removed the public function "cancelDomElementWatch()", as "cancelWatch()" will now do it for you. --- dist/observe.js | 27 +++++++++++--------- dist/observe.min.js | 14 +++++------ docs/CHANGE_LOG.md | 3 ++- docs/PUBLIC_FUNCTIONS.md | 12 ++------- src/observe.js | 44 ++++++++++++++------------------- test/dist/observe.js.basic.html | 3 ++- test/dist/observe.js.min.html | 3 ++- test/src/observe.js.basic.html | 3 ++- 8 files changed, 51 insertions(+), 58 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 99bf2fa..090756c 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -277,17 +277,14 @@ if (_watches.hasOwnProperty(id)) { cancelWatchObject(id); result = true; - } - return result; - }; - this.cancelDomElementWatch = function(elementId) { - var result = false; - var storageId; - for (storageId in _watches) { - if (_watches.hasOwnProperty(storageId) && isDefinedString(_watches[storageId].domElementId) && _watches[storageId].domElementId === elementId) { - cancelWatchObject(storageId); - result = true; - break; + } else { + var storageId; + for (storageId in _watches) { + if (_watches.hasOwnProperty(storageId) && isDefinedString(_watches[storageId].domElementId) && _watches[storageId].domElementId === id) { + cancelWatchObject(storageId); + result = true; + break; + } } } return result; @@ -296,6 +293,14 @@ var result = null; if (_watches.hasOwnProperty(id)) { result = _watches[id]; + } else { + var storageId; + for (storageId in _watches) { + if (_watches.hasOwnProperty(storageId) && isDefinedString(_watches[storageId].domElementId) && _watches[storageId].domElementId === id) { + result = _watches[storageId]; + break; + } + } } return result; }; diff --git a/dist/observe.min.js b/dist/observe.min.js index e38bac4..acd6c91 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,9 +1,9 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ -(function(){function G(a,b,c){var f=null;if(r(a)){f=H();var d=I(b);b={};b.options=d;b.domElementId=c;b.totalChanges=0;t(c)?(a=v.getElementById(c),p(a)&&(b.cachedObject=a.outerHTML,b.originalObject=a.outerHTML)):(b.cachedObject=JSON.stringify(a),b.originalObject=a);b.timer=setInterval(function(){var k=new Date;if(!x(d.starts)||k>=d.starts){var u=f;if(h.hasOwnProperty(u)){var g=h[u],e=t(g.domElementId),m=null;e&&(m=v.getElementById(g.domElementId),p(m)&&(g.originalObject=m.outerHTML));var l=g.cachedObject, -n=g.originalObject,y=e?n:JSON.stringify(n);l!==y&&(n=g.options,n.reset?e?m.outerHTML=g.cachedObject:g.originalObject=z(l).result:g.cachedObject=y,e?A(n.onChange,l,y):(e=z(l).result,m=z(y).result,A(n.onChange,e,m),B(n.onPropertyChange)&&!J(e)&&K(e,m,n)),n.cancelOnChange&&w(u),g.totalChanges++,0=n.maximumChangesBeforeCanceling&&w(u))}x(d.expires)&&k>=d.expires&&w(f)}},d.observeTimeout);h[f]=b}return f}function K(a,b,c){for(var f in a)if(a.hasOwnProperty(f)){var d= -a[f],k=null;b.hasOwnProperty(f)&&(k=b[f]);r(d)&&r(k)?K(d,k,c):JSON.stringify(d)!==JSON.stringify(k)&&A(c.onPropertyChange,f,d,k)}}function w(a){h.hasOwnProperty(a)&&(A(h[a].options.onCancel,a),clearTimeout(h[a].timer),delete h[a])}function I(a){var b=a=r(a)?a:{};var c=a.observeTimeout;c=L(c)?c:250;b.observeTimeout=c;b=a;c=a.starts;c=x(c)?c:null;b.starts=c;b=a;c=a.expires;c=x(c)?c:null;b.expires=c;a.reset=C(a.reset,!1);a.cancelOnChange=C(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c= +(function(){function G(a,b,c){var g=null;if(r(a)){g=H();var e=I(b);b={};b.options=e;b.domElementId=c;b.totalChanges=0;t(c)?(a=v.getElementById(c),p(a)&&(b.cachedObject=a.outerHTML,b.originalObject=a.outerHTML)):(b.cachedObject=JSON.stringify(a),b.originalObject=a);b.timer=setInterval(function(){var k=new Date;if(!x(e.starts)||k>=e.starts){var u=g;if(d.hasOwnProperty(u)){var h=d[u],f=t(h.domElementId),m=null;f&&(m=v.getElementById(h.domElementId),p(m)&&(h.originalObject=m.outerHTML));var l=h.cachedObject, +n=h.originalObject,y=f?n:JSON.stringify(n);l!==y&&(n=h.options,n.reset?f?m.outerHTML=h.cachedObject:h.originalObject=z(l).result:h.cachedObject=y,f?A(n.onChange,l,y):(f=z(l).result,m=z(y).result,A(n.onChange,f,m),B(n.onPropertyChange)&&!J(f)&&K(f,m,n)),n.cancelOnChange&&w(u),h.totalChanges++,0=n.maximumChangesBeforeCanceling&&w(u))}x(e.expires)&&k>=e.expires&&w(g)}},e.observeTimeout);d[g]=b}return g}function K(a,b,c){for(var g in a)if(a.hasOwnProperty(g)){var e= +a[g],k=null;b.hasOwnProperty(g)&&(k=b[g]);r(e)&&r(k)?K(e,k,c):JSON.stringify(e)!==JSON.stringify(k)&&A(c.onPropertyChange,g,e,k)}}function w(a){d.hasOwnProperty(a)&&(A(d[a].options.onCancel,a),clearTimeout(d[a].timer),delete d[a])}function I(a){var b=a=r(a)?a:{};var c=a.observeTimeout;c=L(c)?c:250;b.observeTimeout=c;b=a;c=a.starts;c=x(c)?c:null;b.starts=c;b=a;c=a.expires;c=x(c)?c:null;b.expires=c;a.reset=C(a.reset,!1);a.cancelOnChange=C(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c= L(c)?c:0;b.maximumChangesBeforeCanceling=c;a.onChange=D(a.onChange,null);a.onPropertyChange=D(a.onPropertyChange,null);a.onCancel=D(a.onCancel,null);return a}function A(a){B(a)&&a.apply(null,[].slice.call(arguments,1))}function H(){for(var a=[],b=0;32>b;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(E.empty)}function p(a){return null!==a&&void 0!==a&&a!==E.empty}function r(a){return p(a)&&"object"===typeof a}function t(a){return p(a)&& -"string"===typeof a}function B(a){return p(a)&&"function"===typeof a}function L(a){return p(a)&&"number"===typeof a}function J(a){return r(a)&&a instanceof Array}function x(a){return r(a)&&a instanceof Date}function C(a,b){return p(a)&&"boolean"===typeof a?a:b}function D(a,b){return B(a)?a:b}function z(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(f){try{c=eval("("+a+")"),B(c)&&(c=c())}catch(d){b=N("Errors in object: "+f.message+", "+d.message),c=null}}return{parsed:b,result:c}}function N(a){var b= -!0;q.safeMode||(console.error(a),b=!1);return b}function M(){q.safeMode=C(q.safeMode,!0);var a=q,b=q.domElementTypes,c=["*"];t(b)?(b=b.split(E.space),0===b.length&&(b=c)):b=J(b)?b:c;a.domElementTypes=b}var v=null,F=null,E={empty:""},h={},q={};this.watchObject=function(a,b){return G(a,b)};this.cancelWatch=function(a){var b=!1;h.hasOwnProperty(a)&&(w(a),b=!0);return b};this.cancelDomElementWatch=function(a){var b=!1,c;for(c in h)if(h.hasOwnProperty(c)&&t(h[c].domElementId)&&h[c].domElementId===a){w(c); -b=!0;break}return b};this.getWatch=function(a){var b=null;h.hasOwnProperty(a)&&(b=h[a]);return b};this.setConfiguration=function(a){q=r(a)?a:{};M();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){v=a;F=b;M();v.addEventListener("DOMContentLoaded",function(){for(var c=q.domElementTypes,f=c.length,d=0;d diff --git a/docs/PUBLIC_FUNCTIONS.md b/docs/PUBLIC_FUNCTIONS.md index 5124611..aba3f83 100644 --- a/docs/PUBLIC_FUNCTIONS.md +++ b/docs/PUBLIC_FUNCTIONS.md @@ -20,23 +20,15 @@ Adds an object that should be watched for changes. ### **cancelWatch( *id* )**: Cancels the watching of an object for changes.
-***Parameter: id***: '*string*' - The Id of the object being watched. +***Parameter: id***: '*string*' - The Id of the object being watched, or DOM element ID being watched.
***Returns***: '*boolean*' - States if the object being watched has been canceled.
-### **cancelDomElementWatch( *elementId* )**: -Cancels the watching of a DOM element object for changes. -
-***Parameter: elementId***: '*string*' - The Id of the DOM element object being watched. -
-***Returns***: '*boolean*' - States if the DOM element object being watched has been canceled. -
- ### **getWatch( *id* )**: Returns the properties for an active watch.
-***Parameter: id***: '*string*' - The Id of the object being watched. +***Parameter: id***: '*string*' - The Id of the object being watched, or DOM element ID being watched.
***Returns***: '*Object*' - The watch properties for an object (null if not found).
diff --git a/src/observe.js b/src/observe.js index d625c14..a1118fc 100644 --- a/src/observe.js +++ b/src/observe.js @@ -451,7 +451,7 @@ * * @public * - * @param {string} id The Id of the object being watched. + * @param {string} id The Id of the object being watched, or DOM element ID being watched. * * @returns {boolean} States if the object being watched has been canceled. */ @@ -462,31 +462,15 @@ cancelWatchObject( id ); result = true; - } - - return result; - }; - - /** - * cancelDomElementWatch(). - * - * Cancels the watching of a DOM element object for changes. - * - * @public - * - * @param {string} elementId The Id of the DOM element object being watched. - * - * @returns {boolean} States if the DOM element object being watched has been canceled. - */ - this.cancelDomElementWatch = function( elementId ) { - var result = false; + } else { - for ( var storageId in _watches ) { - if ( _watches.hasOwnProperty( storageId ) && isDefinedString( _watches[ storageId ].domElementId ) && _watches[ storageId ].domElementId === elementId ) { - cancelWatchObject( storageId ); - - result = true; - break; + for ( var storageId in _watches ) { + if ( _watches.hasOwnProperty( storageId ) && isDefinedString( _watches[ storageId ].domElementId ) && _watches[ storageId ].domElementId === id ) { + cancelWatchObject( storageId ); + + result = true; + break; + } } } @@ -500,7 +484,7 @@ * * @public * - * @param {string} id The Id of the object being watched. + * @param {string} id The Id of the object being watched, or DOM element ID being watched. * * @returns {Object} The watch properties for an object (null if not found). */ @@ -509,6 +493,14 @@ if ( _watches.hasOwnProperty( id ) ) { result = _watches[ id ]; + } else { + + for ( var storageId in _watches ) { + if ( _watches.hasOwnProperty( storageId ) && isDefinedString( _watches[ storageId ].domElementId ) && _watches[ storageId ].domElementId === id ) { + result = _watches[ storageId ]; + break; + } + } } return result; diff --git a/test/dist/observe.js.basic.html b/test/dist/observe.js.basic.html index f6db4de..f20b0ca 100644 --- a/test/dist/observe.js.basic.html +++ b/test/dist/observe.js.basic.html @@ -28,7 +28,7 @@

Observe.js - Basic

Watch Object:

- +

Change Object Values:

@@ -42,6 +42,7 @@

Change Object Values:

Get Watch Object:

+

Configuration:

diff --git a/test/dist/observe.js.min.html b/test/dist/observe.js.min.html index 834e3c6..94fd2d6 100644 --- a/test/dist/observe.js.min.html +++ b/test/dist/observe.js.min.html @@ -28,7 +28,7 @@

Observe.js - Basic

Watch Object:

- +

Change Object Values:

@@ -42,6 +42,7 @@

Change Object Values:

Get Watch Object:

+

Configuration:

diff --git a/test/src/observe.js.basic.html b/test/src/observe.js.basic.html index cd75648..6010161 100644 --- a/test/src/observe.js.basic.html +++ b/test/src/observe.js.basic.html @@ -28,7 +28,7 @@

Observe.js - Basic

Watch Object:

- +

Change Object Values:

@@ -42,6 +42,7 @@

Change Object Values:

Get Watch Object:

+

Configuration:

From d852edbc30119d62c0174d7a994280888ec75bfa Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 14:23:29 +0000 Subject: [PATCH 19/20] Renamed the binding/option "observeTimeout" to "timeout". --- dist/observe.js | 4 ++-- dist/observe.min.js | 12 ++++++------ docs/CHANGE_LOG.md | 1 + docs/binding/options/OPTIONS.md | 6 +++--- src/observe.js | 4 ++-- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/dist/observe.js b/dist/observe.js index 090756c..355046b 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -70,7 +70,7 @@ cancelWatchObject(storageId); } } - }, watchOptions.observeTimeout); + }, watchOptions.timeout); _watches[storageId] = watch; } return storageId; @@ -149,7 +149,7 @@ } function getWatchOptions(newOptions) { var options = !isDefinedObject(newOptions) ? {} : newOptions; - options.observeTimeout = getDefaultNumber(options.observeTimeout, 250); + options.timeout = getDefaultNumber(options.timeout, 250); options.starts = getDefaultDate(options.starts, null); options.expires = getDefaultDate(options.expires, null); options.reset = getDefaultBoolean(options.reset, false); diff --git a/dist/observe.min.js b/dist/observe.min.js index acd6c91..5077f20 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,9 +1,9 @@ /*! Observe.js v0.2.0 | (c) Bunoon | MIT License */ (function(){function G(a,b,c){var g=null;if(r(a)){g=H();var e=I(b);b={};b.options=e;b.domElementId=c;b.totalChanges=0;t(c)?(a=v.getElementById(c),p(a)&&(b.cachedObject=a.outerHTML,b.originalObject=a.outerHTML)):(b.cachedObject=JSON.stringify(a),b.originalObject=a);b.timer=setInterval(function(){var k=new Date;if(!x(e.starts)||k>=e.starts){var u=g;if(d.hasOwnProperty(u)){var h=d[u],f=t(h.domElementId),m=null;f&&(m=v.getElementById(h.domElementId),p(m)&&(h.originalObject=m.outerHTML));var l=h.cachedObject, -n=h.originalObject,y=f?n:JSON.stringify(n);l!==y&&(n=h.options,n.reset?f?m.outerHTML=h.cachedObject:h.originalObject=z(l).result:h.cachedObject=y,f?A(n.onChange,l,y):(f=z(l).result,m=z(y).result,A(n.onChange,f,m),B(n.onPropertyChange)&&!J(f)&&K(f,m,n)),n.cancelOnChange&&w(u),h.totalChanges++,0=n.maximumChangesBeforeCanceling&&w(u))}x(e.expires)&&k>=e.expires&&w(g)}},e.observeTimeout);d[g]=b}return g}function K(a,b,c){for(var g in a)if(a.hasOwnProperty(g)){var e= -a[g],k=null;b.hasOwnProperty(g)&&(k=b[g]);r(e)&&r(k)?K(e,k,c):JSON.stringify(e)!==JSON.stringify(k)&&A(c.onPropertyChange,g,e,k)}}function w(a){d.hasOwnProperty(a)&&(A(d[a].options.onCancel,a),clearTimeout(d[a].timer),delete d[a])}function I(a){var b=a=r(a)?a:{};var c=a.observeTimeout;c=L(c)?c:250;b.observeTimeout=c;b=a;c=a.starts;c=x(c)?c:null;b.starts=c;b=a;c=a.expires;c=x(c)?c:null;b.expires=c;a.reset=C(a.reset,!1);a.cancelOnChange=C(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c= -L(c)?c:0;b.maximumChangesBeforeCanceling=c;a.onChange=D(a.onChange,null);a.onPropertyChange=D(a.onPropertyChange,null);a.onCancel=D(a.onCancel,null);return a}function A(a){B(a)&&a.apply(null,[].slice.call(arguments,1))}function H(){for(var a=[],b=0;32>b;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(E.empty)}function p(a){return null!==a&&void 0!==a&&a!==E.empty}function r(a){return p(a)&&"object"===typeof a}function t(a){return p(a)&& -"string"===typeof a}function B(a){return p(a)&&"function"===typeof a}function L(a){return p(a)&&"number"===typeof a}function J(a){return r(a)&&a instanceof Array}function x(a){return r(a)&&a instanceof Date}function C(a,b){return p(a)&&"boolean"===typeof a?a:b}function D(a,b){return B(a)?a:b}function z(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(g){try{c=eval("("+a+")"),B(c)&&(c=c())}catch(e){b=N("Errors in object: "+g.message+", "+e.message),c=null}}return{parsed:b,result:c}}function N(a){var b= -!0;q.safeMode||(console.error(a),b=!1);return b}function M(){q.safeMode=C(q.safeMode,!0);var a=q,b=q.domElementTypes,c=["*"];t(b)?(b=b.split(E.space),0===b.length&&(b=c)):b=J(b)?b:c;a.domElementTypes=b}var v=null,F=null,E={empty:""},d={},q={};this.watchObject=function(a,b){return G(a,b)};this.cancelWatch=function(a){var b=!1;if(d.hasOwnProperty(a))w(a),b=!0;else for(var c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){w(c);b=!0;break}return b};this.getWatch=function(a){var b= -null;if(d.hasOwnProperty(a))b=d[a];else for(var c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){b=d[c];break}return b};this.setConfiguration=function(a){q=r(a)?a:{};M();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){v=a;F=b;M();v.addEventListener("DOMContentLoaded",function(){for(var c=q.domElementTypes,g=c.length,e=0;e=n.maximumChangesBeforeCanceling&&w(u))}x(e.expires)&&k>=e.expires&&w(g)}},e.timeout);d[g]=b}return g}function K(a,b,c){for(var g in a)if(a.hasOwnProperty(g)){var e= +a[g],k=null;b.hasOwnProperty(g)&&(k=b[g]);r(e)&&r(k)?K(e,k,c):JSON.stringify(e)!==JSON.stringify(k)&&A(c.onPropertyChange,g,e,k)}}function w(a){d.hasOwnProperty(a)&&(A(d[a].options.onCancel,a),clearTimeout(d[a].timer),delete d[a])}function I(a){var b=a=r(a)?a:{};var c=a.timeout;c=L(c)?c:250;b.timeout=c;b=a;c=a.starts;c=x(c)?c:null;b.starts=c;b=a;c=a.expires;c=x(c)?c:null;b.expires=c;a.reset=C(a.reset,!1);a.cancelOnChange=C(a.cancelOnChange,!1);b=a;c=a.maximumChangesBeforeCanceling;c=L(c)?c:0;b.maximumChangesBeforeCanceling= +c;a.onChange=D(a.onChange,null);a.onPropertyChange=D(a.onPropertyChange,null);a.onCancel=D(a.onCancel,null);return a}function A(a){B(a)&&a.apply(null,[].slice.call(arguments,1))}function H(){for(var a=[],b=0;32>b;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(E.empty)}function p(a){return null!==a&&void 0!==a&&a!==E.empty}function r(a){return p(a)&&"object"===typeof a}function t(a){return p(a)&&"string"===typeof a}function B(a){return p(a)&& +"function"===typeof a}function L(a){return p(a)&&"number"===typeof a}function J(a){return r(a)&&a instanceof Array}function x(a){return r(a)&&a instanceof Date}function C(a,b){return p(a)&&"boolean"===typeof a?a:b}function D(a,b){return B(a)?a:b}function z(a){var b=!0,c=null;try{t(a)&&(c=JSON.parse(a))}catch(g){try{c=eval("("+a+")"),B(c)&&(c=c())}catch(e){b=N("Errors in object: "+g.message+", "+e.message),c=null}}return{parsed:b,result:c}}function N(a){var b=!0;q.safeMode||(console.error(a),b=!1); +return b}function M(){q.safeMode=C(q.safeMode,!0);var a=q,b=q.domElementTypes,c=["*"];t(b)?(b=b.split(E.space),0===b.length&&(b=c)):b=J(b)?b:c;a.domElementTypes=b}var v=null,F=null,E={empty:""},d={},q={};this.watchObject=function(a,b){return G(a,b)};this.cancelWatch=function(a){var b=!1;if(d.hasOwnProperty(a))w(a),b=!0;else for(var c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){w(c);b=!0;break}return b};this.getWatch=function(a){var b=null;if(d.hasOwnProperty(a))b=d[a]; +else for(var c in d)if(d.hasOwnProperty(c)&&t(d[c].domElementId)&&d[c].domElementId===a){b=d[c];break}return b};this.setConfiguration=function(a){q=r(a)?a:{};M();return this};this.getVersion=function(){return"0.2.0"};(function(a,b){v=a;F=b;M();v.addEventListener("DOMContentLoaded",function(){for(var c=q.domElementTypes,g=c.length,e=0;e ```markdown -
+
Your HTML.
``` @@ -37,7 +37,7 @@ Below is a list of all the options supported in the "data-observe-options" bindi ```markdown ``` \ No newline at end of file diff --git a/src/observe.js b/src/observe.js index a1118fc..68b3dd3 100644 --- a/src/observe.js +++ b/src/observe.js @@ -134,7 +134,7 @@ } } - }, watchOptions.observeTimeout ); + }, watchOptions.timeout ); _watches[ storageId ] = watch; } @@ -244,7 +244,7 @@ function getWatchOptions( newOptions ) { var options = !isDefinedObject( newOptions ) ? {} : newOptions; - options.observeTimeout = getDefaultNumber( options.observeTimeout, 250 ); + options.timeout = getDefaultNumber( options.timeout, 250 ); options.starts = getDefaultDate( options.starts, null ); options.expires = getDefaultDate( options.expires, null ); options.reset = getDefaultBoolean( options.reset, false ); From 07892edb37084db4833e9e39ccf76b1fbb869703 Mon Sep 17 00:00:00 2001 From: William Troup Date: Tue, 2 Jan 2024 16:00:11 +0000 Subject: [PATCH 20/20] Updated change log. --- docs/CHANGE_LOG.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/CHANGE_LOG.md b/docs/CHANGE_LOG.md index c17ab20..02aca0d 100644 --- a/docs/CHANGE_LOG.md +++ b/docs/CHANGE_LOG.md @@ -5,18 +5,18 @@ #### **New Features:** - Added "Reset" support, which will force any object to be reset back to its original value when a change is detected. - Added "Cancel On Change" support, which will force a watch to cancel when the first change is detected in an object. -- Added "Maximum Changes Before Canceling" support, which states how many changes are allowed before a watch is canceled. +- Added "Maximum Changes Before Canceling" support, which states how many changes are allowed before a watch is cancelled. - Added "Starts" support, which states a date/time a watch should be started. #### **Binding Options / Function Options:** - Added a new binding/option called "reset", which states if an object should be reset back to its original value when a change is detected (defaults to false). -- Added a new binding/option called "cancelOnChange", which states if a watch should be canceled when the first change is detected (defaults to false). -- Added a new binding/option called "maximumChangesBeforeCanceling", which states the total number of changes allowed before a watch is canceled (defaults to 0, which for off). +- Added a new binding/option called "cancelOnChange", which states if a watch should be cancelled when the first change is detected (defaults to false). +- Added a new binding/option called "maximumChangesBeforeCanceling", which states the total number of changes allowed before a watch is cancelled (defaults to 0, which for off). - Added a new binding/option called "starts", which states the date/time a watch should start (defaults to null). - Renamed the binding/option "observeTimeout" to "timeout". #### **Binding Options / Function Options - Custom Triggers:** -- Added a new binding/option custom trigger called "onCancel", which states an event that should be triggered when a watch is canceled. +- Added a new binding/option custom trigger called "onCancel", which states an event that should be triggered when a watch is cancelled. #### **Public Functions:** - Added new public function "getWatch()", which is used to return all the watch properties for an object being watched. @@ -33,7 +33,7 @@ #### **Fixes:** - Fixed the "expires" binding/option not accurately checking the expired date and time. -- Fixed a script error that occurred sometimes when a watch is canceled using the public function "cancelWatch()". +- Fixed a script error that occurred sometimes when a watch is cancelled using the public function "cancelWatch()".