Skip to content

Commit

Permalink
Improved event handler.
Browse files Browse the repository at this point in the history
Any function defined in a global object can be used.
  • Loading branch information
feuzeu committed Dec 17, 2022
1 parent b20a257 commit de91c55
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 277 deletions.
245 changes: 107 additions & 138 deletions dist/jaxon.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<jaxon_core.js> file, or by specifying the appropriate configuration
options on a per call basis.
*/
const jaxon = {};
var jaxon = {};

/*
Class: jaxon.config
Expand Down Expand Up @@ -393,123 +393,110 @@ jaxon.tools.array = {


jaxon.tools.dom = {
/*
Function: jaxon.tools.dom.$
Shorthand for finding a uniquely named element within the document.
Parameters:
sId - (string):
The unique name of the element (specified by the ID attribute), not to be confused
with the name attribute on form elements.
Returns:
object - The element found or null.
Note:
This function uses the <jaxon.config.baseDocument> which allows <jaxon> to operate on the
main window document as well as documents from contained iframes and child windows.
See also:
<jaxon.$> and <jxn.$>
*/
/**
* Shorthand for finding a uniquely named element within the document.
*
* Note:
* This function uses the <jaxon.config.baseDocument> which allows <jaxon> to operate on the
* main window document as well as documents from contained iframes and child windows.
*
* @param {string} sId - The unique name of the element (specified by the ID attribute)
* Not to be confused with the name attribute on form elements.
*
* @returns {object} - The element found or null.
*
* @see <jaxon.$> and <jxn.$>
*/
$: function(sId) {
if (!sId)
return null;
//sId not an string so return it maybe its an object.
if (typeof sId != 'string')

if (typeof sId !== 'string')
return sId;

const oDoc = jaxon.config.baseDocument;

const obj = oDoc.getElementById(sId);
if (obj)
return obj;

if (oDoc.all)
return oDoc.all[sId];

return obj;
},

