Skip to content

Commit

Permalink
Changes:
Browse files Browse the repository at this point in the history
* Improvement suggested #109
* changed Modal `clickHandler`, hoping to fix #111
* Corrected some minor issue with Modal
  • Loading branch information
thednp committed Feb 8, 2017
1 parent f08c70d commit 29fed4a
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 34 deletions.
7 changes: 7 additions & 0 deletions assets/js/scripts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
var myModal = document.getElementById('myModal'); // we need a blank modal to fill up and target our object function
myModal.addEventListener('show.bs.modal', function(e) {
console.log('show.bs.modal triggered for #'+e.target.id);
console.log('event.relatedTarget is '+e.relatedTarget.tagName+'.'+e.relatedTarget.className.replace(/\s/g,'.'));
}, false);


// Modal initialized with JavaScript
var myModalJS = document.getElementById('myModalJS'); // we need a blank modal to fill up and target our object function
var btnModal = document.getElementById('openModalViaJS');
Expand Down
19 changes: 12 additions & 7 deletions dist-v4/bootstrap-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,19 @@
mouseHover = ('onmouseleave' in document) ? [ 'mouseenter', 'mouseleave'] : [ 'mouseover', 'mouseout' ],
tipPositions = /\b(top|bottom|left|top)+/,

// class manipulation, since 1.2.0 requires polyfill.js
// set new focus element since 2.0.3
setFocus = function(element){
element.focus ? element.focus() : element.setActive();
},

// class manipulation, since 2.0.0 requires polyfill.js
addClass = function(element,classNAME) {
element.classList.add(classNAME);
},
removeClass = function(element,classNAME) {
element.classList.remove(classNAME);
},
hasClass = function(element,classNAME){ // since 1.2.0
hasClass = function(element,classNAME){ // since 2.0.0
return element.classList.contains(classNAME);
},

Expand Down Expand Up @@ -922,12 +927,11 @@
clickHandler = function(e) {
var clickTarget = e[target];
clickTarget = clickTarget[hasAttribute](dataTarget) || clickTarget[hasAttribute]('href') ? clickTarget : clickTarget[parentNode];
if ( !e.defaultPrevented && !open && clickTarget === element && !hasClass(modal,showClass) ) {
if ( !open && clickTarget === element && !hasClass(modal,showClass) ) {
modal.modalTrigger = element;
relatedTarget = element;
self.show();
e.preventDefault();
setTimeout(function(){ e.defaultPrevented = false; }, 50);
}
},
keyHandler = function(e) {
Expand All @@ -938,12 +942,11 @@
},
dismissHandler = function(e) {
var clickTarget = e[target];
if ( !e.defaultPrevented && open && (clickTarget[parentNode][getAttribute](dataDismiss) === component
if ( open && (clickTarget[parentNode][getAttribute](dataDismiss) === component
|| clickTarget[getAttribute](dataDismiss) === component
|| (clickTarget === modal && self[backdrop] !== staticString) ) ) {
self.hide(); relatedTarget = null;
e.preventDefault();
setTimeout(function(){ e.defaultPrevented = false; }, 50);
}
};

Expand Down Expand Up @@ -982,6 +985,7 @@
}, this[duration]/2);
setTimeout( function() {
open = self.open = true;
setFocus(modal);
bootstrapCustomEvent.call(modal, shownEvent, component, relatedTarget);
}, this[duration]);
};
Expand All @@ -1008,8 +1012,9 @@
}, this[duration]/2);

setTimeout( function() {
if (!getElementsByClassName(document,component+' in')[0]) { removeOverlay(); }
if (!getElementsByClassName(document,component+' '+showClass)[0]) { removeOverlay(); }
open = self.open = false;
element && (setFocus(element));
bootstrapCustomEvent.call(modal, hiddenEvent, component);
}, this[duration]);
};
Expand Down
2 changes: 1 addition & 1 deletion dist-v4/bootstrap-native.min.js

Large diffs are not rendered by default.

22 changes: 13 additions & 9 deletions dist/bootstrap-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,19 @@
mouseHover = ('onmouseleave' in document) ? [ 'mouseenter', 'mouseleave'] : [ 'mouseover', 'mouseout' ],
tipPositions = /\b(top|bottom|left|top)+/,

// class manipulation, since 1.2.0 requires polyfill.js
// set new focus element since 2.0.3
setFocus = function(element){
element.focus ? element.focus() : element.setActive();
},

