Skip to content

Commit ebc0891

Browse files
committed
Enable multiple observers on the same CSS-classes.
Migrate from deprecated jQuery-APIs.
1 parent 9dad639 commit ebc0891

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

src/gmail.js

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,10 @@ var Gmail = function(localJQuery) {
17371737
$.each(api.tracker.dom_observers, function(act,config){
17381738
if(!$.isArray(config.class)) config.class = [config.class];
17391739
$.each(config.class, function(idx, className) {
1740-
api.tracker.dom_observer_map[className] = act;
1740+
if (!api.tracker.dom_observer_map[className]) {
1741+
api.tracker.dom_observer_map[className] = [];
1742+
}
1743+
api.tracker.dom_observer_map[className].push(act);
17411744
});
17421745
});
17431746
//console.log( "observer_config", api.tracker.dom_observers, "dom_observer_map", api.tracker.dom_observer_map);
@@ -1813,7 +1816,7 @@ var Gmail = function(localJQuery) {
18131816
// this listener will check every element inserted into the DOM
18141817
// for specified classes (as defined in api.tracker.dom_observers above) which indicate
18151818
// related actions which need triggering
1816-
$(window.document).bind("DOMNodeInserted", function(e) {
1819+
$(window.document).on("DOMNodeInserted", function(e) {
18171820
api.tools.insertion_observer(e.target, api.tracker.dom_observers, api.tracker.dom_observer_map);
18181821
});
18191822

@@ -1880,46 +1883,52 @@ var Gmail = function(localJQuery) {
18801883
var classes = cn.trim ? cn.trim().split(/\s+/) : [];
18811884
if(!classes.length) classes.push(""); // if no class, then check for anything observing nodes with no class
18821885
$.each(classes, function(idx, className) {
1883-
var observer = dom_observer_map[className];
1886+
var observers = dom_observer_map[className];
1887+
if (!observers) {
1888+
return;
1889+
}
18841890

1885-
// check if this is a defined observer, and callbacks are bound to that observer
1886-
if(observer && api.tracker.watchdog.dom[observer]) {
1887-
var element = $(target);
1888-
var config = dom_observers[observer];
1891+
for (var observer of observers) {
18891892

1890-
// if a config id specified for this observer, ensure it matches for this element
1891-
if(config.selector && !element.is(config.selector)) {
1892-
return;
1893-
}
1893+
// check if this is a defined observer, and callbacks are bound to that observer
1894+
if(observer && api.tracker.watchdog.dom[observer]) {
1895+
var element = $(target);
1896+
var config = dom_observers[observer];
18941897

1895-
// check for any defined sub_selector match - if not found, then this is not a match for this observer
1896-
// if found, then set the matching element to be the one that matches the sub_selector
1897-
if(config.sub_selector) {
1898-
element = element.find(config.sub_selector);
1899-
// console.log("checking for subselector", config.sub_selector, element);
1900-
}
1898+
// if a config id specified for this observer, ensure it matches for this element
1899+
if(config.selector && !element.is(config.selector)) {
1900+
return;
1901+
}
19011902

1902-
// if an element has been found, execute the observer handler (or if none defined, execute the callback)
1903-
if(element.length) {
1903+
// check for any defined sub_selector match - if not found, then this is not a match for this observer
1904+
// if found, then set the matching element to be the one that matches the sub_selector
1905+
if(config.sub_selector) {
1906+
element = element.find(config.sub_selector);
1907+
// console.log("checking for subselector", config.sub_selector, element);
1908+
}
19041909

1905-
var handler = config.handler ? config.handler : function(match, callback) { callback(match); };
1906-
// console.log( "inserted DOM: class match in watchdog",observer,api.tracker.watchdog.dom[observer] );
1907-
api.observe.trigger_dom(observer, element, handler);
1910+
// if an element has been found, execute the observer handler (or if none defined, execute the callback)
1911+
if(element.length) {
19081912

1909-
// if sub_observers are configured for this observer, bind a DOMNodeInsertion listener to this element & to check for specific elements being added to this particular element
1910-
if(config.sub_observers) {
1913+
var handler = config.handler ? config.handler : function(match, callback) { callback(match); };
1914+
// console.log( "inserted DOM: class match in watchdog",observer,api.tracker.watchdog.dom[observer] );
1915+
api.observe.trigger_dom(observer, element, handler);
19111916

1912-
// create observer_map for the sub_observers
1913-
var observer_map = {};
1914-
$.each(config.sub_observers, function(act,cfg){
1915-
observer_map[cfg.class] = act;
1916-
});
1917+
// if sub_observers are configured for this observer, bind a DOMNodeInsertion listener to this element & to check for specific elements being added to this particular element
1918+
if(config.sub_observers) {
19171919

1918-
// this listener will check every element inserted into the DOM below the current element
1919-
// and repeat this method, but specifically below the current element rather than the global DOM
1920-
element.bind("DOMNodeInserted", function(e) {
1921-
api.tools.insertion_observer(e.target, config.sub_observers, observer_map, "SUB ");
1922-
});
1920+
// create observer_map for the sub_observers
1921+
var observer_map = {};
1922+
$.each(config.sub_observers, function(act,cfg){
1923+
observer_map[cfg.class] = act;
1924+
});
1925+
1926+
// this listener will check every element inserted into the DOM below the current element
1927+
// and repeat this method, but specifically below the current element rather than the global DOM
1928+
element.on("DOMNodeInserted", function(e) {
1929+
api.tools.insertion_observer(e.target, config.sub_observers, observer_map, "SUB ");
1930+
});
1931+
}
19231932
}
19241933
}
19251934
}
@@ -2833,7 +2842,7 @@ var Gmail = function(localJQuery) {
28332842

28342843
center();
28352844

2836-
container.bind("DOMSubtreeModified", center);
2845+
container.on("DOMSubtreeModified", center);
28372846
$(window).resize(center);
28382847
};
28392848

0 commit comments

Comments
 (0)