diff --git a/README.md b/README.md index f660f8b..1b064ed 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.8.0-blue)](https://www.npmjs.com/package/jobserve.js) -[![nuget](https://img.shields.io/badge/nuget-v0.8.0-purple)](https://www.nuget.org/packages/jObserve.js/) +[![npm](https://img.shields.io/badge/npmjs-v0.8.1-blue)](https://www.npmjs.com/package/jobserve.js) +[![nuget](https://img.shields.io/badge/nuget-v0.8.1-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://www.william-troup.com/) >

A lightweight JavaScript library that allows developers to keep track of changes to JavaScript objects and/or DOM elements.

->

v0.8.0

+>

v0.8.1



diff --git a/README_NUGET.md b/README_NUGET.md index 2427823..0352b1c 100644 --- a/README_NUGET.md +++ b/README_NUGET.md @@ -1,8 +1,8 @@ -# Observe.js v0.8.0 +# Observe.js v0.8.1 [![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.8.0-blue)](https://www.npmjs.com/package/jobserve.js) -[![nuget](https://img.shields.io/badge/nuget-v0.8.0-purple)](https://www.nuget.org/packages/jObserve.js/) +[![npm](https://img.shields.io/badge/npmjs-v0.8.1-blue)](https://www.npmjs.com/package/jobserve.js) +[![nuget](https://img.shields.io/badge/nuget-v0.8.1-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://www.william-troup.com/) diff --git a/dist/observe.js b/dist/observe.js index 3bcc6f5..c5748a3 100644 --- a/dist/observe.js +++ b/dist/observe.js @@ -1,15 +1,11 @@ -/*! Observe.js v0.8.0 | (c) Bunoon 2024 | MIT License */ +/*! Observe.js v0.8.1 | (c) Bunoon 2024 | MIT License */ (function() { + var _parameter_Document = null, _parameter_Window = null, _parameter_Math = null, _parameter_Json = null, _public = {}, _string = {empty:""}, _watches = {}, _watches_Cancel = false, _configuration = {}, _attribute_Name_Watch_Options = "data-observe-watch-options"; function collectDOMObjects() { - var tagTypes = _configuration.domElementTypes; - var tagTypesLength = tagTypes.length; - var tagTypeIndex = 0; - for (; tagTypeIndex < tagTypesLength; tagTypeIndex++) { - var domElements = _parameter_Document.getElementsByTagName(tagTypes[tagTypeIndex]); - var elements = [].slice.call(domElements); - var elementsLength = elements.length; - var elementIndex = 0; - for (; elementIndex < elementsLength; elementIndex++) { + var tagTypes = _configuration.domElementTypes, tagTypesLength = tagTypes.length; + for (var tagTypeIndex = 0; tagTypeIndex < tagTypesLength; tagTypeIndex++) { + var domElements = _parameter_Document.getElementsByTagName(tagTypes[tagTypeIndex]), elements = [].slice.call(domElements), elementsLength = elements.length; + for (var elementIndex = 0; elementIndex < elementsLength; elementIndex++) { if (!collectDOMObject(elements[elementIndex])) { break; } @@ -46,9 +42,7 @@ var storageId = null; if (isDefinedObject(object)) { storageId = newGuid(); - var watchOptions = getWatchOptions(options); - var watch = {}; - var startWatchObject; + var watchOptions = getWatchOptions(options), watch = {}, startWatchObject; watch.options = watchOptions; watch.totalChanges = 0; if (isDefinedString(domElementId)) { @@ -85,9 +79,7 @@ } function watchObjectForChanges(storageId) { if (_watches.hasOwnProperty(storageId)) { - var watch = _watches[storageId]; - var isDomElement = isDefinedString(watch.domElementId); - var domElement = null; + var watch = _watches[storageId], isDomElement = isDefinedString(watch.domElementId), domElement = null; if (isDomElement) { domElement = _parameter_Document.getElementById(watch.domElementId); if (isDefined(domElement)) { @@ -97,9 +89,7 @@ fireCustomTrigger(watch.options.onRemove, watch.domElementId); } } - var cachedObject = watch.cachedObject; - var originalObject = watch.originalObject; - var originalObjectJson = !isDomElement ? _parameter_Json.stringify(originalObject) : originalObject; + var cachedObject = watch.cachedObject, originalObject = watch.originalObject, originalObjectJson = !isDomElement ? _parameter_Json.stringify(originalObject) : originalObject; if (cachedObject !== originalObjectJson) { if (watch.options.reset) { if (isDomElement) { @@ -113,8 +103,7 @@ if (isDomElement) { fireCustomTrigger(watch.options.onChange, cachedObject, originalObjectJson); } else { - var oldValue = getObjectFromString(cachedObject).result; - var newValue = getObjectFromString(originalObjectJson).result; + var oldValue = getObjectFromString(cachedObject).result, newValue = getObjectFromString(originalObjectJson).result; if (!isDefinedArray(oldValue) && !isDefinedArray(newValue)) { compareWatchObject(oldValue, newValue, watch); if (isDefinedFunction(watch.options.onPropertyChange)) { @@ -140,8 +129,7 @@ function compareWatchObject(oldObject, newObject, watch) { if (isDefinedArray(watch.options.propertyNames)) { var propertyNamesLength = watch.options.propertyNames.length; - var propertyNameIndex = 0; - for (; propertyNameIndex < propertyNamesLength; propertyNameIndex++) { + for (var propertyNameIndex = 0; propertyNameIndex < propertyNamesLength; propertyNameIndex++) { var propertyName = watch.options.propertyNames[propertyNameIndex]; if (oldObject[propertyName] !== newObject[propertyName]) { fireCustomTrigger(watch.options.onChange, oldObject, newObject); @@ -153,11 +141,9 @@ } } function compareWatchObjectProperties(oldObject, newObject, watch) { - var propertyName; - for (propertyName in oldObject) { + for (var propertyName in oldObject) { if (oldObject.hasOwnProperty(propertyName)) { - var propertyOldValue = oldObject[propertyName]; - var propertyNewValue = null; + var propertyOldValue = oldObject[propertyName], propertyNewValue = null; if (newObject.hasOwnProperty(propertyName)) { propertyNewValue = newObject[propertyName]; } @@ -174,8 +160,7 @@ } } function cancelWatchesForObjects() { - var storageId; - for (storageId in _watches) { + for (var storageId in _watches) { if (_watches.hasOwnProperty(storageId)) { cancelWatchObject(storageId); } @@ -234,8 +219,7 @@ } function newGuid() { var result = []; - var charIndex = 0; - for (; charIndex < 32; charIndex++) { + for (var charIndex = 0; charIndex < 32; charIndex++) { if (charIndex === 8 || charIndex === 12 || charIndex === 16 || charIndex === 20) { result.push("-"); } @@ -301,8 +285,7 @@ return value; } function getObjectFromString(objectString) { - var parsed = true; - var result = null; + var parsed = true, result = null; try { if (isDefinedString(objectString)) { result = _parameter_Json.parse(objectString); @@ -328,128 +311,134 @@ } return result; } - function buildDefaultConfiguration() { - _configuration.safeMode = getDefaultBoolean(_configuration.safeMode, true); - _configuration.domElementTypes = getDefaultStringOrArray(_configuration.domElementTypes, ["*"]); - buildDefaultConfigurationStrings(); - } - function buildDefaultConfigurationStrings() { - _configuration.objectErrorText = getDefaultString(_configuration.objectErrorText, "Errors in object: {{error_1}}, {{error_2}}"); - _configuration.attributeNotValidErrorText = getDefaultString(_configuration.attributeNotValidErrorText, "The attribute '{{attribute_name}}' is not a valid object."); - _configuration.attributeNotSetErrorText = getDefaultString(_configuration.attributeNotSetErrorText, "The attribute '{{attribute_name}}' has not been set correctly."); - } - var _parameter_Document = null; - var _parameter_Window = null; - var _parameter_Math = null; - var _parameter_Json = null; - var _string = {empty:""}; - var _watches = {}; - var _watches_Cancel = false; - var _configuration = {}; - var _attribute_Name_Watch_Options = "data-observe-watch-options"; - this.watch = function(object, options) { + _public.watch = function(object, options) { return createWatch(object, options); }; - this.cancelWatch = function(id) { + _public.cancelWatch = function(id) { var result = false; - if (_watches.hasOwnProperty(id)) { - cancelWatchObject(id); - result = true; - } else { - var storageId; - for (storageId in _watches) { - if (_watches.hasOwnProperty(storageId) && isDefinedString(_watches[storageId].domElementId) && _watches[storageId].domElementId === id) { - cancelWatchObject(storageId); - result = true; - break; + if (isDefinedString(id)) { + if (_watches.hasOwnProperty(id)) { + cancelWatchObject(id); + result = true; + } else { + for (var storageId in _watches) { + if (_watches.hasOwnProperty(storageId) && isDefinedString(_watches[storageId].domElementId) && _watches[storageId].domElementId === id) { + cancelWatchObject(storageId); + result = true; + break; + } } } } return result; }; - this.cancelWatches = function() { + _public.cancelWatches = function() { cancelWatchesForObjects(); - return this; + return _public; }; - this.getWatch = function(id) { + _public.getWatch = function(id) { 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; + if (isDefinedString(id)) { + 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; }; - this.getWatches = function() { + _public.getWatches = function() { return _watches; }; - this.pauseWatch = function(id, milliseconds) { + _public.pauseWatch = function(id, milliseconds) { var result = false; - if (_watches.hasOwnProperty(id)) { - result = pauseWatchObject(id, milliseconds); - } else { - var storageId; - for (storageId in _watches) { - if (_watches.hasOwnProperty(storageId) && isDefinedString(_watches[storageId].domElementId) && _watches[storageId].domElementId === id) { - result = pauseWatchObject(storageId, milliseconds); - break; + if (isDefinedString(id) && isDefinedNumber(milliseconds)) { + if (_watches.hasOwnProperty(id)) { + result = pauseWatchObject(id, milliseconds); + } else { + for (var storageId in _watches) { + if (_watches.hasOwnProperty(storageId) && isDefinedString(_watches[storageId].domElementId) && _watches[storageId].domElementId === id) { + result = pauseWatchObject(storageId, milliseconds); + break; + } } } } return result; }; - this.pauseWatches = function(milliseconds) { - var storageId; - for (storageId in _watches) { - if (_watches.hasOwnProperty(storageId)) { - pauseWatchObject(storageId, milliseconds); + _public.pauseWatches = function(milliseconds) { + if (isDefinedNumber(milliseconds)) { + for (var storageId in _watches) { + if (_watches.hasOwnProperty(storageId)) { + pauseWatchObject(storageId, milliseconds); + } } } - return this; + return _public; }; - this.resumeWatch = function(id) { + _public.resumeWatch = function(id) { var result = false; - if (_watches.hasOwnProperty(id)) { - _watches[id].options.starts = null; - result = true; - } else { - var storageId; - for (storageId in _watches) { - if (_watches.hasOwnProperty(storageId) && isDefinedString(_watches[storageId].domElementId) && _watches[storageId].domElementId === id) { - _watches[storageId].options.starts = null; - result = true; - break; + if (isDefinedString(id)) { + if (_watches.hasOwnProperty(id)) { + _watches[id].options.starts = null; + result = true; + } else { + for (var storageId in _watches) { + if (_watches.hasOwnProperty(storageId) && isDefinedString(_watches[storageId].domElementId) && _watches[storageId].domElementId === id) { + _watches[storageId].options.starts = null; + result = true; + break; + } } } } return result; }; - this.resumeWatches = function() { - var storageId; - for (storageId in _watches) { + _public.resumeWatches = function() { + for (var storageId in _watches) { if (_watches.hasOwnProperty(storageId)) { _watches[storageId].options.starts = null; } } - return this; + return _public; }; - this.searchDomForNewWatches = function() { + _public.searchDomForNewWatches = function() { collectDOMObjects(); - return this; + return _public; }; - this.setConfiguration = function(newConfiguration) { - _configuration = getDefaultObject(newConfiguration, {}); - buildDefaultConfiguration(); - return this; + _public.setConfiguration = function(newConfiguration) { + if (isDefinedObject(newConfiguration)) { + var configurationHasChanged = false; + for (var propertyName in newConfiguration) { + if (newConfiguration.hasOwnProperty(propertyName) && _configuration.hasOwnProperty(propertyName) && _configuration[propertyName] !== newConfiguration[propertyName]) { + _configuration[propertyName] = newConfiguration[propertyName]; + configurationHasChanged = true; + } + } + if (configurationHasChanged) { + buildDefaultConfiguration(_configuration); + } + } + return _public; }; - this.getVersion = function() { - return "0.8.0"; + function buildDefaultConfiguration(newConfiguration) { + _configuration = !isDefinedObject(newConfiguration) ? {} : newConfiguration; + _configuration.safeMode = getDefaultBoolean(_configuration.safeMode, true); + _configuration.domElementTypes = getDefaultStringOrArray(_configuration.domElementTypes, ["*"]); + buildDefaultConfigurationStrings(); + } + function buildDefaultConfigurationStrings() { + _configuration.objectErrorText = getDefaultString(_configuration.objectErrorText, "Errors in object: {{error_1}}, {{error_2}}"); + _configuration.attributeNotValidErrorText = getDefaultString(_configuration.attributeNotValidErrorText, "The attribute '{{attribute_name}}' is not a valid object."); + _configuration.attributeNotSetErrorText = getDefaultString(_configuration.attributeNotSetErrorText, "The attribute '{{attribute_name}}' has not been set correctly."); + } + _public.getVersion = function() { + return "0.8.1"; }; (function(documentObject, windowObject, mathObject, jsonObject) { _parameter_Document = documentObject; @@ -460,12 +449,12 @@ _parameter_Document.addEventListener("DOMContentLoaded", function() { collectDOMObjects(); }); - _parameter_Window.addEventListener("unload", function() { + _parameter_Window.addEventListener("pagehide", function() { _watches_Cancel = true; cancelWatchesForObjects(); }); if (!isDefined(_parameter_Window.$observe)) { - _parameter_Window.$observe = this; + _parameter_Window.$observe = _public; } })(document, window, Math, JSON); })(); \ No newline at end of file diff --git a/dist/observe.min.js b/dist/observe.min.js index c83bbde..c90c983 100644 --- a/dist/observe.min.js +++ b/dist/observe.min.js @@ -1,14 +1,14 @@ -/*! Observe.js v0.8.0 | (c) Bunoon 2024 | MIT License */ -(function(){function N(){for(var a=g.domElementTypes,b=a.length,c=0;c=h.starts){if(d.hasOwnProperty(k)){var e= -d[k],n=m(e.domElementId),p=null;n&&(p=B.getElementById(e.domElementId),r(p)?e.originalObject=p.outerHTML:(e.originalObject=F.empty,u(e.options.onRemove,e.domElementId)));var x=e.cachedObject,q=e.originalObject;q=n?q:w.stringify(q);if(x!==q){e.options.reset?n?p.outerHTML=e.cachedObject:e.originalObject=D(x).result:e.cachedObject=q;if(n)u(e.options.onChange,x,q);else if(n=D(x).result,p=D(q).result,y(n)||y(p))u(e.options.onChange,n,p);else{if(y(e.options.propertyNames))for(x=e.options.propertyNames.length, -q=0;q=e.options.maximumChangesBeforeCanceling&&z(k)}}E(h.expires)&&v>=h.expires&&z(k)}},h.timeout),d[f]=b)}return f}function S(a,b,c){for(var f in a)if(a.hasOwnProperty(f)){var h= -a[f],l=null;b.hasOwnProperty(f)&&(l=b[f]);t(h)&&t(l)?S(h,l,c.options):(!y(c.options.propertyNames)||-1b;b++){8!==b&&12!==b&&16!==b&&20!==b||a.push("-");var c=L.floor(16*L.random()).toString(16);a.push(c)}return a.join(F.empty)}function r(a){return null!==a&&void 0!==a&&a!== -F.empty}function t(a){return r(a)&&"object"===typeof a}function m(a){return r(a)&&"string"===typeof a}function G(a){return r(a)&&"function"===typeof a}function y(a){return t(a)&&a instanceof Array}function E(a){return t(a)&&a instanceof Date}function A(a,b){return r(a)&&"boolean"===typeof a?a:b}function C(a,b){return G(a)?a:b}function K(a,b){return r(a)&&"number"===typeof a?a:b}function M(a,b){return m(a)?a:b}function D(a){var b=!0,c=null;try{m(a)&&(c=w.parse(a))}catch(f){try{c=eval("("+a+")"),G(c)&& -(c=c())}catch(h){b=J(g.objectErrorText.replace("{{error_1}}",f.message).replace("{{error_2}}",h.message)),c=null}}return{parsed:b,result:c}}function J(a){var b=!0;g.safeMode||(console.error(a),b=!1);return b}function V(){g.safeMode=A(g.safeMode,!0);var a=g,b=g.domElementTypes,c=["*"];m(b)?(b=b.split(F.space),0===b.length&&(b=c)):b=y(b)?b:c;a.domElementTypes=b;g.objectErrorText=M(g.objectErrorText,"Errors in object: {{error_1}}, {{error_2}}");g.attributeNotValidErrorText=M(g.attributeNotValidErrorText, -"The attribute '{{attribute_name}}' is not a valid object.");g.attributeNotSetErrorText=M(g.attributeNotSetErrorText,"The attribute '{{attribute_name}}' has not been set correctly.")}var B=null,I=null,L=null,w=null,F={empty:""},d={},U=!1,g={};this.watch=function(a,b){return Q(a,b)};this.cancelWatch=function(a){var b=!1;if(d.hasOwnProperty(a))z(a),b=!0;else for(var c in d)if(d.hasOwnProperty(c)&&m(d[c].domElementId)&&d[c].domElementId===a){z(c);b=!0;break}return b};this.cancelWatches=function(){T(); -return this};this.getWatch=function(a){var b=null;if(d.hasOwnProperty(a))b=d[a];else for(var c in d)if(d.hasOwnProperty(c)&&m(d[c].domElementId)&&d[c].domElementId===a){b=d[c];break}return b};this.getWatches=function(){return d};this.pauseWatch=function(a,b){var c=!1;if(d.hasOwnProperty(a))c=H(a,b);else for(var f in d)if(d.hasOwnProperty(f)&&m(d[f].domElementId)&&d[f].domElementId===a){c=H(f,b);break}return c};this.pauseWatches=function(a){for(var b in d)d.hasOwnProperty(b)&&H(b,a);return this};this.resumeWatch= -function(a){var b=!1;if(d.hasOwnProperty(a))d[a].options.starts=null,b=!0;else for(var c in d)if(d.hasOwnProperty(c)&&m(d[c].domElementId)&&d[c].domElementId===a){d[c].options.starts=null;b=!0;break}return b};this.resumeWatches=function(){for(var a in d)d.hasOwnProperty(a)&&(d[a].options.starts=null);return this};this.searchDomForNewWatches=function(){N();return this};this.setConfiguration=function(a){var b={};g=t(a)?a:b;V();return this};this.getVersion=function(){return"0.8.0"};(function(a,b,c,f){B= -a;I=b;L=c;w=f;V();B.addEventListener("DOMContentLoaded",function(){N()});I.addEventListener("unload",function(){U=!0;T()});r(I.$observe)||(I.$observe=this)})(document,window,Math,JSON)})(); \ No newline at end of file +/*! Observe.js v0.8.1 | (c) Bunoon 2024 | MIT License */ +(function(){function P(){for(var a=g.domElementTypes,b=a.length,c=0;c=k.starts){if(d.hasOwnProperty(l)){var e=d[l],p=m(e.domElementId),q=null;p&&(q=C.getElementById(e.domElementId),t(q)?e.originalObject=q.outerHTML:(e.originalObject=G.empty,v(e.options.onRemove,e.domElementId)));var y=e.cachedObject,r=e.originalObject;r=p?r:x.stringify(r);if(y!==r){e.options.reset?p?q.outerHTML=e.cachedObject:e.originalObject=E(y).result:e.cachedObject=r;if(p)v(e.options.onChange,y,r);else if(p=E(y).result,q=E(r).result,z(p)||z(q))v(e.options.onChange,p,q);else{if(z(e.options.propertyNames))for(y= +e.options.propertyNames.length,r=0;r=e.options.maximumChangesBeforeCanceling&&A(l)}}F(k.expires)&&w>=k.expires&&A(l)}},k.timeout),d[f]=b)}return f}function U(a,b,c){for(var f in a)if(a.hasOwnProperty(f)){var k= +a[f],n=null;b.hasOwnProperty(f)&&(n=b[f]);u(k)&&u(n)?U(k,n,c.options):(!z(c.options.propertyNames)||-1b;b++){8!==b&&12!==b&&16!==b&&20!==b||a.push("-");var c=M.floor(16*M.random()).toString(16);a.push(c)}return a.join(G.empty)}function t(a){return null!==a&&void 0!==a&&a!==G.empty}function u(a){return t(a)&&"object"===typeof a}function m(a){return t(a)&& +"string"===typeof a}function H(a){return t(a)&&"function"===typeof a}function N(a){return t(a)&&"number"===typeof a}function z(a){return u(a)&&a instanceof Array}function F(a){return u(a)&&a instanceof Date}function B(a,b){return t(a)&&"boolean"===typeof a?a:b}function D(a,b){return H(a)?a:b}function L(a,b){return N(a)?a:b}function O(a,b){return m(a)?a:b}function E(a){var b=!0,c=null;try{m(a)&&(c=x.parse(a))}catch(f){try{c=eval("("+a+")"),H(c)&&(c=c())}catch(k){b=K(g.objectErrorText.replace("{{error_1}}", +f.message).replace("{{error_2}}",k.message)),c=null}}return{parsed:b,result:c}}function K(a){var b=!0;g.safeMode||(console.error(a),b=!1);return b}function X(a){g=u(a)?a:{};g.safeMode=B(g.safeMode,!0);a=g;var b=g.domElementTypes,c=["*"];m(b)?(b=b.split(G.space),0===b.length&&(b=c)):b=z(b)?b:c;a.domElementTypes=b;g.objectErrorText=O(g.objectErrorText,"Errors in object: {{error_1}}, {{error_2}}");g.attributeNotValidErrorText=O(g.attributeNotValidErrorText,"The attribute '{{attribute_name}}' is not a valid object."); +g.attributeNotSetErrorText=O(g.attributeNotSetErrorText,"The attribute '{{attribute_name}}' has not been set correctly.")}var C=null,J=null,M=null,x=null,h={},G={empty:""},d={},W=!1,g={};h.watch=function(a,b){return S(a,b)};h.cancelWatch=function(a){var b=!1;if(m(a))if(d.hasOwnProperty(a))A(a),b=!0;else for(var c in d)if(d.hasOwnProperty(c)&&m(d[c].domElementId)&&d[c].domElementId===a){A(c);b=!0;break}return b};h.cancelWatches=function(){V();return h};h.getWatch=function(a){var b=null;if(m(a))if(d.hasOwnProperty(a))b= +d[a];else for(var c in d)if(d.hasOwnProperty(c)&&m(d[c].domElementId)&&d[c].domElementId===a){b=d[c];break}return b};h.getWatches=function(){return d};h.pauseWatch=function(a,b){var c=!1;if(m(a)&&N(b))if(d.hasOwnProperty(a))c=I(a,b);else for(var f in d)if(d.hasOwnProperty(f)&&m(d[f].domElementId)&&d[f].domElementId===a){c=I(f,b);break}return c};h.pauseWatches=function(a){if(N(a))for(var b in d)d.hasOwnProperty(b)&&I(b,a);return h};h.resumeWatch=function(a){var b=!1;if(m(a))if(d.hasOwnProperty(a))d[a].options.starts= +null,b=!0;else for(var c in d)if(d.hasOwnProperty(c)&&m(d[c].domElementId)&&d[c].domElementId===a){d[c].options.starts=null;b=!0;break}return b};h.resumeWatches=function(){for(var a in d)d.hasOwnProperty(a)&&(d[a].options.starts=null);return h};h.searchDomForNewWatches=function(){P();return h};h.setConfiguration=function(a){if(u(a)){var b=!1,c;for(c in a)a.hasOwnProperty(c)&&g.hasOwnProperty(c)&&g[c]!==a[c]&&(g[c]=a[c],b=!0);b&&X(g)}return h};h.getVersion=function(){return"0.8.1"};(function(a,b,c, +f){C=a;J=b;M=c;x=f;X();C.addEventListener("DOMContentLoaded",function(){P()});J.addEventListener("pagehide",function(){W=!0;V()});t(J.$observe)||(J.$observe=h)})(document,window,Math,JSON)})(); \ No newline at end of file diff --git a/docs/CHANGE_LOG.md b/docs/CHANGE_LOG.md index 6c47c08..f1f344b 100644 --- a/docs/CHANGE_LOG.md +++ b/docs/CHANGE_LOG.md @@ -1,5 +1,14 @@ # Observe.js - Change Log: +## Version 0.8.1: +- The "unload" window event has been replaced with "pagehide" (due to "unload" being deprecated). +- The public function "setConfiguration()" will now only update the configuration if something in the configuration has been changed. +- All public functions now only accept the right variable types for data (to prevent issues from occurring). +- Added "use strict" support internally and updated all public functions to use the new scope. + +
+ + ## Version 0.8.0: #### **Binding Options:** diff --git a/observe.js.nuspec b/observe.js.nuspec index 2fe57ac..4428fc5 100644 --- a/observe.js.nuspec +++ b/observe.js.nuspec @@ -2,7 +2,7 @@ jObserve.js - 0.8.0 + 0.8.1 Observe.js A lightweight JavaScript library that allows developers to keep track of changes to JavaScript objects and/or DOM elements. William Troup diff --git a/package.json b/package.json index e3179d9..16dce49 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "jobserve.js", "title": "Observe.js", "description": "A lightweight JavaScript library that allows developers to keep track of changes to JavaScript objects and/or DOM elements.", - "version": "0.8.0", + "version": "0.8.1", "main": "dist/observe.js", "homepage": "https://www.william-troup.com/observe-js/", "author": { diff --git a/src/observe.js b/src/observe.js index 2c90fe2..1dc4309 100644 --- a/src/observe.js +++ b/src/observe.js @@ -4,7 +4,7 @@ * A lightweight JavaScript library that allows developers to keep track of changes to JavaScript objects and/or DOM elements. * * @file observe.js - * @version v0.8.0 + * @version v0.8.1 * @author Bunoon * @license MIT License * @copyright Bunoon 2024 @@ -12,12 +12,17 @@ ( function() { + "use strict"; + var // Variables: Constructor Parameters _parameter_Document = null, _parameter_Window = null, _parameter_Math = null, _parameter_Json = null, + // Variables: Public Scope + _public = {}, + // Variables: Strings _string = { empty: "" @@ -528,7 +533,7 @@ * * @returns {string} The ID that object watch is stored under. */ - this.watch = function( object, options ) { + _public.watch = function( object, options ) { return createWatch( object, options ); }; @@ -543,21 +548,23 @@ * * @returns {boolean} States if the object being watched has been cancelled. */ - this.cancelWatch = function( id ) { + _public.cancelWatch = function( id ) { var result = false; - if ( _watches.hasOwnProperty( id ) ) { - cancelWatchObject( id ); - - result = true; - } else { - - for ( var storageId in _watches ) { - if ( _watches.hasOwnProperty( storageId ) && isDefinedString( _watches[ storageId ].domElementId ) && _watches[ storageId ].domElementId === id ) { - cancelWatchObject( storageId ); - - result = true; - break; + if ( isDefinedString( id ) ) { + if ( _watches.hasOwnProperty( id ) ) { + cancelWatchObject( id ); + + result = true; + } else { + + for ( var storageId in _watches ) { + if ( _watches.hasOwnProperty( storageId ) && isDefinedString( _watches[ storageId ].domElementId ) && _watches[ storageId ].domElementId === id ) { + cancelWatchObject( storageId ); + + result = true; + break; + } } } } @@ -574,10 +581,10 @@ * * @returns {Object} The Observe.js class instance. */ - this.cancelWatches = function() { + _public.cancelWatches = function() { cancelWatchesForObjects(); - return this; + return _public; }; /** @@ -591,17 +598,19 @@ * * @returns {Object} The watch properties for an object (null if not found). */ - this.getWatch = function( id ) { + _public.getWatch = function( id ) { var result = null; - 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; + if ( isDefinedString( id ) ) { + 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; + } } } } @@ -618,7 +627,7 @@ * * @returns {Object} The object of watches currently running, or paused. */ - this.getWatches = function() { + _public.getWatches = function() { return _watches; }; @@ -634,17 +643,19 @@ * * @returns {boolean} States if the object being watched has been paused. */ - this.pauseWatch = function( id, milliseconds ) { + _public.pauseWatch = function( id, milliseconds ) { var result = false; - if ( _watches.hasOwnProperty( id ) ) { - result = pauseWatchObject( id, milliseconds ); - } else { - - for ( var storageId in _watches ) { - if ( _watches.hasOwnProperty( storageId ) && isDefinedString( _watches[ storageId ].domElementId ) && _watches[ storageId ].domElementId === id ) { - result = pauseWatchObject( storageId, milliseconds ); - break; + if ( isDefinedString( id ) && isDefinedNumber( milliseconds ) ) { + if ( _watches.hasOwnProperty( id ) ) { + result = pauseWatchObject( id, milliseconds ); + } else { + + for ( var storageId in _watches ) { + if ( _watches.hasOwnProperty( storageId ) && isDefinedString( _watches[ storageId ].domElementId ) && _watches[ storageId ].domElementId === id ) { + result = pauseWatchObject( storageId, milliseconds ); + break; + } } } } @@ -663,14 +674,16 @@ * * @returns {Object} The Observe.js class instance. */ - this.pauseWatches = function( milliseconds ) { - for ( var storageId in _watches ) { - if ( _watches.hasOwnProperty( storageId ) ) { - pauseWatchObject( storageId, milliseconds ); + _public.pauseWatches = function( milliseconds ) { + if ( isDefinedNumber( milliseconds ) ) { + for ( var storageId in _watches ) { + if ( _watches.hasOwnProperty( storageId ) ) { + pauseWatchObject( storageId, milliseconds ); + } } } - return this; + return _public; }; /** @@ -684,19 +697,21 @@ * * @returns {boolean} States if the watching of an object has been resumed */ - this.resumeWatch = function( id ) { + _public.resumeWatch = function( id ) { var result = false; - if ( _watches.hasOwnProperty( id ) ) { - _watches[ id ].options.starts = null; - result = true; - } else { - - for ( var storageId in _watches ) { - if ( _watches.hasOwnProperty( storageId ) && isDefinedString( _watches[ storageId ].domElementId ) && _watches[ storageId ].domElementId === id ) { - _watches[ storageId ].options.starts = null; - result = true; - break; + if ( isDefinedString( id ) ) { + if ( _watches.hasOwnProperty( id ) ) { + _watches[ id ].options.starts = null; + result = true; + } else { + + for ( var storageId in _watches ) { + if ( _watches.hasOwnProperty( storageId ) && isDefinedString( _watches[ storageId ].domElementId ) && _watches[ storageId ].domElementId === id ) { + _watches[ storageId ].options.starts = null; + result = true; + break; + } } } } @@ -713,14 +728,14 @@ * * @returns {Object} The Observe.js class instance. */ - this.resumeWatches = function() { + _public.resumeWatches = function() { for ( var storageId in _watches ) { if ( _watches.hasOwnProperty( storageId ) ) { _watches[ storageId ].options.starts = null; } } - return this; + return _public; }; /** @@ -732,10 +747,10 @@ * * @returns {Object} The Observe.js class instance. */ - this.searchDomForNewWatches = function() { + _public.searchDomForNewWatches = function() { collectDOMObjects(); - return this; + return _public; }; @@ -756,15 +771,27 @@ * * @returns {Object} The Observe.js class instance. */ - this.setConfiguration = function( newConfiguration ) { - _configuration = getDefaultObject( newConfiguration, {} ); + _public.setConfiguration = function( newConfiguration ) { + if ( isDefinedObject( newConfiguration ) ) { + var configurationHasChanged = false; - buildDefaultConfiguration(); + for ( var propertyName in newConfiguration ) { + if ( newConfiguration.hasOwnProperty( propertyName ) && _configuration.hasOwnProperty( propertyName ) && _configuration[ propertyName ] !== newConfiguration[ propertyName ] ) { + _configuration[ propertyName ] = newConfiguration[ propertyName ]; + configurationHasChanged = true; + } + } + + if ( configurationHasChanged ) { + buildDefaultConfiguration( _configuration ); + } + } - return this; + return _public; }; - function buildDefaultConfiguration() { + function buildDefaultConfiguration( newConfiguration ) { + _configuration = !isDefinedObject( newConfiguration ) ? {} : newConfiguration; _configuration.safeMode = getDefaultBoolean( _configuration.safeMode, true ); _configuration.domElementTypes = getDefaultStringOrArray( _configuration.domElementTypes, [ "*" ] ); @@ -793,8 +820,8 @@ * * @returns {string} The version number. */ - this.getVersion = function() { - return "0.8.0"; + _public.getVersion = function() { + return "0.8.1"; }; @@ -816,14 +843,14 @@ collectDOMObjects(); } ); - _parameter_Window.addEventListener( "unload", function() { + _parameter_Window.addEventListener( "pagehide", function() { _watches_Cancel = true; cancelWatchesForObjects(); } ); if ( !isDefined( _parameter_Window.$observe ) ) { - _parameter_Window.$observe = this; + _parameter_Window.$observe = _public; } } ) ( document, window, Math, JSON );