// class manipulation, since 2.0.0 requires polyfill.js
addClass = function(element,classNAME) {
element.classList.add(classNAME);
},
removeClass = function(element,classNAME) {
element.classList.remove(classNAME);
},
hasClass = function(element,classNAME){ // since 1.2.0
hasClass = function(element,classNAME){ // since 2.0.0
return element.classList.contains(classNAME);
},

Expand Down Expand Up @@ -188,8 +193,7 @@
// reference a live collection of the DOM
AllDOMElements = document[getElementsByTagName]('*'),

/* Init DATA API
--------------*/
// Init DATA API
initializeDataAPI = function( component, constructor, dataAttribute, collection ){
var lookUp = collection && collection[length] ? collection : AllDOMElements;
for (var i=0; i < lookUp[length]; i++) {
Expand Down Expand Up @@ -1082,12 +1086,11 @@
clickHandler = function(e) {
var clickTarget = e[target];
clickTarget = clickTarget[hasAttribute](dataTarget) || clickTarget[hasAttribute]('href') ? clickTarget : clickTarget[parentNode];
if ( !e.defaultPrevented && !open && clickTarget === element && !hasClass(modal,inClass) ) {
if ( !open && clickTarget === element && !hasClass(modal,inClass) ) {
modal.modalTrigger = element;
relatedTarget = element;
self.show();
e.preventDefault();
setTimeout(function(){ e.defaultPrevented = false; }, 50);
}
},
keyHandler = function(e) {
Expand All @@ -1098,12 +1101,11 @@
},
dismissHandler = function(e) {
var clickTarget = e[target];
if ( !e.defaultPrevented && open && (clickTarget[parentNode][getAttribute](dataDismiss) === component
if ( open && (clickTarget[parentNode][getAttribute](dataDismiss) === component
|| clickTarget[getAttribute](dataDismiss) === component
|| (clickTarget === modal && self[backdrop] !== staticString) ) ) {
self.hide(); relatedTarget = null;
e.preventDefault();
setTimeout(function(){ e.defaultPrevented = false; }, 50);
}
};

Expand Down Expand Up @@ -1142,6 +1144,7 @@
}, this[duration]/2);
setTimeout( function() {
open = self.open = true;
setFocus(modal);
bootstrapCustomEvent.call(modal, shownEvent, component, relatedTarget);
}, this[duration]);
};
Expand All @@ -1168,8 +1171,9 @@
}, this[duration]/2);

setTimeout( function() {
if (!getElementsByClassName(document,component+' in')[0]) { removeOverlay(); }
if (!getElementsByClassName(document,component+' '+inClass)[0]) { removeOverlay(); }
open = self.open = false;
element && (setFocus(element));
bootstrapCustomEvent.call(modal, hiddenEvent, component);
}, this[duration]);
};
Expand Down
2 changes: 1 addition & 1 deletion dist/bootstrap-native.min.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions lib/V3/modal-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ var Modal = function(element, options) { // element can be the modal/triggering
clickHandler = function(e) {
var clickTarget = e[target];
clickTarget = clickTarget[hasAttribute](dataTarget) || clickTarget[hasAttribute]('href') ? clickTarget : clickTarget[parentNode];
if ( !e.defaultPrevented && !open && clickTarget === element && !hasClass(modal,inClass) ) {
if ( !open && clickTarget === element && !hasClass(modal,inClass) ) {
modal.modalTrigger = element;
relatedTarget = element;
self.show();
e.preventDefault();
setTimeout(function(){ e.defaultPrevented = false; }, 50);
}
},
keyHandler = function(e) {
Expand All @@ -128,12 +127,11 @@ var Modal = function(element, options) { // element can be the modal/triggering
},
dismissHandler = function(e) {
var clickTarget = e[target];
if ( !e.defaultPrevented && open && (clickTarget[parentNode][getAttribute](dataDismiss) === component
if ( open && (clickTarget[parentNode][getAttribute](dataDismiss) === component
|| clickTarget[getAttribute](dataDismiss) === component
|| (clickTarget === modal && self[backdrop] !== staticString) ) ) {
self.hide(); relatedTarget = null;
e.preventDefault();
setTimeout(function(){ e.defaultPrevented = false; }, 50);
}
};

Expand Down Expand Up @@ -172,6 +170,7 @@ var Modal = function(element, options) { // element can be the modal/triggering
}, this[duration]/2);
setTimeout( function() {
open = self.open = true;
setFocus(modal);
bootstrapCustomEvent.call(modal, shownEvent, component, relatedTarget);
}, this[duration]);
};
Expand All @@ -198,8 +197,9 @@ var Modal = function(element, options) { // element can be the modal/triggering
}, this[duration]/2);

setTimeout( function() {
if (!getElementsByClassName(document,component+' in')[0]) { removeOverlay(); }
if (!getElementsByClassName(document,component+' '+inClass)[0]) { removeOverlay(); }
open = self.open = false;
element && (setFocus(element));
bootstrapCustomEvent.call(modal, hiddenEvent, component);
}, this[duration]);
};
Expand Down
12 changes: 8 additions & 4 deletions lib/V3/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,19 @@ var globalObject = typeof global !== 'undefined' ? global : this||window,
mouseHover = ('onmouseleave' in document) ? [ 'mouseenter', 'mouseleave'] : [ 'mouseover', 'mouseout' ],
tipPositions = /\b(top|bottom|left|top)+/,

// class manipulation, since 1.2.0 requires polyfill.js
// set new focus element since 2.0.3
setFocus = function(element){
element.focus ? element.focus() : element.setActive();
},

// class manipulation, since 2.0.0 requires polyfill.js
addClass = function(element,classNAME) {
element.classList.add(classNAME);
},
removeClass = function(element,classNAME) {
element.classList.remove(classNAME);
},
hasClass = function(element,classNAME){ // since 1.2.0
hasClass = function(element,classNAME){ // since 2.0.0
return element.classList.contains(classNAME);
},

Expand Down Expand Up @@ -164,8 +169,7 @@ var globalObject = typeof global !== 'undefined' ? global : this||window,
// reference a live collection of the DOM
AllDOMElements = document[getElementsByTagName]('*'),

/* Init DATA API
--------------*/
// Init DATA API
initializeDataAPI = function( component, constructor, dataAttribute, collection ){
var lookUp = collection && collection[length] ? collection : AllDOMElements;
for (var i=0; i < lookUp[length]; i++) {
Expand Down
10 changes: 5 additions & 5 deletions lib/V4/modal-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,11 @@ var Modal = function(element, options) { // element can be the modal/triggering
clickHandler = function(e) {
var clickTarget = e[target];
clickTarget = clickTarget[hasAttribute](dataTarget) || clickTarget[hasAttribute]('href') ? clickTarget : clickTarget[parentNode];
if ( !e.defaultPrevented && !open && clickTarget === element && !hasClass(modal,showClass) ) {
if ( !open && clickTarget === element && !hasClass(modal,showClass) ) {
modal.modalTrigger = element;
relatedTarget = element;
self.show();
e.preventDefault();
setTimeout(function(){ e.defaultPrevented = false; }, 50);
}
},
keyHandler = function(e) {
Expand All @@ -128,12 +127,11 @@ var Modal = function(element, options) { // element can be the modal/triggering
},
dismissHandler = function(e) {
var clickTarget = e[target];
if ( !e.defaultPrevented && open && (clickTarget[parentNode][getAttribute](dataDismiss) === component
if ( open && (clickTarget[parentNode][getAttribute](dataDismiss) === component
|| clickTarget[getAttribute](dataDismiss) === component
|| (clickTarget === modal && self[backdrop] !== staticString) ) ) {
self.hide(); relatedTarget = null;
e.preventDefault();
setTimeout(function(){ e.defaultPrevented = false; }, 50);
}
};

Expand Down Expand Up @@ -172,6 +170,7 @@ var Modal = function(element, options) { // element can be the modal/triggering
}, this[duration]/2);
setTimeout( function() {
open = self.open = true;
setFocus(modal);
bootstrapCustomEvent.call(modal, shownEvent, component, relatedTarget);
}, this[duration]);
};
Expand All @@ -198,8 +197,9 @@ var Modal = function(element, options) { // element can be the modal/triggering
}, this[duration]/2);

setTimeout( function() {
if (!getElementsByClassName(document,component+' in')[0]) { removeOverlay(); }
if (!getElementsByClassName(document,component+' '+showClass)[0]) { removeOverlay(); }
open = self.open = false;
element && (setFocus(element));
bootstrapCustomEvent.call(modal, hiddenEvent, component);
}, this[duration]);
};
Expand Down
9 changes: 7 additions & 2 deletions lib/V4/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,19 @@ var globalObject = typeof global !== 'undefined' ? global : this||window,
mouseHover = ('onmouseleave' in document) ? [ 'mouseenter', 'mouseleave'] : [ 'mouseover', 'mouseout' ],
tipPositions = /\b(top|bottom|left|top)+/,

// class manipulation, since 1.2.0 requires polyfill.js
// set new focus element since 2.0.3
setFocus = function(element){
element.focus ? element.focus() : element.setActive();
},

// class manipulation, since 2.0.0 requires polyfill.js
addClass = function(element,classNAME) {
element.classList.add(classNAME);
},
removeClass = function(element,classNAME) {
element.classList.remove(classNAME);
},
hasClass = function(element,classNAME){ // since 1.2.0
hasClass = function(element,classNAME){ // since 2.0.0
return element.classList.contains(classNAME);
},

Expand Down

0 comments on commit 29fed4a

Please sign in to comment.