Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

0.5.0 #4

Merged
merged 9 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
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.4.0-blue)](https://www.npmjs.com/package/jobserve.js)
[![nuget](https://img.shields.io/badge/nuget-v0.4.0-purple)](https://www.nuget.org/packages/jObserve.js/)
[![npm](https://img.shields.io/badge/npmjs-v0.5.0-blue)](https://www.npmjs.com/package/jobserve.js)
[![nuget](https://img.shields.io/badge/nuget-v0.5.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)
</h1>

> <p align="center">A lightweight, and easy-to-use, JavaScript library for observing any kind of JS object, or HTML DOM element, to detect changes!</p>
> <p align="center">v0.4.0</p>
> <p align="center">v0.5.0</p>
<br />
<br>

<h1>What features does Observe.js have?</h1>

- Zero-dependencies and extremely lightweight!
- JS Object and HTML DOM Element watching!
- Watch for specific property changes!
- Cancellation and Pausing support!
- Full API available via public functions.
- Fully configurable!
Expand Down
7 changes: 4 additions & 3 deletions README_NUGET.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Observe.js v0.4.0
# Observe.js v0.5.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.4.0-blue)](https://www.npmjs.com/package/jobserve.js)
[![nuget](https://img.shields.io/badge/nuget-v0.4.0-purple)](https://www.nuget.org/packages/jObserve.js/)
[![npm](https://img.shields.io/badge/npmjs-v0.5.0-blue)](https://www.npmjs.com/package/jobserve.js)
[![nuget](https://img.shields.io/badge/nuget-v0.5.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)
Expand All @@ -14,6 +14,7 @@

- Zero-dependencies and extremely lightweight!
- JS Object and HTML DOM Element watching!
- Watch for specific property changes!
- Cancellation and Pausing support!
- Full API available via public functions.
- Fully configurable!
Expand Down
56 changes: 48 additions & 8 deletions dist/observe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! Observe.js v0.4.0 | (c) Bunoon | MIT License */
/*! Observe.js v0.5.0 | (c) Bunoon | MIT License */
(function() {
function collectDOMObjects() {
var tagTypes = _configuration.domElementTypes;
Expand Down Expand Up @@ -90,6 +90,7 @@
watch.originalObject = domElement.outerHTML;
} else {
watch.originalObject = _string.empty;
fireCustomTrigger(watch.options.onRemove, watch.domElementId);
}
}
var cachedObject = watch.cachedObject;
Expand All @@ -110,9 +111,11 @@
} else {
var oldValue = getObjectFromString(cachedObject).result;
var newValue = getObjectFromString(originalObjectJson).result;
fireCustomTrigger(watch.options.onChange, oldValue, newValue);
if (isDefinedFunction(watch.options.onPropertyChange) && !isDefinedArray(oldValue)) {
compareWatchObjectProperties(oldValue, newValue, watch.options);
if (!isDefinedArray(oldValue) && !isDefinedArray(newValue)) {
compareWatchObject(oldValue, newValue, watch);
if (isDefinedFunction(watch.options.onPropertyChange)) {
compareWatchObjectProperties(oldValue, newValue, watch);
}
}
}
if (watch.options.pauseTimeoutOnChange > 0) {
Expand All @@ -128,7 +131,23 @@
}
}
}
function compareWatchObjectProperties(oldObject, newObject, options) {
function compareWatchObject(oldObject, newObject, watch) {
if (isDefinedArray(watch.options.propertyNames)) {
var propertyNamesLength = watch.options.propertyNames.length;
var propertyNameIndex = 0;
for (; propertyNameIndex < propertyNamesLength; propertyNameIndex++) {
var propertyName = watch.options.propertyNames[propertyNameIndex];
if (oldObject[propertyName] !== newObject[propertyName]) {
fireCustomTrigger(watch.options.onChange, oldObject, newObject);
break;
}
}
} else {
fireCustomTrigger(watch.options.onChange, oldObject, newObject);
}
}
function compareWatchObjectProperties(oldObject, newObject, watch) {
var options = watch.options;
var propertyName;
for (propertyName in oldObject) {
if (oldObject.hasOwnProperty(propertyName)) {
Expand All @@ -140,8 +159,10 @@
if (isDefinedObject(propertyOldValue) && isDefinedObject(propertyNewValue)) {
compareWatchObjectProperties(propertyOldValue, propertyNewValue, options);
} else {
if (JSON.stringify(propertyOldValue) !== JSON.stringify(propertyNewValue)) {
fireCustomTrigger(options.onPropertyChange, propertyName, propertyOldValue, propertyNewValue);
if (!isDefinedArray(watch.options.propertyNames) || watch.options.propertyNames.indexOf(propertyName) > -1) {
if (JSON.stringify(propertyOldValue) !== JSON.stringify(propertyNewValue)) {
fireCustomTrigger(options.onPropertyChange, propertyName, propertyOldValue, propertyNewValue);
}
}
}
}
Expand Down Expand Up @@ -181,13 +202,15 @@
options.cancelOnChange = getDefaultBoolean(options.cancelOnChange, false);
options.maximumChangesBeforeCanceling = getDefaultNumber(options.maximumChangesBeforeCanceling, 0);
options.pauseTimeoutOnChange = getDefaultNumber(options.pauseTimeoutOnChange, 0);
options.propertyNames = getDefaultArray(options.propertyNames, null);
options = getWatchOptionsCustomTriggers(options);
return options;
}
function getWatchOptionsCustomTriggers(options) {
options.onChange = getDefaultFunction(options.onChange, null);
options.onPropertyChange = getDefaultFunction(options.onPropertyChange, null);
options.onCancel = getDefaultFunction(options.onCancel, null);
options.onRemove = getDefaultFunction(options.onRemove, null);
return options;
}
function fireCustomTrigger(triggerFunction) {
Expand Down Expand Up @@ -352,6 +375,23 @@
}
return result;
};
this.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;
}
}
}
return result;
};
this.searchDomForNewWatches = function() {
collectDOMObjects();
return this;
Expand All @@ -362,7 +402,7 @@
return this;
};
this.getVersion = function() {
return "0.4.0";
return "0.5.0";
};
(function(documentObject, windowObject) {
_parameter_Document = documentObject;
Expand Down
23 changes: 12 additions & 11 deletions dist/observe.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions docs/CHANGE_LOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# Observe.js - Change Log:

## Version 0.5.0:

#### **New Features:**
- Added specific properties watch support! This will only watch for changes in specific properties on an object, instead of all of them by default.

#### **Binding Options / Function Options:**
- Added a new binding/option called "propertyNames", which states the property names that should be watched for changes (defaults to all).

#### **Binding Options / Function Options - Custom Triggers:**
- Added a new binding/option custom trigger called "onRemove", which states an event that should be triggered when a DOM element is removed.

#### **Public Functions:**
- Added new public function "resumeWatch()", which is used to resume a watch that has been paused.

#### **Fixes:**
- Fixed a fault that allowed comparisons between arrays and objects (was causing some script errors).

#### **Documentation:**
- Fixed some of the documentation mistakes.

<br>


## Version 0.4.0:

#### **Public Functions:**
Expand Down
9 changes: 8 additions & 1 deletion docs/PUBLIC_FUNCTIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ Pauses the watching of an object for changes for a specific number of millisecon
***Returns***: '*boolean*' - States if the object being watched has been paused.
<br>

### **resumeWatch( *id* )**:
Resumes the watching of an object for changes after it was paused.
<br>
***Parameter: id***: '*string*' - The ID of the object being watched, or DOM element ID being watched.
<br>
***Returns***: '*boolean*' - States if the watching of an object has been resumed
<br>

### **searchDomForNewWatches()**:
Searches the DOM for new elements to watch, and adds them.
<br>
Expand Down Expand Up @@ -86,7 +94,6 @@ Returns the version of Observe.js.


## Example:
<br/>

```markdown
<script>
Expand Down
Loading
Loading