/*
Function: jaxon.tools.dom.getBrowserHTML
Insert the specified string of HTML into the document, then extract it.
This gives the browser the ability to validate the code and to apply any transformations it deems appropriate.
Parameters:
sValue - (string):
A block of html code or text to be inserted into the browser's document.
/**
* Create a div as workspace for the getBrowserHTML() function.
*
* @returns {object} - The workspace DOM element.
*/
_getWorkspace: function() {
const elWorkspace = jaxon.$('jaxon_temp_workspace');
if (elWorkspace) {
return elWorkspace;
}

Returns:
The (potentially modified) html code or text.
*/
getBrowserHTML: function(sValue) {
// Workspace not found. Must be ceated.
const oDoc = jaxon.config.baseDocument;
if (!oDoc.body)
return '';
return null;

const elWorkspace = jaxon.$('jaxon_temp_workspace');
if (!elWorkspace) {
elWorkspace = oDoc.createElement('div');
elWorkspace.setAttribute('id', 'jaxon_temp_workspace');
elWorkspace.style.display = 'none';
elWorkspace.style.visibility = 'hidden';
oDoc.body.appendChild(elWorkspace);
}
const elNewWorkspace = oDoc.createElement('div');
elNewWorkspace.setAttribute('id', 'jaxon_temp_workspace');
elNewWorkspace.style.display = 'none';
elNewWorkspace.style.visibility = 'hidden';
oDoc.body.appendChild(elNewWorkspace);
return elNewWorkspace;
},

/**
* Insert the specified string of HTML into the document, then extract it.
* This gives the browser the ability to validate the code and to apply any transformations it deems appropriate.
*
* @param {string} sValue - A block of html code or text to be inserted into the browser's document.
*
* @returns {string} - The (potentially modified) html code or text.
*/
getBrowserHTML: function(sValue) {
const elWorkspace = jaxon.tools.dom._getWorkspace();
elWorkspace.innerHTML = sValue;
const browserHTML = elWorkspace.innerHTML;
elWorkspace.innerHTML = '';

return browserHTML;
},

/*
Function: jaxon.tools.dom.willChange
Tests to see if the specified data is the same as the current value of the element's attribute.
Parameters:
element - (string or object):
The element or it's unique name (specified by the ID attribute)
attribute - (string):
The name of the attribute.
newData - (string):
The value to be compared with the current value of the specified element.
Returns:
true - The specified value differs from the current attribute value.
false - The specified value is the same as the current value.
*/
/**
* Tests to see if the specified data is the same as the current value of the element's attribute.
*
* @param {string|object} element - The element or it's unique name (specified by the ID attribute)
* @param {string} ttribute - The name of the attribute.
* @param {string} newData - The value to be compared with the current value of the specified element.
*
* @returns {true} - The specified value differs from the current attribute value.
* @returns {false} - The specified value is the same as the current value.
*/
willChange: function(element, attribute, newData) {
if ('string' == typeof element)
if ('string' === typeof element)
element = jaxon.$(element);
if (element) {
let oldData;
// eval('oldData=element.' + attribute);
oldData = element[attribute];
return (newData != oldData);
if (!element) {
return false;
}
return false;
return (newData != element[attribute]);
},

/*
Function: jaxon.tools.dom.findFunction
Find a function using its name as a string.
Parameters:
sFuncName - (string): The name of the function to find.
Returns:
Functiion - The function with the given name.
*/
/**
* Find a function using its name as a string.
*
* @param {string} sFuncName - The name of the function to find.
*
* @returns {object} - The function
*/
findFunction: function (sFuncName) {
let context = window;
const namespaces = sFuncName.split(".");
for(const i = 0; i < namespaces.length && context != undefined; i++) {
context = context[namespaces[i]];
const names = sFuncName.split(".");
const length = names.length;

for(let i = 0; i < length && (context); i++) {
context = context[names[i]];
}
return context;
}
Expand Down Expand Up @@ -1103,22 +1090,16 @@ jaxon.cmd.delay = {


jaxon.cmd.event = {
/*
Function: jaxon.cmd.event.setEvent
Set an event handler.
Parameters:
command - (object): Response command object.
- id: Element ID
- prop: Event
- data: Code
Returns:
true - The operation completed successfully.
*/
/**
* Set an event handler.
*
* @param {object} command - Response command object.
* - id: Element ID
* - prop: Event
* - data: Code
*
* @returns {true} - The operation completed successfully.
*/
setEvent: function(command) {
command.fullName = 'setEvent';
const sEvent = jaxon.tools.string.addOnPrefix(command.prop);
Expand All @@ -1129,22 +1110,16 @@ jaxon.cmd.event = {
return true;
},

/*
Function: jaxon.cmd.event.addHandler
Add an event handler to the specified target.
Parameters:
command - (object): Response command object.
- id: The id of, or the target itself
- prop: The name of the event.
- data: The name of the function to be called
Returns:
true - The operation completed successfully.
*/
/**
* Add an event handler to the specified target.
*
* @param {object} command - Response command object.
* - id: The id of, or the target itself
* - prop: The name of the event.
* - data: The name of the function to be called
*
* @returns {true} - The operation completed successfully.
*/
addHandler: function(command) {
command.fullName = 'addHandler';
const sFuncName = command.data;
Expand All @@ -1154,22 +1129,16 @@ jaxon.cmd.event = {
return jaxon.cmd.event._addHandler(oTarget, sEvent, sFuncName);
},

/*
Function: jaxon.cmd.event.removeHandler
Remove an event handler from an target.
Parameters:
command - (object): Response command object.
- id: The id of, or the target itself
- prop: The name of the event.
- data: The name of the function to be removed
Returns:
true - The operation completed successfully.
*/
/**
* Remove an event handler from an target.
*
* @param {object} command - Response command object.
* - id: The id of, or the target itself
* - prop: The name of the event.
* - data: The name of the function to be called
*
* @returns {true} - The operation completed successfully.
*/
removeHandler: function(command) {
command.fullName = 'removeHandler';
const sFuncName = command.data;
Expand All @@ -1186,12 +1155,12 @@ if(window.addEventListener) {
};

jaxon.cmd.event._addHandler = function(target, event, func) {
target.addEventListener(event, window[func], false);
target.addEventListener(event, jaxon.tools.dom.findFunction(func), false);
return true;
};

jaxon.cmd.event._removeHandler = function(target, event, func) {
target.removeEventListener(event, window[func], false);
target.removeEventListener(event, jaxon.tools.dom.findFunction(func), false);
return true;
};
} else {
Expand All @@ -1200,12 +1169,12 @@ if(window.addEventListener) {
};

jaxon.cmd.event._addHandler = function(target, event, func) {
target.attachEvent(event, window[func]);
target.attachEvent(event, jaxon.tools.dom.findFunction(func));
return true;
};

jaxon.cmd.event._removeHandler = function(target, event, func) {
target.detachEvent(event, window[func]);
target.detachEvent(event, jaxon.tools.dom.findFunction(func));
return true;
};
}
Expand Down
2 changes: 1 addition & 1 deletion dist/jaxon.core.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit de91c55

Please sign in to comment.