From 1ae05d698bf2d4ec73d3d04aaf42e2785604a961 Mon Sep 17 00:00:00 2001 From: jlukic Date: Mon, 1 Jun 2015 17:06:55 -0400 Subject: [PATCH] Adds onVisible and onRequest callbacks, adds new 2.0 callback names onFirstLoad and onLoad, adds deprecation warnings for previous callbacks #1612 --- src/definitions/modules/tab.js | 128 ++++++++++++++++++++------------- 1 file changed, 79 insertions(+), 49 deletions(-) diff --git a/src/definitions/modules/tab.js b/src/definitions/modules/tab.js index 5e7c12315..3854411b9 100644 --- a/src/definitions/modules/tab.js +++ b/src/definitions/modules/tab.js @@ -251,6 +251,23 @@ $.fn.tab = function(parameters) { }; } }, + loading: function(tabPath) { + var + $tab = module.get.tabElement(tabPath), + isLoading = $tab.hasClass(className.loading) + ; + if(!isLoading) { + module.verbose('Setting loading state for', $tab); + $tab + .addClass(className.loading) + .siblings($tabs) + .removeClass(className.active + ' ' + className.loading) + ; + if($tab.length > 0) { + settings.onTabRequest.call($tab[0], tabPath); + } + } + }, state: function(state) { $.address.value(state); } @@ -286,7 +303,6 @@ $.fn.tab = function(parameters) { ; module.verbose('Looking for tab', tab); if(isTab) { - module.verbose('Tab was found', tab); // scope up activeTabPath = currentPath; @@ -306,15 +322,15 @@ $.fn.tab = function(parameters) { if(isLastTab && remoteContent) { if(!shouldIgnoreLoad) { module.activate.navigation(currentPath); - module.content.fetch(currentPath, tabPath); + module.fetch.content(currentPath, tabPath); } else { module.debug('Ignoring remote content on first tab load', currentPath); firstLoad = false; module.cache.add(tabPath, $tab.html()); module.activate.all(currentPath); - settings.onTabInit.call($tab, currentPath, parameterArray, historyEvent); - settings.onTabLoad.call($tab, currentPath, parameterArray, historyEvent); + settings.onTabInit.call($tab[0], currentPath, parameterArray, historyEvent); + settings.onTabLoad.call($tab[0], currentPath, parameterArray, historyEvent); } return false; } @@ -324,10 +340,11 @@ $.fn.tab = function(parameters) { if( !module.cache.read(currentPath) ) { module.cache.add(currentPath, true); module.debug('First time tab loaded calling tab init'); - settings.onTabInit.call($tab, currentPath, parameterArray, historyEvent); + settings.onTabInit.call($tab[0], currentPath, parameterArray, historyEvent); } - settings.onTabLoad.call($tab, currentPath, parameterArray, historyEvent); + settings.onTabLoad.call($tab[0], currentPath, parameterArray, historyEvent); } + } else if(tabPath.search('/') == -1 && tabPath !== '') { // look for in page anchor @@ -341,8 +358,9 @@ $.fn.tab = function(parameters) { if( !module.cache.read(currentPath) ) { module.cache.add(currentPath, true); module.debug('First time tab loaded calling tab init'); - settings.onTabInit.call($tab, currentPath, parameterArray, historyEvent); + settings.onTabInit.call($tab[0], currentPath, parameterArray, historyEvent); } + return false; } } @@ -353,9 +371,30 @@ $.fn.tab = function(parameters) { }); }, - content: { + update: { + content: function(tabPath, html, evaluateScripts) { + var + $tab = module.get.tabElement(tabPath), + tab = $tab[0] + ; + evaluateScripts = (evaluateScripts !== undefined) + ? evaluateScripts + : settings.evaluateScripts + ; + if(evaluateScripts) { + module.debug('Updating HTML and evaluating inline scripts', tabPath, html); + $tab.html(html); + } + else { + module.debug('Updating HTML', tabPath, html); + tab.innerHTML = html; + } + } + }, + + fetch: { - fetch: function(tabPath, fullTabPath) { + content: function(tabPath, fullTabPath) { var $tab = module.get.tabElement(tabPath), apiSettings = { @@ -363,7 +402,7 @@ $.fn.tab = function(parameters) { on : 'now', onSuccess : function(response) { module.cache.add(fullTabPath, response); - module.content.update(tabPath, response); + module.update.content(tabPath, response); if(tabPath == activeTabPath) { module.debug('Content loaded', tabPath); module.activate.tab(tabPath); @@ -371,8 +410,8 @@ $.fn.tab = function(parameters) { else { module.debug('Content loaded in background', tabPath); } - settings.onTabInit.call($tab, tabPath, parameterArray, historyEvent); - settings.onTabLoad.call($tab, tabPath, parameterArray, historyEvent); + settings.onTabInit.call($tab[0], tabPath, parameterArray, historyEvent); + settings.onTabLoad.call($tab[0], tabPath, parameterArray, historyEvent); }, urlData: { tab: fullTabPath @@ -388,21 +427,20 @@ $.fn.tab = function(parameters) { cachedContent = module.cache.read(fullTabPath); - module.activate.tab(tabPath); - if(settings.cache && cachedContent) { + module.activate.tab(tabPath); module.debug('Adding cached content', fullTabPath); if(settings.evaluateScripts == 'once') { - module.content.update(tabPath, cachedContent, false); + module.update.content(tabPath, cachedContent, false); } else { - module.content.update(tabPath, cachedContent); + module.update.content(tabPath, cachedContent); } - settings.onTabLoad.call($tab, tabPath, parameterArray, historyEvent); + settings.onTabLoad.call($tab[0], tabPath, parameterArray, historyEvent); } else if(existingRequest) { + module.set.loading(tabPath); module.debug('Content is already loading', fullTabPath); - $tab.addClass(className.loading); } else if($.api !== undefined) { requestSettings = $.extend(true, { @@ -416,25 +454,6 @@ $.fn.tab = function(parameters) { else { module.error(error.api); } - }, - - update: function(tabPath, html, evaluateScripts) { - var - $tab = module.get.tabElement(tabPath), - tab = $tab[0] - ; - evaluateScripts = (evaluateScripts !== undefined) - ? evaluateScripts - : settings.evaluateScripts - ; - if(evaluateScripts) { - module.debug('Updating HTML and evaluating inline scripts', tabPath, html); - $tab.html(html); - } - else { - module.debug('Updating HTML', tabPath, html); - tab.innerHTML = html; - } } }, @@ -445,25 +464,34 @@ $.fn.tab = function(parameters) { }, tab: function(tabPath) { var - $tab = module.get.tabElement(tabPath) + $tab = module.get.tabElement(tabPath), + isActive = $tab.hasClass(className.active) ; module.verbose('Showing tab content for', $tab); - $tab - .addClass(className.active) - .siblings($tabs) - .removeClass(className.active + ' ' + className.loading) - ; + if(!isActive) { + $tab + .addClass(className.active) + .siblings($tabs) + .removeClass(className.active + ' ' + className.loading) + ; + if($tab.length > 0) { + settings.onTabVisible.call($tab[0], tabPath); + } + } }, navigation: function(tabPath) { var - $navigation = module.get.navElement(tabPath) + $navigation = module.get.navElement(tabPath), + isActive = $navigation.hasClass(className.active) ; module.verbose('Activating tab navigation for', $navigation, tabPath); - $navigation - .addClass(className.active) - .siblings($allModules) - .removeClass(className.active + ' ' + className.loading) - ; + if(!isActive) { + $navigation + .addClass(className.active) + .siblings($allModules) + .removeClass(className.active + ' ' + className.loading) + ; + } } }, @@ -789,6 +817,8 @@ $.fn.tab.settings = { onTabInit : function(tabPath, parameterArray, historyEvent) {}, // called first time loaded onTabLoad : function(tabPath, parameterArray, historyEvent) {}, // called on every load + onTabVisible : function(tabPath, parameterArray, historyEvent) {}, // called every time tab visible + onTabRequest : function(tabPath, parameterArray, historyEvent) {}, // called ever time a tab beings loading remote content templates : { determineTitle: function(tabArray) {} // returns page title for path