Skip to content

Commit

Permalink
netteForms.js: addEvent() uses addEventListener or attachEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
jahudka authored and dg committed Apr 1, 2016
1 parent e9849ef commit 97a6638
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions src/assets/netteForms.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,21 @@ var Nette = {};
* Attaches a handler to an event for the element.
*/
Nette.addEvent = function(element, on, callback) {
var original = element['on' + on];
element['on' + on] = function() {
if (typeof original === 'function' && original.apply(element, arguments) === false) {
return false;
}
return callback.apply(element, arguments);
};
if (element.addEventListener) {
element.addEventListener(on, callback);
} else {
element.attachEvent('on' + on, getHandler(callback));
}
};


function getHandler(callback) {
return function(e) {
return callback.call(this, e);
};
}


/**
* Returns the value of form element.
*/
Expand Down Expand Up @@ -559,15 +564,16 @@ Nette.initOnLoad = function() {
}

Nette.addEvent(document.body, 'click', function(e) {
var target = e ? e.target : event.srcElement;
var target = e.target || e.srcElement;
if (target.form && target.type in {submit: 1, image: 1}) {
target.form['nette-submittedBy'] = target;
}
});

Nette.addEvent(document.body, 'change', function(e) {
if (e.target['nette-toggleForm']) {
Nette.toggleForm(e.target.form, e.target);
var target = e.target || e.srcElement;
if (target['nette-toggleForm']) {
Nette.toggleForm(target.form, target);
}
});
});
Expand Down

0 comments on commit 97a6638

Please sign in to comment.