Base for a static organization website

js_debug_toolbar.js 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /**
  2. * Debug Toolbar Javascript.
  3. *
  4. * Creates the DEBUGKIT namespace and provides methods for extending
  5. * and enhancing the Html toolbar. Includes library agnostic Event, Element,
  6. * Cookie and Request wrappers.
  7. *
  8. *
  9. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  10. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. *
  12. * Licensed under The MIT License
  13. * Redistributions of files must retain the above copyright notice.
  14. *
  15. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  16. * @link http://cakephp.org CakePHP(tm) Project
  17. * @since DebugKit 0.1
  18. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  19. */
  20. /* jshint jquery: true */
  21. var DEBUGKIT = function () {
  22. var undef;
  23. return {
  24. module: function (newmodule) {
  25. if (this[newmodule] === undef) {
  26. this[newmodule] = {};
  27. return this[newmodule];
  28. }
  29. return this[newmodule];
  30. }
  31. };
  32. }();
  33. (function () {
  34. function versionGTE(a, b) {
  35. var len = Math.min(a.length, b.length);
  36. for (var i = 0; i < len; i++) {
  37. a[i] = parseInt(a[i], 10);
  38. b[i] = parseInt(b[i], 10);
  39. if (a[i] > b[i]) {
  40. return true;
  41. }
  42. if (a[i] < b[i]) {
  43. return false;
  44. }
  45. }
  46. return true;
  47. }
  48. function versionWithin(version, min, max) {
  49. version = version.split('.');
  50. min = min.split('.');
  51. max = max.split('.');
  52. return versionGTE(version, min) && versionGTE(max, version);
  53. }
  54. function initOnReady() {
  55. DEBUGKIT.$(document).ready(function () {
  56. DEBUGKIT.registerModules(DEBUGKIT.$);
  57. DEBUGKIT.loader.init();
  58. });
  59. }
  60. // Push checking for jQuery at the end of the stack.
  61. // This will catch JS included at the bottom of a page.
  62. setTimeout(function() {
  63. // Look for existing jQuery that matches the requirements.
  64. if (window.jQuery && versionWithin(jQuery.fn.jquery, "1.8", "2.1")) {
  65. DEBUGKIT.$ = window.jQuery;
  66. initOnReady();
  67. } else {
  68. var req = new XMLHttpRequest();
  69. req.onload = function () {
  70. eval(this.responseText);
  71. // Restore both $ and jQuery to the original values.
  72. DEBUGKIT.$ = jQuery.noConflict(true);
  73. initOnReady();
  74. };
  75. req.open('get', window.DEBUGKIT_JQUERY_URL, true);
  76. req.send();
  77. }
  78. }, 0);
  79. })();
  80. DEBUGKIT.loader = function () {
  81. return {
  82. // List of methods to run on startup.
  83. _startup: [],
  84. // Register a new method to be run on dom ready.
  85. register: function (method) {
  86. this._startup.push(method);
  87. },
  88. init: function () {
  89. for (var i = 0, callback; callback = this._startup[i]; i++) {
  90. callback.init();
  91. }
  92. }
  93. };
  94. }();
  95. DEBUGKIT.registerModules = function($) {
  96. DEBUGKIT.module('sqlLog');
  97. DEBUGKIT.sqlLog = function () {
  98. return {
  99. init : function () {
  100. var sqlPanel = $('#sql_log-tab');
  101. var buttons = sqlPanel.find('input');
  102. // Button handling code for explain links.
  103. // Performs XHR request to get explain query.
  104. var handleButton = function (event) {
  105. event.preventDefault();
  106. var form = $(this.form),
  107. data = form.serialize(),
  108. dbName = form.find('input[name*=ds]').val() || 'default';
  109. var fetch = $.ajax({
  110. url: this.form.action,
  111. data: data,
  112. type: 'POST',
  113. success : function (response) {
  114. $('#sql-log-explain-' + dbName).html(response);
  115. },
  116. error : function () {
  117. alert('Could not fetch EXPLAIN for query.');
  118. }
  119. });
  120. };
  121. buttons.filter('.sql-explain-link').on('click', handleButton);
  122. }
  123. };
  124. }();
  125. DEBUGKIT.loader.register(DEBUGKIT.sqlLog);
  126. //
  127. // NOTE DEBUGKIT.Util.Element is Deprecated.
  128. //
  129. // Util module and Element utility class.
  130. DEBUGKIT.module('Util');
  131. DEBUGKIT.Util.Element = {
  132. // Test if an element is a name node.
  133. nodeName: function (element, name) {
  134. return element.nodeName && element.nodeName.toLowerCase() === name.toLowerCase();
  135. },
  136. // Return a boolean if the element has the classname
  137. hasClass: function (element, className) {
  138. if (!element.className) {
  139. return false;
  140. }
  141. return element.className.indexOf(className) > -1;
  142. },
  143. addClass: function (element, className) {
  144. if (!element.className) {
  145. element.className = className;
  146. return;
  147. }
  148. element.className = element.className.replace(/^(.*)$/, '$1 ' + className);
  149. },
  150. removeClass: function (element, className) {
  151. if (DEBUGKIT.Util.isArray(element)) {
  152. DEBUGKIT.Util.Collection.apply(element, function (element) {
  153. DEBUGKIT.Util.Element.removeClass(element, className);
  154. });
  155. }
  156. if (!element.className) {
  157. return false;
  158. }
  159. element.className = element.className.replace(new RegExp(' ?(' + className + ') ?'), '');
  160. },
  161. swapClass: function (element, removeClass, addClass) {
  162. if (!element.className) {
  163. return false;
  164. }
  165. element.className = element.className.replace(removeClass, addClass);
  166. },
  167. show: function (element) {
  168. element.style.display = 'block';
  169. },
  170. hide: function (element) {
  171. element.style.display = 'none';
  172. },
  173. // Go between hide() and show() depending on element.style.display
  174. toggle: function (element) {
  175. if (element.style.display === 'none') {
  176. this.show(element);
  177. return;
  178. }
  179. this.hide(element);
  180. },
  181. _walk: function (element, walk) {
  182. var sibling = element[walk];
  183. while (true) {
  184. if (sibling.nodeType == 1) {
  185. break;
  186. }
  187. sibling = sibling[walk];
  188. }
  189. return sibling;
  190. },
  191. getNext: function (element) {
  192. return this._walk(element, 'nextSibling');
  193. },
  194. getPrevious: function (element) {
  195. return this._walk(element, 'previousSibling');
  196. },
  197. // Get or set an element's height, omit value to get, add value (integer) to set.
  198. height: function (element, value) {
  199. // Get value
  200. if (value === undefined) {
  201. return parseInt(this.getStyle(element, 'height'), 10);
  202. }
  203. element.style.height = value + 'px';
  204. },
  205. // Gets the style in css format for property
  206. getStyle: function (element, property) {
  207. if (element.currentStyle) {
  208. property = property.replace(/-[a-z]/g, function (match) {
  209. return match.charAt(1).toUpperCase();
  210. });
  211. return element.currentStyle[property];
  212. }
  213. if (window.getComputedStyle) {
  214. return document.defaultView.getComputedStyle(element, null).getPropertyValue(property);
  215. }
  216. }
  217. };
  218. //
  219. // NOTE DEBUGKIT.Util.Collection is Deprecated.
  220. //
  221. DEBUGKIT.Util.Collection = {
  222. /**
  223. * Apply the passed function to each item in the collection.
  224. * The current element in the collection will be `this` in the callback
  225. * The callback is also passed the element and the index as arguments.
  226. * Optionally you can supply a binding parameter to change `this` in the callback.
  227. */
  228. apply: function (collection, callback, binding) {
  229. var name, thisVar, i = 0, len = collection.length;
  230. if (len === undefined) {
  231. for (name in collection) {
  232. thisVar = (binding === undefined) ? collection[name] : binding;
  233. callback.apply(thisVar, [collection[name], name]);
  234. }
  235. } else {
  236. for (; i < len; i++) {
  237. thisVar = (binding === undefined) ? collection[i] : binding;
  238. callback.apply(thisVar, [collection[i], i]);
  239. }
  240. }
  241. }
  242. };
  243. //
  244. // NOTE DEBUGKIT.Util.Event is Deprecated.
  245. //
  246. // Event binding
  247. DEBUGKIT.Util.Event = function () {
  248. var _listeners = {},
  249. _eventId = 0;
  250. var preventDefault = function () {
  251. this.returnValue = false;
  252. };
  253. var stopPropagation = function () {
  254. this.cancelBubble = true;
  255. };
  256. // Fixes IE's broken event object, adds in common methods + properties.
  257. var fixEvent = function (event) {
  258. if (!event.preventDefault) {
  259. event.preventDefault = preventDefault;
  260. }
  261. if (!event.stopPropagation) {
  262. event.stopPropagation = stopPropagation;
  263. }
  264. if (!event.target) {
  265. event.target = event.srcElement || document;
  266. }
  267. if (event.pageX === null && event.clientX !== null) {
  268. var doc = document.body;
  269. event.pageX = event.clientX + (doc.scrollLeft || 0) - (doc.clientLeft || 0);
  270. event.pageY = event.clientY + (doc.scrollTop || 0) - (doc.clientTop || 0);
  271. }
  272. return event;
  273. };
  274. return {
  275. // Bind an event listener of type to element, handler is your method.
  276. addEvent: function (element, type, handler, capture) {
  277. capture = (capture === undefined) ? false : capture;
  278. var callback = function (event) {
  279. event = fixEvent(event || window.event);
  280. handler.apply(element, [event]);
  281. };
  282. if (element.addEventListener) {
  283. element.addEventListener(type, callback, capture);
  284. } else if (element.attachEvent) {
  285. type = 'on' + type;
  286. element.attachEvent(type, callback);
  287. } else {
  288. type = 'on' + type;
  289. element[type] = callback;
  290. }
  291. _listeners[++_eventId] = {element: element, type: type, handler: callback};
  292. },
  293. // Destroy an event listener. requires the exact same function as was used for attaching
  294. // the event.
  295. removeEvent: function (element, type, handler) {
  296. if (element.removeEventListener) {
  297. element.removeEventListener(type, handler, false);
  298. } else if (element.detachEvent) {
  299. type = 'on' + type;
  300. element.detachEvent(type, handler);
  301. } else {
  302. type = 'on' + type;
  303. element[type] = null;
  304. }
  305. },
  306. // Bind an event to the DOMContentLoaded or other similar event.
  307. domready: function (callback) {
  308. if (document.addEventListener) {
  309. return document.addEventListener('DOMContentLoaded', callback, false);
  310. }
  311. if (document.all && !window.opera) {
  312. // Define a "blank" external JavaScript tag
  313. document.write(
  314. '<script type="text/javascript" id="__domreadywatcher" defer="defer" src="javascript:void(0)"><\/script>'
  315. );
  316. var contentloadtag = document.getElementById('__domreadywatcher');
  317. contentloadtag.onreadystatechange = function () {
  318. if (this.readyState === 'complete') {
  319. callback();
  320. }
  321. };
  322. contentloadtag = null;
  323. return;
  324. }
  325. if (/Webkit/i.test(navigator.userAgent)) {
  326. var _timer = setInterval(function () {
  327. if (/loaded|complete/.test(document.readyState)) {
  328. clearInterval(_timer);
  329. callback();
  330. }
  331. }, 10);
  332. }
  333. },
  334. // Unload all the events attached by DebugKit. Fix any memory leaks.
  335. unload: function () {
  336. var listener;
  337. for (var i in _listeners) {
  338. listener = _listeners[i];
  339. try {
  340. this.removeEvent(listener.element, listener.type, listener.handler);
  341. } catch (e) {}
  342. delete _listeners[i];
  343. }
  344. delete _listeners;
  345. }
  346. };
  347. }();
  348. // Cookie utility
  349. DEBUGKIT.Util.Cookie = function () {
  350. var cookieLife = 60;
  351. // Public methods
  352. return {
  353. /**
  354. * Write to cookie.
  355. *
  356. * @param [string] name Name of cookie to write.
  357. * @param [mixed] value Value to write to cookie.
  358. */
  359. write: function (name, value) {
  360. var date = new Date();
  361. date.setTime(date.getTime() + (cookieLife * 24 * 60 * 60 * 1000));
  362. var expires = '; expires=' + date.toGMTString();
  363. document.cookie = name + '=' + value + expires + '; path=/';
  364. return true;
  365. },
  366. /**
  367. * Read from the cookie.
  368. *
  369. * @param [string] name Name of cookie to read.
  370. */
  371. read: function (name) {
  372. name = name + '=';
  373. var cookieJar = document.cookie.split(';');
  374. var cookieJarLength = cookieJar.length;
  375. for (var i = 0; i < cookieJarLength; i++) {
  376. var chips = cookieJar[i];
  377. // Trim leading spaces
  378. while (chips.charAt(0) === ' ') {
  379. chips = chips.substring(1, chips.length);
  380. }
  381. if (chips.indexOf(name) === 0) {
  382. return chips.substring(name.length, chips.length);
  383. }
  384. }
  385. return false;
  386. },
  387. /**
  388. * Delete a cookie by name.
  389. *
  390. * @param [string] name of cookie to delete.
  391. */
  392. del: function (name) {
  393. var date = new Date();
  394. date.setFullYear(2000, 0, 1);
  395. var expires = ' ; expires=' + date.toGMTString();
  396. document.cookie = name + '=' + expires + '; path=/';
  397. }
  398. };
  399. }();
  400. //
  401. // NOTE DEBUGKIT.Util.merge is Deprecated.
  402. //
  403. /**
  404. * Object merge takes any number of arguments and glues them together.
  405. *
  406. * @param [Object] one first object
  407. * @return object
  408. */
  409. DEBUGKIT.Util.merge = function () {
  410. var out = {};
  411. var argumentsLength = arguments.length;
  412. for (var i = 0; i < argumentsLength; i++) {
  413. var current = arguments[i];
  414. for (var prop in current) {
  415. if (current[prop] !== undefined) {
  416. out[prop] = current[prop];
  417. }
  418. }
  419. }
  420. return out;
  421. };
  422. //
  423. // NOTE DEBUGKIT.Util.isArray is Deprecated.
  424. //
  425. /**
  426. * Check if the given object is an array.
  427. */
  428. DEBUGKIT.Util.isArray = function (test) {
  429. return Object.prototype.toString.call(test) === '[object Array]';
  430. };
  431. //
  432. // NOTE DEBUGKIT.Util.Request is Deprecated.
  433. //
  434. // Simple wrapper for XmlHttpRequest objects.
  435. DEBUGKIT.Util.Request = function (options) {
  436. var _defaults = {
  437. onComplete : function () {},
  438. onRequest : function () {},
  439. onFail : function () {},
  440. method : 'GET',
  441. async : true,
  442. headers : {
  443. 'X-Requested-With': 'XMLHttpRequest',
  444. 'Accept': 'text/javascript, text/html, application/xml, text/xml, */*'
  445. }
  446. };
  447. var self = this;
  448. this.options = DEBUGKIT.Util.merge(_defaults, options);
  449. this.options.method = this.options.method.toUpperCase();
  450. var ajax = this.createObj();
  451. this.transport = ajax;
  452. // Event assignment
  453. this.onComplete = this.options.onComplete;
  454. this.onRequest = this.options.onRequest;
  455. this.onFail = this.options.onFail;
  456. this.send = function (url, data) {
  457. if (this.options.method === 'GET' && data) {
  458. url = url + ((url.charAt(url.length - 1) === '?') ? '&' : '?') + data; //check for ? at the end of the string
  459. data = null;
  460. }
  461. // Open connection
  462. this.transport.open(this.options.method, url, this.options.async);
  463. // Set statechange and pass the active XHR object to it. From here it handles all status changes.
  464. this.transport.onreadystatechange = function () {
  465. self.onReadyStateChange.apply(self, arguments);
  466. };
  467. for (var key in this.options.headers) {
  468. this.transport.setRequestHeader(key, this.options.headers[key]);
  469. }
  470. if (typeof data === 'object') {
  471. data = this.serialize(data);
  472. }
  473. if (data) {
  474. this.transport.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  475. }
  476. this.onRequest();
  477. this.transport.send(data);
  478. };
  479. };
  480. DEBUGKIT.Util.Request.prototype.onReadyStateChange = function () {
  481. if (this.transport.readyState !== 4) {
  482. return;
  483. }
  484. if (this.transport.status === 200 || this.transport.status > 300 && this.transport.status < 400) {
  485. this.response = {
  486. xml: this.transport.responseXML,
  487. text: this.transport.responseText
  488. };
  489. if (typeof this.onComplete === 'function') {
  490. this.onComplete.apply(this, [this, this.response]);
  491. } else {
  492. return this.response;
  493. }
  494. } else if (this.transport.status > 400) {
  495. if (typeof this.onFail === 'function') {
  496. this.onFail.apply(this, []);
  497. } else {
  498. console.error('Request failed');
  499. }
  500. }
  501. };
  502. /**
  503. * Creates cross-broswer XHR object used for requests.
  504. * Tries using the standard XmlHttpRequest, then IE's wacky ActiveX Objects.
  505. */
  506. DEBUGKIT.Util.Request.prototype.createObj = function () {
  507. var request = null;
  508. try {
  509. request = new XMLHttpRequest();
  510. } catch (MS) {
  511. try {
  512. request = new ActiveXObject('Msxml2.XMLHTTP');
  513. } catch (old_MS) {
  514. try {
  515. request = new ActiveXObject('Microsoft.XMLHTTP');
  516. } catch (failure) {
  517. request = null;
  518. }
  519. }
  520. }
  521. return request;
  522. };
  523. /**
  524. * Serializes an object literal into a querystring.
  525. */
  526. DEBUGKIT.Util.Request.prototype.serialize = function (data) {
  527. var out = '';
  528. for (var name in data) {
  529. if (data.hasOwnProperty(name)) {
  530. out += name + '=' + data[name] + '&';
  531. }
  532. }
  533. return out.substring(0, out.length - 1);
  534. };
  535. // Basic toolbar module.
  536. DEBUGKIT.toolbar = function () {
  537. // Shortcuts
  538. var Cookie = DEBUGKIT.Util.Cookie,
  539. toolbarHidden = false;
  540. return {
  541. elements: {},
  542. panels: {},
  543. init: function () {
  544. var i, element, lists, index, _this = this;
  545. this.elements.toolbar = $('#debug-kit-toolbar');
  546. if (this.elements.toolbar.length === 0) {
  547. throw new Error('Toolbar not found, make sure you loaded it.');
  548. }
  549. this.elements.panel = $('#panel-tabs');
  550. this.elements.panel.find('.panel-tab').each(function (i, panel) {
  551. _this.addPanel(panel);
  552. });
  553. lists = this.elements.toolbar.find('.depth-0');
  554. this.makeNeatArray(lists);
  555. this.deactivatePanel(true);
  556. },
  557. // Add a panel to the toolbar
  558. addPanel: function (tab) {
  559. var button, content, _this = this;
  560. var panel = {
  561. id : false,
  562. element : tab,
  563. button : undefined,
  564. content : undefined,
  565. active : false
  566. };
  567. tab = $(tab);
  568. button = tab.children('a');
  569. panel.id = button.attr('href').replace(/^#/, '');
  570. panel.button = button;
  571. panel.content = tab.find('.panel-content');
  572. if (!panel.id || panel.content.length === 0) {
  573. return false;
  574. }
  575. this.makePanelDraggable(panel);
  576. this.makePanelMinMax(panel);
  577. button.on('click', function (event) {
  578. event.preventDefault();
  579. _this.togglePanel(panel.id);
  580. });
  581. this.panels[panel.id] = panel;
  582. return panel.id;
  583. },
  584. // Find the handle element and make the panel drag resizable.
  585. makePanelDraggable: function (panel) {
  586. // Create a variable in the enclosing scope, for scope tricks.
  587. var currentElement = null;
  588. // Use the elements startHeight stored Event.pageY and current Event.pageY to
  589. // resize the panel.
  590. var mouseMoveHandler = function (event) {
  591. event.preventDefault();
  592. if (!currentElement) {
  593. return;
  594. }
  595. var newHeight = currentElement.data('startHeight') + (event.pageY - currentElement.data('startY'));
  596. currentElement.parent().height(newHeight);
  597. };
  598. // Handle the mouseup event, remove the other listeners so the panel
  599. // doesn't continue to resize.
  600. var mouseUpHandler = function (event) {
  601. currentElement = null;
  602. $(document).off('mousemove', mouseMoveHandler).off('mouseup', mouseUpHandler);
  603. };
  604. var mouseDownHandler = function (event) {
  605. event.preventDefault();
  606. currentElement = $(this);
  607. currentElement.data('startY', event.pageY);
  608. currentElement.data('startHeight', currentElement.parent().height());
  609. // Attach to document so mouse doesn't have to stay precisely on the 'handle'.
  610. $(document).on('mousemove', mouseMoveHandler)
  611. .on('mouseup', mouseUpHandler);
  612. };
  613. panel.content.find('.panel-resize-handle').on('mousedown', mouseDownHandler);
  614. },
  615. // Make the maximize button work on the panels.
  616. makePanelMinMax: function (panel) {
  617. var _oldHeight;
  618. var maximize = function () {
  619. if (!_oldHeight) {
  620. _oldHeight = this.parentNode.offsetHeight;
  621. }
  622. var windowHeight = window.innerHeight;
  623. var panelHeight = windowHeight - this.parentNode.offsetTop;
  624. $(this.parentNode).height(panelHeight);
  625. $(this).text('-');
  626. };
  627. var minimize = function () {
  628. $(this.parentNode).height(_oldHeight);
  629. $(this).text('+');
  630. _oldHeight = null;
  631. };
  632. var state = 1;
  633. var toggle = function (event) {
  634. event.preventDefault();
  635. if (state === 1) {
  636. maximize.call(this);
  637. state = 0;
  638. } else {
  639. state = 1;
  640. minimize.call(this);
  641. }
  642. };
  643. panel.content.find('.panel-toggle').on('click', toggle);
  644. },
  645. // Toggle a panel
  646. togglePanel: function (id) {
  647. if (this.panels[id] && this.panels[id].active) {
  648. this.deactivatePanel(true);
  649. } else {
  650. this.deactivatePanel(true);
  651. this.activatePanel(id);
  652. }
  653. },
  654. // Make a panel active.
  655. activatePanel: function (id, unique) {
  656. if (this.panels[id] !== undefined && !this.panels[id].active) {
  657. var panel = this.panels[id];
  658. if (panel.content.length > 0) {
  659. panel.content.show();
  660. }
  661. var contentHeight = panel.content.find('.panel-content-data').height() + 70;
  662. if (contentHeight <= (window.innerHeight / 2)) {
  663. panel.content.height(contentHeight);
  664. }
  665. panel.button.addClass('active');
  666. panel.active = true;
  667. return true;
  668. }
  669. return false;
  670. },
  671. // Deactivate a panel. use true to hide all panels.
  672. deactivatePanel: function (id) {
  673. if (id === true) {
  674. for (var i in this.panels) {
  675. this.deactivatePanel(i);
  676. }
  677. return true;
  678. }
  679. if (this.panels[id] !== undefined) {
  680. var panel = this.panels[id];
  681. if (panel.content !== undefined) {
  682. panel.content.hide();
  683. }
  684. panel.button.removeClass('active');
  685. panel.active = false;
  686. return true;
  687. }
  688. return false;
  689. },
  690. // Bind events for all the collapsible arrays.
  691. makeNeatArray: function (lists) {
  692. lists.find('ul').hide()
  693. .parent().addClass('expandable collapsed');
  694. lists.on('click', 'li', function (event) {
  695. event.stopPropagation();
  696. $(this).children('ul').toggle().toggleClass('expanded collapsed');
  697. });
  698. }
  699. };
  700. }();
  701. DEBUGKIT.loader.register(DEBUGKIT.toolbar);
  702. DEBUGKIT.module('historyPanel');
  703. DEBUGKIT.historyPanel = function () {
  704. var toolbar = DEBUGKIT.toolbar,
  705. historyLinks;
  706. // Private methods to handle JSON response and insertion of
  707. // new content.
  708. var switchHistory = function (response) {
  709. historyLinks.removeClass('loading');
  710. $.each(toolbar.panels, function (id, panel) {
  711. if (panel.content === undefined || response[id] === undefined) {
  712. return;
  713. }
  714. var regionDiv = panel.content.find('.panel-resize-region');
  715. if (!regionDiv.length) {
  716. return;
  717. }
  718. var regionDivs = regionDiv.children();
  719. regionDivs.filter('div').hide();
  720. regionDivs.filter('.panel-history').each(function (i, panelContent) {
  721. var panelId = panelContent.id.replace('-history', '');
  722. if (response[panelId]) {
  723. panelContent = $(panelContent);
  724. panelContent.html(response[panelId]);
  725. var lists = panelContent.find('.depth-0');
  726. toolbar.makeNeatArray(lists);
  727. }
  728. panelContent.show();
  729. });
  730. });
  731. };
  732. // Private method to handle restoration to current request.
  733. var restoreCurrentState = function () {
  734. var id, i, panelContent, tag;
  735. historyLinks.removeClass('loading');
  736. $.each(toolbar.panels, function (panel, id) {
  737. if (panel.content === undefined) {
  738. return;
  739. }
  740. var regionDiv = panel.content.find('.panel-resize-region');
  741. if (!regionDiv.length) {
  742. return;
  743. }
  744. var regionDivs = regionDiv.children();
  745. regionDivs.filter('div').show()
  746. .end()
  747. .filter('.panel-history').hide();
  748. });
  749. };
  750. function handleHistoryLink(event) {
  751. event.preventDefault();
  752. historyLinks.removeClass('active');
  753. $(this).addClass('active loading');
  754. if (this.id === 'history-restore-current') {
  755. restoreCurrentState();
  756. return false;
  757. }
  758. var xhr = $.ajax({
  759. url: this.href,
  760. type: 'GET',
  761. dataType: 'json'
  762. });
  763. xhr.success(switchHistory).fail(function () {
  764. alert('History retrieval failed');
  765. });
  766. }
  767. return {
  768. init : function () {
  769. if (toolbar.panels.history === undefined) {
  770. return;
  771. }
  772. historyLinks = toolbar.panels.history.content.find('.history-link');
  773. historyLinks.on('click', handleHistoryLink);
  774. }
  775. };
  776. }();
  777. DEBUGKIT.loader.register(DEBUGKIT.historyPanel);
  778. //Add events + behaviors for toolbar collapser.
  779. DEBUGKIT.toolbarToggle = function () {
  780. var toolbar = DEBUGKIT.toolbar,
  781. Cookie = DEBUGKIT.Util.Cookie,
  782. toolbarHidden = false;
  783. return {
  784. init: function () {
  785. var button = $('#hide-toolbar'),
  786. self = this;
  787. button.on('click', function (event) {
  788. event.preventDefault();
  789. self.toggleToolbar();
  790. });
  791. var toolbarState = Cookie.read('toolbarDisplay');
  792. if (toolbarState !== 'show') {
  793. toolbarHidden = false;
  794. this.toggleToolbar();
  795. }
  796. },
  797. toggleToolbar: function () {
  798. var display = toolbarHidden ? 'show' : 'hide';
  799. $.each(toolbar.panels, function (i, panel) {
  800. $(panel.element)[display]();
  801. Cookie.write('toolbarDisplay', display);
  802. });
  803. toolbarHidden = !toolbarHidden;
  804. if (toolbarHidden) {
  805. $('#debug-kit-toolbar').addClass('minimized');
  806. } else {
  807. $('#debug-kit-toolbar').removeClass('minimized');
  808. }
  809. return false;
  810. }
  811. };
  812. }();
  813. DEBUGKIT.loader.register(DEBUGKIT.toolbarToggle);
  814. }; // DEBUGKIT.registerModules