tab.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. /*!
  2. * # Semantic UI 2.2.6 - Tab
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Released under the MIT license
  7. * http://opensource.org/licenses/MIT
  8. *
  9. */
  10. ;(function ($, window, document, undefined) {
  11. "use strict";
  12. window = (typeof window != 'undefined' && window.Math == Math)
  13. ? window
  14. : (typeof self != 'undefined' && self.Math == Math)
  15. ? self
  16. : Function('return this')()
  17. ;
  18. $.fn.tab = function(parameters) {
  19. var
  20. // use window context if none specified
  21. $allModules = $.isFunction(this)
  22. ? $(window)
  23. : $(this),
  24. moduleSelector = $allModules.selector || '',
  25. time = new Date().getTime(),
  26. performance = [],
  27. query = arguments[0],
  28. methodInvoked = (typeof query == 'string'),
  29. queryArguments = [].slice.call(arguments, 1),
  30. initializedHistory = false,
  31. returnedValue
  32. ;
  33. $allModules
  34. .each(function() {
  35. var
  36. settings = ( $.isPlainObject(parameters) )
  37. ? $.extend(true, {}, $.fn.tab.settings, parameters)
  38. : $.extend({}, $.fn.tab.settings),
  39. className = settings.className,
  40. metadata = settings.metadata,
  41. selector = settings.selector,
  42. error = settings.error,
  43. eventNamespace = '.' + settings.namespace,
  44. moduleNamespace = 'module-' + settings.namespace,
  45. $module = $(this),
  46. $context,
  47. $tabs,
  48. cache = {},
  49. firstLoad = true,
  50. recursionDepth = 0,
  51. element = this,
  52. instance = $module.data(moduleNamespace),
  53. activeTabPath,
  54. parameterArray,
  55. module,
  56. historyEvent
  57. ;
  58. module = {
  59. initialize: function() {
  60. module.debug('Initializing tab menu item', $module);
  61. module.fix.callbacks();
  62. module.determineTabs();
  63. module.debug('Determining tabs', settings.context, $tabs);
  64. // set up automatic routing
  65. if(settings.auto) {
  66. module.set.auto();
  67. }
  68. module.bind.events();
  69. if(settings.history && !initializedHistory) {
  70. module.initializeHistory();
  71. initializedHistory = true;
  72. }
  73. module.instantiate();
  74. },
  75. instantiate: function () {
  76. module.verbose('Storing instance of module', module);
  77. instance = module;
  78. $module
  79. .data(moduleNamespace, module)
  80. ;
  81. },
  82. destroy: function() {
  83. module.debug('Destroying tabs', $module);
  84. $module
  85. .removeData(moduleNamespace)
  86. .off(eventNamespace)
  87. ;
  88. },
  89. bind: {
  90. events: function() {
  91. // if using $.tab don't add events
  92. if( !$.isWindow( element ) ) {
  93. module.debug('Attaching tab activation events to element', $module);
  94. $module
  95. .on('click' + eventNamespace, module.event.click)
  96. ;
  97. }
  98. }
  99. },
  100. determineTabs: function() {
  101. var
  102. $reference
  103. ;
  104. // determine tab context
  105. if(settings.context === 'parent') {
  106. if($module.closest(selector.ui).length > 0) {
  107. $reference = $module.closest(selector.ui);
  108. module.verbose('Using closest UI element as parent', $reference);
  109. }
  110. else {
  111. $reference = $module;
  112. }
  113. $context = $reference.parent();
  114. module.verbose('Determined parent element for creating context', $context);
  115. }
  116. else if(settings.context) {
  117. $context = $(settings.context);
  118. module.verbose('Using selector for tab context', settings.context, $context);
  119. }
  120. else {
  121. $context = $('body');
  122. }
  123. // find tabs
  124. if(settings.childrenOnly) {
  125. $tabs = $context.children(selector.tabs);
  126. module.debug('Searching tab context children for tabs', $context, $tabs);
  127. }
  128. else {
  129. $tabs = $context.find(selector.tabs);
  130. module.debug('Searching tab context for tabs', $context, $tabs);
  131. }
  132. },
  133. fix: {
  134. callbacks: function() {
  135. if( $.isPlainObject(parameters) && (parameters.onTabLoad || parameters.onTabInit) ) {
  136. if(parameters.onTabLoad) {
  137. parameters.onLoad = parameters.onTabLoad;
  138. delete parameters.onTabLoad;
  139. module.error(error.legacyLoad, parameters.onLoad);
  140. }
  141. if(parameters.onTabInit) {
  142. parameters.onFirstLoad = parameters.onTabInit;
  143. delete parameters.onTabInit;
  144. module.error(error.legacyInit, parameters.onFirstLoad);
  145. }
  146. settings = $.extend(true, {}, $.fn.tab.settings, parameters);
  147. }
  148. }
  149. },
  150. initializeHistory: function() {
  151. module.debug('Initializing page state');
  152. if( $.address === undefined ) {
  153. module.error(error.state);
  154. return false;
  155. }
  156. else {
  157. if(settings.historyType == 'state') {
  158. module.debug('Using HTML5 to manage state');
  159. if(settings.path !== false) {
  160. $.address
  161. .history(true)
  162. .state(settings.path)
  163. ;
  164. }
  165. else {
  166. module.error(error.path);
  167. return false;
  168. }
  169. }
  170. $.address
  171. .bind('change', module.event.history.change)
  172. ;
  173. }
  174. },
  175. event: {
  176. click: function(event) {
  177. var
  178. tabPath = $(this).data(metadata.tab)
  179. ;
  180. if(tabPath !== undefined) {
  181. if(settings.history) {
  182. module.verbose('Updating page state', event);
  183. $.address.value(tabPath);
  184. }
  185. else {
  186. module.verbose('Changing tab', event);
  187. module.changeTab(tabPath);
  188. }
  189. event.preventDefault();
  190. }
  191. else {
  192. module.debug('No tab specified');
  193. }
  194. },
  195. history: {
  196. change: function(event) {
  197. var
  198. tabPath = event.pathNames.join('/') || module.get.initialPath(),
  199. pageTitle = settings.templates.determineTitle(tabPath) || false
  200. ;
  201. module.performance.display();
  202. module.debug('History change event', tabPath, event);
  203. historyEvent = event;
  204. if(tabPath !== undefined) {
  205. module.changeTab(tabPath);
  206. }
  207. if(pageTitle) {
  208. $.address.title(pageTitle);
  209. }
  210. }
  211. }
  212. },
  213. refresh: function() {
  214. if(activeTabPath) {
  215. module.debug('Refreshing tab', activeTabPath);
  216. module.changeTab(activeTabPath);
  217. }
  218. },
  219. cache: {
  220. read: function(cacheKey) {
  221. return (cacheKey !== undefined)
  222. ? cache[cacheKey]
  223. : false
  224. ;
  225. },
  226. add: function(cacheKey, content) {
  227. cacheKey = cacheKey || activeTabPath;
  228. module.debug('Adding cached content for', cacheKey);
  229. cache[cacheKey] = content;
  230. },
  231. remove: function(cacheKey) {
  232. cacheKey = cacheKey || activeTabPath;
  233. module.debug('Removing cached content for', cacheKey);
  234. delete cache[cacheKey];
  235. }
  236. },
  237. set: {
  238. auto: function() {
  239. var
  240. url = (typeof settings.path == 'string')
  241. ? settings.path.replace(/\/$/, '') + '/{$tab}'
  242. : '/{$tab}'
  243. ;
  244. module.verbose('Setting up automatic tab retrieval from server', url);
  245. if($.isPlainObject(settings.apiSettings)) {
  246. settings.apiSettings.url = url;
  247. }
  248. else {
  249. settings.apiSettings = {
  250. url: url
  251. };
  252. }
  253. },
  254. loading: function(tabPath) {
  255. var
  256. $tab = module.get.tabElement(tabPath),
  257. isLoading = $tab.hasClass(className.loading)
  258. ;
  259. if(!isLoading) {
  260. module.verbose('Setting loading state for', $tab);
  261. $tab
  262. .addClass(className.loading)
  263. .siblings($tabs)
  264. .removeClass(className.active + ' ' + className.loading)
  265. ;
  266. if($tab.length > 0) {
  267. settings.onRequest.call($tab[0], tabPath);
  268. }
  269. }
  270. },
  271. state: function(state) {
  272. $.address.value(state);
  273. }
  274. },
  275. changeTab: function(tabPath) {
  276. var
  277. pushStateAvailable = (window.history && window.history.pushState),
  278. shouldIgnoreLoad = (pushStateAvailable && settings.ignoreFirstLoad && firstLoad),
  279. remoteContent = (settings.auto || $.isPlainObject(settings.apiSettings) ),
  280. // only add default path if not remote content
  281. pathArray = (remoteContent && !shouldIgnoreLoad)
  282. ? module.utilities.pathToArray(tabPath)
  283. : module.get.defaultPathArray(tabPath)
  284. ;
  285. tabPath = module.utilities.arrayToPath(pathArray);
  286. $.each(pathArray, function(index, tab) {
  287. var
  288. currentPathArray = pathArray.slice(0, index + 1),
  289. currentPath = module.utilities.arrayToPath(currentPathArray),
  290. isTab = module.is.tab(currentPath),
  291. isLastIndex = (index + 1 == pathArray.length),
  292. $tab = module.get.tabElement(currentPath),
  293. $anchor,
  294. nextPathArray,
  295. nextPath,
  296. isLastTab
  297. ;
  298. module.verbose('Looking for tab', tab);
  299. if(isTab) {
  300. module.verbose('Tab was found', tab);
  301. // scope up
  302. activeTabPath = currentPath;
  303. parameterArray = module.utilities.filterArray(pathArray, currentPathArray);
  304. if(isLastIndex) {
  305. isLastTab = true;
  306. }
  307. else {
  308. nextPathArray = pathArray.slice(0, index + 2);
  309. nextPath = module.utilities.arrayToPath(nextPathArray);
  310. isLastTab = ( !module.is.tab(nextPath) );
  311. if(isLastTab) {
  312. module.verbose('Tab parameters found', nextPathArray);
  313. }
  314. }
  315. if(isLastTab && remoteContent) {
  316. if(!shouldIgnoreLoad) {
  317. module.activate.navigation(currentPath);
  318. module.fetch.content(currentPath, tabPath);
  319. }
  320. else {
  321. module.debug('Ignoring remote content on first tab load', currentPath);
  322. firstLoad = false;
  323. module.cache.add(tabPath, $tab.html());
  324. module.activate.all(currentPath);
  325. settings.onFirstLoad.call($tab[0], currentPath, parameterArray, historyEvent);
  326. settings.onLoad.call($tab[0], currentPath, parameterArray, historyEvent);
  327. }
  328. return false;
  329. }
  330. else {
  331. module.debug('Opened local tab', currentPath);
  332. module.activate.all(currentPath);
  333. if( !module.cache.read(currentPath) ) {
  334. module.cache.add(currentPath, true);
  335. module.debug('First time tab loaded calling tab init');
  336. settings.onFirstLoad.call($tab[0], currentPath, parameterArray, historyEvent);
  337. }
  338. settings.onLoad.call($tab[0], currentPath, parameterArray, historyEvent);
  339. }
  340. }
  341. else if(tabPath.search('/') == -1 && tabPath !== '') {
  342. // look for in page anchor
  343. $anchor = $('#' + tabPath + ', a[name="' + tabPath + '"]');
  344. currentPath = $anchor.closest('[data-tab]').data(metadata.tab);
  345. $tab = module.get.tabElement(currentPath);
  346. // if anchor exists use parent tab
  347. if($anchor && $anchor.length > 0 && currentPath) {
  348. module.debug('Anchor link used, opening parent tab', $tab, $anchor);
  349. if( !$tab.hasClass(className.active) ) {
  350. setTimeout(function() {
  351. module.scrollTo($anchor);
  352. }, 0);
  353. }
  354. module.activate.all(currentPath);
  355. if( !module.cache.read(currentPath) ) {
  356. module.cache.add(currentPath, true);
  357. module.debug('First time tab loaded calling tab init');
  358. settings.onFirstLoad.call($tab[0], currentPath, parameterArray, historyEvent);
  359. }
  360. settings.onLoad.call($tab[0], currentPath, parameterArray, historyEvent);
  361. return false;
  362. }
  363. }
  364. else {
  365. module.error(error.missingTab, $module, $context, currentPath);
  366. return false;
  367. }
  368. });
  369. },
  370. scrollTo: function($element) {
  371. var
  372. scrollOffset = ($element && $element.length > 0)
  373. ? $element.offset().top
  374. : false
  375. ;
  376. if(scrollOffset !== false) {
  377. module.debug('Forcing scroll to an in-page link in a hidden tab', scrollOffset, $element);
  378. $(document).scrollTop(scrollOffset);
  379. }
  380. },
  381. update: {
  382. content: function(tabPath, html, evaluateScripts) {
  383. var
  384. $tab = module.get.tabElement(tabPath),
  385. tab = $tab[0]
  386. ;
  387. evaluateScripts = (evaluateScripts !== undefined)
  388. ? evaluateScripts
  389. : settings.evaluateScripts
  390. ;
  391. if(typeof settings.cacheType == 'string' && settings.cacheType.toLowerCase() == 'dom' && typeof html !== 'string') {
  392. $tab
  393. .empty()
  394. .append($(html).clone(true))
  395. ;
  396. }
  397. else {
  398. if(evaluateScripts) {
  399. module.debug('Updating HTML and evaluating inline scripts', tabPath, html);
  400. $tab.html(html);
  401. }
  402. else {
  403. module.debug('Updating HTML', tabPath, html);
  404. tab.innerHTML = html;
  405. }
  406. }
  407. }
  408. },
  409. fetch: {
  410. content: function(tabPath, fullTabPath) {
  411. var
  412. $tab = module.get.tabElement(tabPath),
  413. apiSettings = {
  414. dataType : 'html',
  415. encodeParameters : false,
  416. on : 'now',
  417. cache : settings.alwaysRefresh,
  418. headers : {
  419. 'X-Remote': true
  420. },
  421. onSuccess : function(response) {
  422. if(settings.cacheType == 'response') {
  423. module.cache.add(fullTabPath, response);
  424. }
  425. module.update.content(tabPath, response);
  426. if(tabPath == activeTabPath) {
  427. module.debug('Content loaded', tabPath);
  428. module.activate.tab(tabPath);
  429. }
  430. else {
  431. module.debug('Content loaded in background', tabPath);
  432. }
  433. settings.onFirstLoad.call($tab[0], tabPath, parameterArray, historyEvent);
  434. settings.onLoad.call($tab[0], tabPath, parameterArray, historyEvent);
  435. if(typeof settings.cacheType == 'string' && settings.cacheType.toLowerCase() == 'dom' && $tab.children().length > 0) {
  436. setTimeout(function() {
  437. var
  438. $clone = $tab.children().clone(true)
  439. ;
  440. $clone = $clone.not('script');
  441. module.cache.add(fullTabPath, $clone);
  442. }, 0);
  443. }
  444. else {
  445. module.cache.add(fullTabPath, $tab.html());
  446. }
  447. },
  448. urlData: {
  449. tab: fullTabPath
  450. }
  451. },
  452. request = $tab.api('get request') || false,
  453. existingRequest = ( request && request.state() === 'pending' ),
  454. requestSettings,
  455. cachedContent
  456. ;
  457. fullTabPath = fullTabPath || tabPath;
  458. cachedContent = module.cache.read(fullTabPath);
  459. if(settings.cache && cachedContent) {
  460. module.activate.tab(tabPath);
  461. module.debug('Adding cached content', fullTabPath);
  462. if(settings.evaluateScripts == 'once') {
  463. module.update.content(tabPath, cachedContent, false);
  464. }
  465. else {
  466. module.update.content(tabPath, cachedContent);
  467. }
  468. settings.onLoad.call($tab[0], tabPath, parameterArray, historyEvent);
  469. }
  470. else if(existingRequest) {
  471. module.set.loading(tabPath);
  472. module.debug('Content is already loading', fullTabPath);
  473. }
  474. else if($.api !== undefined) {
  475. requestSettings = $.extend(true, {}, settings.apiSettings, apiSettings);
  476. module.debug('Retrieving remote content', fullTabPath, requestSettings);
  477. module.set.loading(tabPath);
  478. $tab.api(requestSettings);
  479. }
  480. else {
  481. module.error(error.api);
  482. }
  483. }
  484. },
  485. activate: {
  486. all: function(tabPath) {
  487. module.activate.tab(tabPath);
  488. module.activate.navigation(tabPath);
  489. },
  490. tab: function(tabPath) {
  491. var
  492. $tab = module.get.tabElement(tabPath),
  493. $deactiveTabs = (settings.deactivate == 'siblings')
  494. ? $tab.siblings($tabs)
  495. : $tabs.not($tab),
  496. isActive = $tab.hasClass(className.active)
  497. ;
  498. module.verbose('Showing tab content for', $tab);
  499. if(!isActive) {
  500. $tab
  501. .addClass(className.active)
  502. ;
  503. $deactiveTabs
  504. .removeClass(className.active + ' ' + className.loading)
  505. ;
  506. if($tab.length > 0) {
  507. settings.onVisible.call($tab[0], tabPath);
  508. }
  509. }
  510. },
  511. navigation: function(tabPath) {
  512. var
  513. $navigation = module.get.navElement(tabPath),
  514. $deactiveNavigation = (settings.deactivate == 'siblings')
  515. ? $navigation.siblings($allModules)
  516. : $allModules.not($navigation),
  517. isActive = $navigation.hasClass(className.active)
  518. ;
  519. module.verbose('Activating tab navigation for', $navigation, tabPath);
  520. if(!isActive) {
  521. $navigation
  522. .addClass(className.active)
  523. ;
  524. $deactiveNavigation
  525. .removeClass(className.active + ' ' + className.loading)
  526. ;
  527. }
  528. }
  529. },
  530. deactivate: {
  531. all: function() {
  532. module.deactivate.navigation();
  533. module.deactivate.tabs();
  534. },
  535. navigation: function() {
  536. $allModules
  537. .removeClass(className.active)
  538. ;
  539. },
  540. tabs: function() {
  541. $tabs
  542. .removeClass(className.active + ' ' + className.loading)
  543. ;
  544. }
  545. },
  546. is: {
  547. tab: function(tabName) {
  548. return (tabName !== undefined)
  549. ? ( module.get.tabElement(tabName).length > 0 )
  550. : false
  551. ;
  552. }
  553. },
  554. get: {
  555. initialPath: function() {
  556. return $allModules.eq(0).data(metadata.tab) || $tabs.eq(0).data(metadata.tab);
  557. },
  558. path: function() {
  559. return $.address.value();
  560. },
  561. // adds default tabs to tab path
  562. defaultPathArray: function(tabPath) {
  563. return module.utilities.pathToArray( module.get.defaultPath(tabPath) );
  564. },
  565. defaultPath: function(tabPath) {
  566. var
  567. $defaultNav = $allModules.filter('[data-' + metadata.tab + '^="' + tabPath + '/"]').eq(0),
  568. defaultTab = $defaultNav.data(metadata.tab) || false
  569. ;
  570. if( defaultTab ) {
  571. module.debug('Found default tab', defaultTab);
  572. if(recursionDepth < settings.maxDepth) {
  573. recursionDepth++;
  574. return module.get.defaultPath(defaultTab);
  575. }
  576. module.error(error.recursion);
  577. }
  578. else {
  579. module.debug('No default tabs found for', tabPath, $tabs);
  580. }
  581. recursionDepth = 0;
  582. return tabPath;
  583. },
  584. navElement: function(tabPath) {
  585. tabPath = tabPath || activeTabPath;
  586. return $allModules.filter('[data-' + metadata.tab + '="' + tabPath + '"]');
  587. },
  588. tabElement: function(tabPath) {
  589. var
  590. $fullPathTab,
  591. $simplePathTab,
  592. tabPathArray,
  593. lastTab
  594. ;
  595. tabPath = tabPath || activeTabPath;
  596. tabPathArray = module.utilities.pathToArray(tabPath);
  597. lastTab = module.utilities.last(tabPathArray);
  598. $fullPathTab = $tabs.filter('[data-' + metadata.tab + '="' + tabPath + '"]');
  599. $simplePathTab = $tabs.filter('[data-' + metadata.tab + '="' + lastTab + '"]');
  600. return ($fullPathTab.length > 0)
  601. ? $fullPathTab
  602. : $simplePathTab
  603. ;
  604. },
  605. tab: function() {
  606. return activeTabPath;
  607. }
  608. },
  609. utilities: {
  610. filterArray: function(keepArray, removeArray) {
  611. return $.grep(keepArray, function(keepValue) {
  612. return ( $.inArray(keepValue, removeArray) == -1);
  613. });
  614. },
  615. last: function(array) {
  616. return $.isArray(array)
  617. ? array[ array.length - 1]
  618. : false
  619. ;
  620. },
  621. pathToArray: function(pathName) {
  622. if(pathName === undefined) {
  623. pathName = activeTabPath;
  624. }
  625. return typeof pathName == 'string'
  626. ? pathName.split('/')
  627. : [pathName]
  628. ;
  629. },
  630. arrayToPath: function(pathArray) {
  631. return $.isArray(pathArray)
  632. ? pathArray.join('/')
  633. : false
  634. ;
  635. }
  636. },
  637. setting: function(name, value) {
  638. module.debug('Changing setting', name, value);
  639. if( $.isPlainObject(name) ) {
  640. $.extend(true, settings, name);
  641. }
  642. else if(value !== undefined) {
  643. if($.isPlainObject(settings[name])) {
  644. $.extend(true, settings[name], value);
  645. }
  646. else {
  647. settings[name] = value;
  648. }
  649. }
  650. else {
  651. return settings[name];
  652. }
  653. },
  654. internal: function(name, value) {
  655. if( $.isPlainObject(name) ) {
  656. $.extend(true, module, name);
  657. }
  658. else if(value !== undefined) {
  659. module[name] = value;
  660. }
  661. else {
  662. return module[name];
  663. }
  664. },
  665. debug: function() {
  666. if(!settings.silent && settings.debug) {
  667. if(settings.performance) {
  668. module.performance.log(arguments);
  669. }
  670. else {
  671. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  672. module.debug.apply(console, arguments);
  673. }
  674. }
  675. },
  676. verbose: function() {
  677. if(!settings.silent && settings.verbose && settings.debug) {
  678. if(settings.performance) {
  679. module.performance.log(arguments);
  680. }
  681. else {
  682. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  683. module.verbose.apply(console, arguments);
  684. }
  685. }
  686. },
  687. error: function() {
  688. if(!settings.silent) {
  689. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  690. module.error.apply(console, arguments);
  691. }
  692. },
  693. performance: {
  694. log: function(message) {
  695. var
  696. currentTime,
  697. executionTime,
  698. previousTime
  699. ;
  700. if(settings.performance) {
  701. currentTime = new Date().getTime();
  702. previousTime = time || currentTime;
  703. executionTime = currentTime - previousTime;
  704. time = currentTime;
  705. performance.push({
  706. 'Name' : message[0],
  707. 'Arguments' : [].slice.call(message, 1) || '',
  708. 'Element' : element,
  709. 'Execution Time' : executionTime
  710. });
  711. }
  712. clearTimeout(module.performance.timer);
  713. module.performance.timer = setTimeout(module.performance.display, 500);
  714. },
  715. display: function() {
  716. var
  717. title = settings.name + ':',
  718. totalTime = 0
  719. ;
  720. time = false;
  721. clearTimeout(module.performance.timer);
  722. $.each(performance, function(index, data) {
  723. totalTime += data['Execution Time'];
  724. });
  725. title += ' ' + totalTime + 'ms';
  726. if(moduleSelector) {
  727. title += ' \'' + moduleSelector + '\'';
  728. }
  729. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  730. console.groupCollapsed(title);
  731. if(console.table) {
  732. console.table(performance);
  733. }
  734. else {
  735. $.each(performance, function(index, data) {
  736. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  737. });
  738. }
  739. console.groupEnd();
  740. }
  741. performance = [];
  742. }
  743. },
  744. invoke: function(query, passedArguments, context) {
  745. var
  746. object = instance,
  747. maxDepth,
  748. found,
  749. response
  750. ;
  751. passedArguments = passedArguments || queryArguments;
  752. context = element || context;
  753. if(typeof query == 'string' && object !== undefined) {
  754. query = query.split(/[\. ]/);
  755. maxDepth = query.length - 1;
  756. $.each(query, function(depth, value) {
  757. var camelCaseValue = (depth != maxDepth)
  758. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  759. : query
  760. ;
  761. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  762. object = object[camelCaseValue];
  763. }
  764. else if( object[camelCaseValue] !== undefined ) {
  765. found = object[camelCaseValue];
  766. return false;
  767. }
  768. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  769. object = object[value];
  770. }
  771. else if( object[value] !== undefined ) {
  772. found = object[value];
  773. return false;
  774. }
  775. else {
  776. module.error(error.method, query);
  777. return false;
  778. }
  779. });
  780. }
  781. if ( $.isFunction( found ) ) {
  782. response = found.apply(context, passedArguments);
  783. }
  784. else if(found !== undefined) {
  785. response = found;
  786. }
  787. if($.isArray(returnedValue)) {
  788. returnedValue.push(response);
  789. }
  790. else if(returnedValue !== undefined) {
  791. returnedValue = [returnedValue, response];
  792. }
  793. else if(response !== undefined) {
  794. returnedValue = response;
  795. }
  796. return found;
  797. }
  798. };
  799. if(methodInvoked) {
  800. if(instance === undefined) {
  801. module.initialize();
  802. }
  803. module.invoke(query);
  804. }
  805. else {
  806. if(instance !== undefined) {
  807. instance.invoke('destroy');
  808. }
  809. module.initialize();
  810. }
  811. })
  812. ;
  813. return (returnedValue !== undefined)
  814. ? returnedValue
  815. : this
  816. ;
  817. };
  818. // shortcut for tabbed content with no defined navigation
  819. $.tab = function() {
  820. $(window).tab.apply(this, arguments);
  821. };
  822. $.fn.tab.settings = {
  823. name : 'Tab',
  824. namespace : 'tab',
  825. silent : false,
  826. debug : false,
  827. verbose : false,
  828. performance : true,
  829. auto : false, // uses pjax style endpoints fetching content from same url with remote-content headers
  830. history : false, // use browser history
  831. historyType : 'hash', // #/ or html5 state
  832. path : false, // base path of url
  833. context : false, // specify a context that tabs must appear inside
  834. childrenOnly : false, // use only tabs that are children of context
  835. maxDepth : 25, // max depth a tab can be nested
  836. deactivate : 'siblings', // whether tabs should deactivate sibling menu elements or all elements initialized together
  837. alwaysRefresh : false, // load tab content new every tab click
  838. cache : true, // cache the content requests to pull locally
  839. cacheType : 'response', // Whether to cache exact response, or to html cache contents after scripts execute
  840. ignoreFirstLoad : false, // don't load remote content on first load
  841. apiSettings : false, // settings for api call
  842. evaluateScripts : 'once', // whether inline scripts should be parsed (true/false/once). Once will not re-evaluate on cached content
  843. onFirstLoad : function(tabPath, parameterArray, historyEvent) {}, // called first time loaded
  844. onLoad : function(tabPath, parameterArray, historyEvent) {}, // called on every load
  845. onVisible : function(tabPath, parameterArray, historyEvent) {}, // called every time tab visible
  846. onRequest : function(tabPath, parameterArray, historyEvent) {}, // called ever time a tab beings loading remote content
  847. templates : {
  848. determineTitle: function(tabArray) {} // returns page title for path
  849. },
  850. error: {
  851. api : 'You attempted to load content without API module',
  852. method : 'The method you called is not defined',
  853. missingTab : 'Activated tab cannot be found. Tabs are case-sensitive.',
  854. noContent : 'The tab you specified is missing a content url.',
  855. path : 'History enabled, but no path was specified',
  856. recursion : 'Max recursive depth reached',
  857. legacyInit : 'onTabInit has been renamed to onFirstLoad in 2.0, please adjust your code.',
  858. legacyLoad : 'onTabLoad has been renamed to onLoad in 2.0. Please adjust your code',
  859. state : 'History requires Asual\'s Address library <https://github.com/asual/jquery-address>'
  860. },
  861. metadata : {
  862. tab : 'tab',
  863. loaded : 'loaded',
  864. promise: 'promise'
  865. },
  866. className : {
  867. loading : 'loading',
  868. active : 'active'
  869. },
  870. selector : {
  871. tabs : '.ui.tab',
  872. ui : '.ui'
  873. }
  874. };
  875. })( jQuery, window, document );