PageRenderTime 3609ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/js/smartwidgets/jarvis.widget.js

https://github.com/skybot/TTR
JavaScript | 1442 lines | 751 code | 211 blank | 480 comment | 150 complexity | 36c86962b6cb5270290aa4a9890520d7 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. __ .__
  3. |__|____ __________ _|__| ______
  4. | \__ \\_ __ \ \/ / |/ ___/
  5. | |/ __ \| | \/\ /| |\___ \
  6. /\__| (____ /__| \_/ |__/____ >
  7. \______| \/ \/
  8. Copyright 2013 - SmartAdmin Template
  9. * This script is part of an item on wrapbootstrap.com
  10. * https://wrapbootstrap.com/user/myorange
  11. *
  12. * Date : Jan 2014
  13. * Updated : 12/12/2013
  14. * Dependency: jQuery UI core, json2(ie7)
  15. *
  16. * ************************************************************* *
  17. *
  18. * Jarvis Widgets (AKA Power Widgets), is originally created by
  19. * Mark (www.creativemilk.net) and Sunny (bootstraphunter.com).
  20. * This script may NOT be RESOLD or REDISTRUBUTED under any
  21. * circumstances, and is only to be used with this purchased
  22. * copy of SmartAdmin Template.
  23. *
  24. * ************************************************************* */
  25. ;
  26. (function ($, window, document, undefined) {
  27. //"use strict"; // jshint ;_;
  28. var pluginName = 'jarvisWidgets';
  29. function Plugin(element, options) {
  30. /**
  31. * Variables.
  32. **/
  33. this.obj = $(element);
  34. this.o = $.extend({}, $.fn[pluginName].defaults, options);
  35. this.objId = this.obj.attr('id');
  36. this.pwCtrls = '.jarviswidget-ctrls'
  37. this.widget = this.obj.find(this.o.widgets);
  38. this.toggleClass = this.o.toggleClass.split('|');
  39. this.editClass = this.o.editClass.split('|');
  40. this.fullscreenClass = this.o.fullscreenClass.split('|');
  41. this.customClass = this.o.customClass.split('|');
  42. this.init();
  43. };
  44. Plugin.prototype = {
  45. /**
  46. * Important settings like storage and touch support.
  47. *
  48. * @param:
  49. **/
  50. _settings: function () {
  51. var self = this;
  52. //*****************************************************************//
  53. //////////////////////// LOCALSTORAGE CHECK /////////////////////////
  54. //*****************************************************************//
  55. storage = !! function () {
  56. var result, uid = +new Date;
  57. try {
  58. localStorage.setItem(uid, uid);
  59. result = localStorage.getItem(uid) == uid;
  60. localStorage.removeItem(uid);
  61. return result;
  62. } catch (e) {}
  63. }() && localStorage;
  64. //*****************************************************************//
  65. /////////////////////////// SET/GET KEYS ////////////////////////////
  66. //*****************************************************************//
  67. // TODO : Push state does not work on IE9, try to find a way to detect IE and use a seperate filter
  68. if (storage && self.o.localStorage) {
  69. if (self.o.ajaxnav === true) {
  70. widget_url = location.hash.replace(/^#/, '')
  71. keySettings = 'Plugin_settings_' + widget_url + '_' + self.objId;
  72. getKeySettings = localStorage.getItem(keySettings);
  73. keyPosition = 'Plugin_position_' + widget_url + '_' + self.objId;
  74. getKeyPosition = localStorage.getItem(keyPosition);
  75. //console.log("from jarvis widget " + widget_url);
  76. //console.log(self.o.ajaxnav + " if")
  77. } else {
  78. keySettings = 'jarvisWidgets_settings_' + location.pathname + '_' + self.objId;
  79. getKeySettings = localStorage.getItem(keySettings);
  80. keyPosition = 'jarvisWidgets_position_' + location.pathname + '_' + self.objId;
  81. getKeyPosition = localStorage.getItem(keyPosition);
  82. //console.log(self.o.ajaxnav + " else")
  83. } // end else
  84. } // end if
  85. //*****************************************************************//
  86. ////////////////////////// TOUCH SUPPORT ////////////////////////////
  87. //*****************************************************************//
  88. /**
  89. * Check for touch support and set right click events.
  90. **/
  91. if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
  92. clickEvent = 'touchstart';
  93. //click tap
  94. } else {
  95. clickEvent = 'click';
  96. }
  97. },
  98. /**
  99. * Function for the indicator image.
  100. *
  101. * @param:
  102. **/
  103. _runLoaderWidget: function (elm) {
  104. var self = this;
  105. if (self.o.indicator === true) {
  106. elm.parents(self.o.widgets)
  107. .find('.jarviswidget-loader')
  108. .stop(true, true)
  109. .fadeIn(100)
  110. .delay(self.o.indicatorTime)
  111. .fadeOut(100);
  112. }
  113. },
  114. /**
  115. * Create a fixed timestamp.
  116. *
  117. * @param: t | date | Current date.
  118. **/
  119. _getPastTimestamp: function (t) {
  120. var self = this;
  121. var da = new Date(t);
  122. /**
  123. * Get and set the date and time.
  124. **/
  125. tsMonth = da.getMonth() + 1;
  126. // index based
  127. tsDay = da.getDate();
  128. tsYear = da.getFullYear();
  129. tsHours = da.getHours();
  130. tsMinutes = da.getMinutes();
  131. tsSeconds = da.getUTCSeconds();
  132. /**
  133. * Checking for one digit values, if so add an zero.
  134. **/
  135. if (tsMonth < 10) {
  136. var tsMonth = '0' + tsMonth
  137. }
  138. if (tsDay < 10) {
  139. var tsDay = '0' + tsDay
  140. }
  141. if (tsHours < 10) {
  142. var tsHours = '0' + tsHours
  143. }
  144. if (tsMinutes < 10) {
  145. var tsMinutes = '0' + tsMinutes
  146. }
  147. if (tsSeconds < 10) {
  148. var tsSeconds = '0' + tsSeconds
  149. }
  150. /**
  151. * The output, how you want it.
  152. **/
  153. var format = self.o.timestampFormat.replace(/%d%/g, tsDay)
  154. .replace(/%m%/g, tsMonth)
  155. .replace(/%y%/g, tsYear)
  156. .replace(/%h%/g, tsHours)
  157. .replace(/%i%/g, tsMinutes)
  158. .replace(/%s%/g, tsSeconds);
  159. return format;
  160. },
  161. /**
  162. * AJAX load File, which get and shows the .
  163. *
  164. * @param: awidget | object | The widget.
  165. * @param: file | file | The file thats beeing loaded.
  166. * @param: loader | object | The widget.
  167. **/
  168. _loadAjaxFile: function (awidget, file, loader) {
  169. var self = this
  170. awidget.find('.widget-body')
  171. .load(file, function (response, status, xhr) {
  172. var $this = $(this);
  173. /**
  174. * If action runs into an error display an error msg.
  175. **/
  176. if (status == "error") {
  177. $this.html('<h4 class="alert alert-danger">' + self.o.labelError + '<b> ' +
  178. xhr.status + " " + xhr.statusText + '</b></h4>');
  179. }
  180. /**
  181. * Run if there are no errors.
  182. **/
  183. if (status == "success") {
  184. /**
  185. * Show a timestamp.
  186. **/
  187. var aPalceholder = awidget.find(self.o.timestampPlaceholder);
  188. if (aPalceholder.length) {
  189. aPalceholder.html(self._getPastTimestamp(new Date()));
  190. }
  191. /**
  192. * Run the callback function.
  193. **/
  194. if (typeof self.o.afterLoad == 'function') {
  195. self.o.afterLoad.call(this, awidget);
  196. }
  197. }
  198. });
  199. /**
  200. * Run function for the indicator image.
  201. **/
  202. self._runLoaderWidget(loader);
  203. },
  204. /**
  205. * Save all settings to the localStorage.
  206. *
  207. * @param:
  208. **/
  209. _saveSettingsWidget: function () {
  210. var self = this;
  211. self._settings();
  212. if (storage && self.o.localStorage) {
  213. var storeSettings = [];
  214. self.obj.find(self.o.widgets)
  215. .each(function () {
  216. var storeSettingsStr = {};
  217. storeSettingsStr['id'] = $(this)
  218. .attr('id');
  219. storeSettingsStr['style'] = $(this)
  220. .attr('data-widget-attstyle');
  221. storeSettingsStr['title'] = $(this)
  222. .children('header')
  223. .children('h2')
  224. .text();
  225. storeSettingsStr['hidden'] = ($(this)
  226. .is(':hidden') ? 1 : 0);
  227. storeSettingsStr['collapsed'] = ($(this)
  228. .hasClass('jarviswidget-collapsed') ? 1 : 0);
  229. storeSettings.push(storeSettingsStr);
  230. });
  231. var storeSettingsObj = JSON.stringify({
  232. 'widget': storeSettings
  233. });
  234. /* Place it in the storage(only if needed) */
  235. if (getKeySettings != storeSettingsObj) {
  236. localStorage.setItem(keySettings, storeSettingsObj);
  237. }
  238. }
  239. /**
  240. * Run the callback function.
  241. **/
  242. if (typeof self.o.onSave == 'function') {
  243. self.o.onSave.call(this, null, storeSettingsObj);
  244. }
  245. },
  246. /**
  247. * Save positions to the localStorage.
  248. *
  249. * @param:
  250. **/
  251. _savePositionWidget: function () {
  252. var self = this;
  253. self._settings();
  254. if (storage && self.o.localStorage) {
  255. var mainArr = [];
  256. self.obj.find(self.o.grid + '.sortable-grid')
  257. .each(function () {
  258. var subArr = [];
  259. $(this)
  260. .children(self.o.widgets)
  261. .each(function () {
  262. var subObj = {};
  263. subObj['id'] = $(this)
  264. .attr('id');
  265. subArr.push(subObj);
  266. });
  267. var out = {
  268. 'section': subArr
  269. }
  270. mainArr.push(out);
  271. });
  272. var storePositionObj = JSON.stringify({
  273. 'grid': mainArr
  274. });
  275. /* Place it in the storage(only if needed) */
  276. if (getKeyPosition != storePositionObj) {
  277. localStorage.setItem(keyPosition, storePositionObj, null);
  278. }
  279. }
  280. /**
  281. * Run the callback function.
  282. **/
  283. if (typeof self.o.onSave == 'function') {
  284. self.o.onSave.call(this, storePositionObj);
  285. }
  286. },
  287. /**
  288. * Code that we run at the start.
  289. *
  290. * @param:
  291. **/
  292. init: function () {
  293. var self = this;
  294. self._settings();
  295. /**
  296. * Force users to use an id(it's needed for the local storage).
  297. **/
  298. if (!$('#' + self.objId)
  299. .length) {
  300. alert('It looks like your using a class instead of an ID, dont do that!')
  301. }
  302. /**
  303. * Add RTL support.
  304. **/
  305. if (self.o.rtl === true) {
  306. $('body')
  307. .addClass('rtl');
  308. }
  309. /**
  310. * This will add an extra class that we use to store the
  311. * widgets in the right order.(savety)
  312. **/
  313. $(self.o.grid)
  314. .each(function () {
  315. if ($(this)
  316. .find(self.o.widgets)
  317. .length) {
  318. $(this)
  319. .addClass('sortable-grid');
  320. }
  321. });
  322. //*****************************************************************//
  323. //////////////////////// SET POSITION WIDGET ////////////////////////
  324. //*****************************************************************//
  325. /**
  326. * Run if data is present.
  327. **/
  328. if (storage && self.o.localStorage && getKeyPosition) {
  329. var jsonPosition = JSON.parse(getKeyPosition);
  330. /**
  331. * Loop the data, and put every widget on the right place.
  332. **/
  333. for (var key in jsonPosition.grid) {
  334. var changeOrder = self.obj.find(self.o.grid + '.sortable-grid')
  335. .eq(key);
  336. for (var key2 in jsonPosition.grid[key].section) {
  337. changeOrder.append($('#' + jsonPosition.grid[key].section[key2].id));
  338. }
  339. }
  340. }
  341. //*****************************************************************//
  342. /////////////////////// SET SETTINGS WIDGET /////////////////////////
  343. //*****************************************************************//
  344. /**
  345. * Run if data is present.
  346. **/
  347. if (storage && self.o.localStorage && getKeySettings) {
  348. var jsonSettings = JSON.parse(getKeySettings);
  349. /**
  350. * Loop the data and hide/show the widgets and set the inputs in
  351. * panel to checked(if hidden) and add an indicator class to the div.
  352. * Loop all labels and update the widget titles.
  353. **/
  354. for (var key in jsonSettings.widget) {
  355. var widgetId = $('#' + jsonSettings.widget[key].id);
  356. /**
  357. * Set a style(if present).
  358. **/
  359. if (jsonSettings.widget[key].style) {
  360. //console.log("test");
  361. widgetId.removeClassPrefix('jarviswidget-color-')
  362. .addClass(jsonSettings.widget[key].style)
  363. .attr('data-widget-attstyle', '' + jsonSettings.widget[key].style + '');
  364. }
  365. /**
  366. * Hide/show widget.
  367. **/
  368. if (jsonSettings.widget[key].hidden == 1) {
  369. widgetId.hide(1);
  370. } else {
  371. widgetId.show(1)
  372. .removeAttr('data-widget-hidden');
  373. }
  374. /**
  375. * Toggle content widget.
  376. **/
  377. if (jsonSettings.widget[key].collapsed == 1) {
  378. widgetId.addClass('jarviswidget-collapsed')
  379. .children('div')
  380. .hide(1);
  381. }
  382. /**
  383. * Update title widget (if needed).
  384. **/
  385. if (widgetId.children('header')
  386. .children('h2')
  387. .text() != jsonSettings.widget[key].title) {
  388. widgetId.children('header')
  389. .children('h2')
  390. .text(jsonSettings.widget[key].title);
  391. }
  392. }
  393. }
  394. //*****************************************************************//
  395. ////////////////////////// LOOP AL WIDGETS //////////////////////////
  396. //*****************************************************************//
  397. /**
  398. * This will add/edit/remove the settings to all widgets
  399. **/
  400. self.widget.each(function () {
  401. var tWidget = $(this);
  402. var thisHeader = $(this)
  403. .children('header');
  404. /**
  405. * Dont double wrap(check).
  406. **/
  407. if (!thisHeader.parent()
  408. .attr('role')) {
  409. /**
  410. * Hide the widget if the dataset 'widget-hidden' is set to true.
  411. **/
  412. if (tWidget.data('widget-hidden') === true) {
  413. tWidget.hide();
  414. }
  415. /**
  416. * Hide the content of the widget if the dataset
  417. * 'widget-collapsed' is set to true.
  418. **/
  419. if (tWidget.data('widget-collapsed') === true) {
  420. tWidget.addClass('jarviswidget-collapsed')
  421. .children('div')
  422. .hide();
  423. }
  424. /**
  425. * Check for the dataset 'widget-icon' if so get the icon
  426. * and attach it to the widget header.
  427. * NOTE: MOVED THIS TO PHYSICAL for more control
  428. **/
  429. //if(tWidget.data('widget-icon')){
  430. // thisHeader.prepend('<i class="jarviswidget-icon '+tWidget.data('widget-icon')+'"></i>');
  431. //}
  432. /**
  433. * Add a delete button to the widget header (if set to true).
  434. **/
  435. if (self.o.customButton === true && tWidget.data('widget-custombutton') ===
  436. undefined && self.customClass[0].length != 0) {
  437. var customBtn =
  438. '<a href="javascript:void(0);" class="button-icon jarviswidget-custom-btn"><i class="' +
  439. self.customClass[0] + '"></i></a>';
  440. } else {
  441. customBtn = '';
  442. }
  443. /**
  444. * Add a delete button to the widget header (if set to true).
  445. **/
  446. if (self.o.deleteButton === true && tWidget.data('widget-deletebutton') ===
  447. undefined) {
  448. var deleteBtn =
  449. '<a href="javascript:void(0);" class="button-icon jarviswidget-delete-btn" rel="tooltip" title="Delete" data-placement="bottom"><i class="' +
  450. self.o.deleteClass + '"></i></a>';
  451. } else {
  452. deleteBtn = '';
  453. }
  454. /**
  455. * Add a delete button to the widget header (if set to true).
  456. **/
  457. if (self.o.editButton === true && tWidget.data('widget-editbutton') === undefined) {
  458. var editBtn =
  459. '<a href="javascript:void(0);" class="button-icon jarviswidget-edit-btn" rel="tooltip" title="Edit Title" data-placement="bottom"><i class="' +
  460. self.editClass[0] + '"></i></a>';
  461. } else {
  462. editBtn = '';
  463. }
  464. /**
  465. * Add a delete button to the widget header (if set to true).
  466. **/
  467. if (self.o.fullscreenButton === true && tWidget.data('widget-fullscreenbutton') ===
  468. undefined) {
  469. var fullscreenBtn =
  470. '<a href="javascript:void(0);" class="button-icon jarviswidget-fullscreen-btn" rel="tooltip" title="Fullscreen" data-placement="bottom"><i class="' +
  471. self.fullscreenClass[0] + '"></i></a>';
  472. } else {
  473. fullscreenBtn = '';
  474. }
  475. /**
  476. * Add a delete button to the widget header (if set to true).
  477. **/
  478. if (self.o.colorButton === true && tWidget.data('widget-colorbutton') ===
  479. undefined) {
  480. var widgetcolorBtn =
  481. '<a data-toggle="dropdown" class="dropdown-toggle color-box selector" href="javascript:void(0);"></a><ul class="dropdown-menu arrow-box-up-right color-select pull-right"><li><span class="bg-color-green" data-widget-setstyle="jarviswidget-color-green" rel="tooltip" data-placement="left" data-original-title="Green Grass"></span></li><li><span class="bg-color-greenDark" data-widget-setstyle="jarviswidget-color-greenDark" rel="tooltip" data-placement="top" data-original-title="Dark Green"></span></li><li><span class="bg-color-greenLight" data-widget-setstyle="jarviswidget-color-greenLight" rel="tooltip" data-placement="top" data-original-title="Light Green"></span></li><li><span class="bg-color-purple" data-widget-setstyle="jarviswidget-color-purple" rel="tooltip" data-placement="top" data-original-title="Purple"></span></li><li><span class="bg-color-magenta" data-widget-setstyle="jarviswidget-color-magenta" rel="tooltip" data-placement="top" data-original-title="Magenta"></span></li><li><span class="bg-color-pink" data-widget-setstyle="jarviswidget-color-pink" rel="tooltip" data-placement="right" data-original-title="Pink"></span></li><li><span class="bg-color-pinkDark" data-widget-setstyle="jarviswidget-color-pinkDark" rel="tooltip" data-placement="left" data-original-title="Fade Pink"></span></li><li><span class="bg-color-blueLight" data-widget-setstyle="jarviswidget-color-blueLight" rel="tooltip" data-placement="top" data-original-title="Light Blue"></span></li><li><span class="bg-color-teal" data-widget-setstyle="jarviswidget-color-teal" rel="tooltip" data-placement="top" data-original-title="Teal"></span></li><li><span class="bg-color-blue" data-widget-setstyle="jarviswidget-color-blue" rel="tooltip" data-placement="top" data-original-title="Ocean Blue"></span></li><li><span class="bg-color-blueDark" data-widget-setstyle="jarviswidget-color-blueDark" rel="tooltip" data-placement="top" data-original-title="Night Sky"></span></li><li><span class="bg-color-darken" data-widget-setstyle="jarviswidget-color-darken" rel="tooltip" data-placement="right" data-original-title="Night"></span></li><li><span class="bg-color-yellow" data-widget-setstyle="jarviswidget-color-yellow" rel="tooltip" data-placement="left" data-original-title="Day Light"></span></li><li><span class="bg-color-orange" data-widget-setstyle="jarviswidget-color-orange" rel="tooltip" data-placement="bottom" data-original-title="Orange"></span></li><li><span class="bg-color-orangeDark" data-widget-setstyle="jarviswidget-color-orangeDark" rel="tooltip" data-placement="bottom" data-original-title="Dark Orange"></span></li><li><span class="bg-color-red" data-widget-setstyle="jarviswidget-color-red" rel="tooltip" data-placement="bottom" data-original-title="Red Rose"></span></li><li><span class="bg-color-redLight" data-widget-setstyle="jarviswidget-color-redLight" rel="tooltip" data-placement="bottom" data-original-title="Light Red"></span></li><li><span class="bg-color-white" data-widget-setstyle="jarviswidget-color-white" rel="tooltip" data-placement="right" data-original-title="Purity"></span></li><li><a href="javascript:void(0);" class="jarviswidget-remove-colors" data-widget-setstyle="" rel="tooltip" data-placement="bottom" data-original-title="Reset widget color to default">Remove</a></li></ul>';
  482. thisHeader.prepend('<div class="widget-toolbar">' + widgetcolorBtn + '</div>');
  483. } else {
  484. widgetcolorBtn = '';
  485. }
  486. /**
  487. * Add a toggle button to the widget header (if set to true).
  488. **/
  489. if (self.o.toggleButton === true && tWidget.data('widget-togglebutton') ===
  490. undefined) {
  491. if (tWidget.data('widget-collapsed') === true || tWidget.hasClass(
  492. 'jarviswidget-collapsed')) {
  493. var toggleSettings = self.toggleClass[1];
  494. } else {
  495. toggleSettings = self.toggleClass[0];
  496. }
  497. var toggleBtn =
  498. '<a href="#" class="button-icon jarviswidget-toggle-btn" rel="tooltip" title="Collapse" data-placement="bottom"><i class="' +
  499. toggleSettings + '"></i></a>';
  500. } else {
  501. toggleBtn = '';
  502. }
  503. /**
  504. * Add a refresh button to the widget header (if set to true).
  505. **/
  506. if (self.o.refreshButton === true && tWidget.data('widget-refreshbutton') !=
  507. false && tWidget.data('widget-load')) {
  508. var refreshBtn =
  509. '<a href="#" class="button-icon jarviswidget-refresh-btn" data-loading-text="&nbsp;&nbsp;Loading...&nbsp;" rel="tooltip" title="Refresh" data-placement="bottom"><i class="' +
  510. self.o.refreshButtonClass + '"></i></a>';
  511. } else {
  512. refreshBtn = '';
  513. }
  514. /**
  515. * Set the buttons order.
  516. **/
  517. var formatButtons = self.o.buttonOrder.replace(/%refresh%/g, refreshBtn)
  518. .replace(/%delete%/g, deleteBtn)
  519. .replace(/%custom%/g, customBtn)
  520. .replace(/%fullscreen%/g, fullscreenBtn)
  521. .replace(/%edit%/g, editBtn)
  522. .replace(/%toggle%/g, toggleBtn);
  523. /**
  524. * Add a button wrapper to the header.
  525. **/
  526. if (refreshBtn != '' || deleteBtn != '' || customBtn != '' || fullscreenBtn != '' ||
  527. editBtn != '' || toggleBtn != '') {
  528. thisHeader.prepend('<div class="jarviswidget-ctrls">' + formatButtons +
  529. '</div>');
  530. }
  531. /**
  532. * Adding a helper class to all sortable widgets, this will be
  533. * used to find the widgets that are sortable, it will skip the widgets
  534. * that have the dataset 'widget-sortable="false"' set to false.
  535. **/
  536. if (self.o.sortable === true && tWidget.data('widget-sortable') === undefined) {
  537. tWidget.addClass('jarviswidget-sortable');
  538. }
  539. /**
  540. * If the edit box is present copy the title to the input.
  541. **/
  542. if (tWidget.find(self.o.editPlaceholder)
  543. .length) {
  544. tWidget.find(self.o.editPlaceholder)
  545. .find('input')
  546. .val($.trim(thisHeader.children('h2')
  547. .text()));
  548. }
  549. /**
  550. * Prepend the image to the widget header.
  551. **/
  552. thisHeader.append(
  553. '<span class="jarviswidget-loader"><i class="fa fa-refresh fa-spin"></i></span>'
  554. );
  555. /**
  556. * Adding roles to some parts.
  557. **/
  558. tWidget.attr('role', 'widget')
  559. .children('div')
  560. .attr('role', 'content')
  561. .prev('header')
  562. .attr('role', 'heading')
  563. .children('div')
  564. .attr('role', 'menu');
  565. }
  566. });
  567. /**
  568. * Hide all buttons if option is set to true.
  569. **/
  570. if (self.o.buttonsHidden === true) {
  571. $(self.o.pwCtrls)
  572. .hide();
  573. }
  574. /* activate all tooltips */
  575. $(".jarviswidget header [rel=tooltip]")
  576. .tooltip();
  577. //******************************************************************//
  578. //////////////////////////////// AJAX ////////////////////////////////
  579. //******************************************************************//
  580. /**
  581. * Loop all ajax widgets.
  582. **/
  583. self.obj.find('[data-widget-load]')
  584. .each(function () {
  585. /**
  586. * Variables.
  587. **/
  588. var thisItem = $(this),
  589. thisItemHeader = thisItem.children(),
  590. pathToFile = thisItem.data('widget-load'),
  591. reloadTime = thisItem.data('widget-refresh') * 1000,
  592. ajaxLoader = thisItem.children();
  593. if (!thisItem.find('.jarviswidget-ajax-placeholder')
  594. .length) {
  595. /**
  596. * Append a AJAX placeholder.
  597. **/
  598. thisItem.children('widget-body')
  599. .append('<div class="jarviswidget-ajax-placeholder">' + self.o.loadingLabel +
  600. '</div>');
  601. /**
  602. * If widget has a reload time refresh the widget, if the value
  603. * has been set to 0 dont reload.
  604. **/
  605. if (thisItem.data('widget-refresh') > 0) {
  606. /**
  607. * Load file on start.
  608. **/
  609. self._loadAjaxFile(thisItem, pathToFile, thisItemHeader);
  610. /**
  611. * Set an interval to reload the content every XXX seconds.
  612. **/
  613. setInterval(function () {
  614. self._loadAjaxFile(thisItem, pathToFile, thisItemHeader);
  615. }, reloadTime);
  616. } else {
  617. /**
  618. * Load the content just once.
  619. **/
  620. self._loadAjaxFile(thisItem, pathToFile, thisItemHeader);
  621. }
  622. }
  623. });
  624. //******************************************************************//
  625. ////////////////////////////// SORTABLE //////////////////////////////
  626. //******************************************************************//
  627. /**
  628. * jQuery UI soratble, this allows users to sort the widgets.
  629. * Notice that this part needs the jquery-ui core to work.
  630. **/
  631. if (self.o.sortable === true && jQuery.ui) {
  632. var sortItem = self.obj.find('.sortable-grid')
  633. .not('[data-widget-excludegrid]');
  634. sortItem.sortable({
  635. items: sortItem.find('.jarviswidget-sortable'),
  636. connectWith: sortItem,
  637. placeholder: self.o.placeholderClass,
  638. cursor: 'move',
  639. revert: true,
  640. opacity: self.o.opacity,
  641. delay: 200,
  642. cancel: '.button-icon, #jarviswidget-fullscreen-mode > div',
  643. zIndex: 10000,
  644. handle: self.o.dragHandle,
  645. forcePlaceholderSize: true,
  646. forceHelperSize: true,
  647. update: function (event, ui) {
  648. /* run pre-loader in the widget */
  649. self._runLoaderWidget(ui.item.children());
  650. /* store the positions of the plugins */
  651. self._savePositionWidget();
  652. /**
  653. * Run the callback function.
  654. **/
  655. if (typeof self.o.onChange == 'function') {
  656. self.o.onChange.call(this, ui.item);
  657. }
  658. }
  659. });
  660. }
  661. //*****************************************************************//
  662. ////////////////////////// BUTTONS VISIBLE //////////////////////////
  663. //*****************************************************************//
  664. /**
  665. * Show and hide the widget control buttons, the buttons will be
  666. * visible if the users hover over the widgets header. At default the
  667. * buttons are always visible.
  668. **/
  669. if (self.o.buttonsHidden === true) {
  670. /**
  671. * Show and hide the buttons.
  672. **/
  673. self.widget.children('header')
  674. .hover(function () {
  675. $(this)
  676. .children(self.o.pwCtrls)
  677. .stop(true, true)
  678. .fadeTo(100, 1.0);
  679. }, function () {
  680. $(this)
  681. .children(self.o.pwCtrls)
  682. .stop(true, true)
  683. .fadeTo(100, 0.0);
  684. });
  685. }
  686. //*****************************************************************//
  687. ///////////////////////// CLICKEVENTS //////////////////////////
  688. //*****************************************************************//
  689. self._clickEvents();
  690. //*****************************************************************//
  691. ///////////////////// DELETE LOCAL STORAGE KEYS /////////////////////
  692. //*****************************************************************//
  693. /**
  694. * Delete the settings key.
  695. **/
  696. $(self.o.deleteSettingsKey)
  697. .on(clickEvent, this, function (e) {
  698. if (storage && self.o.localStorage) {
  699. var cleared = confirm(self.o.settingsKeyLabel);
  700. if (cleared) {
  701. localStorage.removeItem(keySettings);
  702. }
  703. }
  704. e.preventDefault();
  705. });
  706. /**
  707. * Delete the position key.
  708. **/
  709. $(self.o.deletePositionKey)
  710. .on(clickEvent, this, function (e) {
  711. if (storage && self.o.localStorage) {
  712. var cleared = confirm(self.o.positionKeyLabel);
  713. if (cleared) {
  714. localStorage.removeItem(keyPosition);
  715. }
  716. }
  717. e.preventDefault();
  718. });
  719. //*****************************************************************//
  720. ///////////////////////// CREATE NEW KEYS //////////////////////////
  721. //*****************************************************************//
  722. /**
  723. * Create new keys if non are present.
  724. **/
  725. if (storage && self.o.localStorage) {
  726. /**
  727. * If the local storage key (keySettings) is empty or
  728. * does not excite, create one and fill it.
  729. **/
  730. if (getKeySettings === null || getKeySettings.length < 1) {
  731. self._saveSettingsWidget();
  732. }
  733. /**
  734. * If the local storage key (keyPosition) is empty or
  735. * does not excite, create one and fill it.
  736. **/
  737. if (getKeyPosition === null || getKeyPosition.length < 1) {
  738. self._savePositionWidget();
  739. }
  740. }
  741. },
  742. /**
  743. * All of the click events.
  744. *
  745. * @param:
  746. **/
  747. _clickEvents: function () {
  748. var self = this;
  749. self._settings();
  750. //*****************************************************************//
  751. /////////////////////////// TOGGLE WIDGETS //////////////////////////
  752. //*****************************************************************//
  753. /**
  754. * Allow users to toggle the content of the widgets.
  755. **/
  756. self.widget.on(clickEvent, '.jarviswidget-toggle-btn', function (e) {
  757. var tWidget = $(this);
  758. var pWidget = tWidget.parents(self.o.widgets);
  759. /**
  760. * Run function for the indicator image.
  761. **/
  762. self._runLoaderWidget(tWidget);
  763. /**
  764. * Change the class and hide/show the widgets content.
  765. **/
  766. if (pWidget.hasClass('jarviswidget-collapsed')) {
  767. tWidget.children()
  768. .removeClass(self.toggleClass[1])
  769. .addClass(self.toggleClass[0])
  770. .parents(self.o.widgets)
  771. .removeClass('jarviswidget-collapsed')
  772. .children('[role=content]')
  773. .slideDown(self.o.toggleSpeed, function () {
  774. self._saveSettingsWidget();
  775. });
  776. } else {
  777. tWidget.children()
  778. .removeClass(self.toggleClass[0])
  779. .addClass(self.toggleClass[1])
  780. .parents(self.o.widgets)
  781. .addClass('jarviswidget-collapsed')
  782. .children('[role=content]')
  783. .slideUp(self.o.toggleSpeed, function () {
  784. self._saveSettingsWidget();
  785. });
  786. }
  787. /**
  788. * Run the callback function.
  789. **/
  790. if (typeof self.o.onToggle == 'function') {
  791. self.o.onToggle.call(this, pWidget);
  792. }
  793. e.preventDefault();
  794. });
  795. //*****************************************************************//
  796. ///////////////////////// FULLSCREEN WIDGETS ////////////////////////
  797. //*****************************************************************//
  798. /**
  799. * Set fullscreen height function.
  800. **/
  801. function heightFullscreen() {
  802. if ($('#jarviswidget-fullscreen-mode')
  803. .length) {
  804. /**
  805. * Setting height variables.
  806. **/
  807. var heightWindow = $(window)
  808. .height();
  809. var heightHeader = $('#jarviswidget-fullscreen-mode')
  810. .find(self.o.widgets)
  811. .children('header')
  812. .height();
  813. /**
  814. * Setting the height to the right widget.
  815. **/
  816. $('#jarviswidget-fullscreen-mode')
  817. .find(self.o.widgets)
  818. .children('div')
  819. .height(heightWindow - heightHeader - 15);
  820. }
  821. }
  822. /**
  823. * On click go to fullscreen mode.
  824. **/
  825. self.widget.on(clickEvent, '.jarviswidget-fullscreen-btn', function (e) {
  826. var thisWidget = $(this)
  827. .parents(self.o.widgets);
  828. var thisWidgetContent = thisWidget.children('div');
  829. /**
  830. * Run function for the indicator image.
  831. **/
  832. self._runLoaderWidget($(this));
  833. /**
  834. * Wrap the widget and go fullsize.
  835. **/
  836. if ($('#jarviswidget-fullscreen-mode')
  837. .length) {
  838. /**
  839. * Remove class from the body.
  840. **/
  841. $('.nooverflow')
  842. .removeClass('nooverflow');
  843. /**
  844. * Unwrap the widget, remove the height, set the right
  845. * fulscreen button back, and show all other buttons.
  846. **/
  847. thisWidget.unwrap('<div>')
  848. .children('div')
  849. .removeAttr('style')
  850. .end()
  851. .find('.jarviswidget-fullscreen-btn')
  852. .children()
  853. .removeClass(self.fullscreenClass[1])
  854. .addClass(self.fullscreenClass[0])
  855. .parents(self.pwCtrls)
  856. .children('a')
  857. .show();
  858. /**
  859. * Reset collapsed widgets.
  860. **/
  861. if (thisWidgetContent.hasClass('jarviswidget-visible')) {
  862. thisWidgetContent.hide()
  863. .removeClass('jarviswidget-visible');
  864. }
  865. } else {
  866. /**
  867. * Prevent the body from scrolling.
  868. **/
  869. $('body')
  870. .addClass('nooverflow');
  871. /**
  872. * Wrap, append it to the body, show the right button
  873. * and hide all other buttons.
  874. **/
  875. thisWidget.wrap('<div id="jarviswidget-fullscreen-mode"/>')
  876. .parent()
  877. .find('.jarviswidget-fullscreen-btn')
  878. .children()
  879. .removeClass(self.fullscreenClass[0])
  880. .addClass(self.fullscreenClass[1])
  881. .parents(self.pwCtrls)
  882. .children('a:not(.jarviswidget-fullscreen-btn)')
  883. .hide();
  884. /**
  885. * Show collapsed widgets.
  886. **/
  887. if (thisWidgetContent.is(':hidden')) {
  888. thisWidgetContent.show()
  889. .addClass('jarviswidget-visible');
  890. }
  891. }
  892. /**
  893. * Run the set height function.
  894. **/
  895. heightFullscreen();
  896. /**
  897. * Run the callback function.
  898. **/
  899. if (typeof self.o.onFullscreen == 'function') {
  900. self.o.onFullscreen.call(this, thisWidget);
  901. }
  902. e.preventDefault();
  903. });
  904. /**
  905. * Run the set fullscreen height function when the screen resizes.
  906. **/
  907. $(window)
  908. .resize(function () {
  909. /**
  910. * Run the set height function.
  911. **/
  912. heightFullscreen();
  913. });
  914. //*****************************************************************//
  915. //////////////////////////// EDIT WIDGETS ///////////////////////////
  916. //*****************************************************************//
  917. /**
  918. * Allow users to show/hide a edit box.
  919. **/
  920. self.widget.on(clickEvent, '.jarviswidget-edit-btn', function (e) {
  921. var tWidget = $(this)
  922. .parents(self.o.widgets);
  923. /**
  924. * Run function for the indicator image.
  925. **/
  926. self._runLoaderWidget($(this));
  927. /**
  928. * Show/hide the edit box.
  929. **/
  930. if (tWidget.find(self.o.editPlaceholder)
  931. .is(':visible')) {
  932. $(this)
  933. .children()
  934. .removeClass(self.editClass[1])
  935. .addClass(self.editClass[0])
  936. .parents(self.o.widgets)
  937. .find(self.o.editPlaceholder)
  938. .slideUp(self.o.editSpeed, function () {
  939. self._saveSettingsWidget();
  940. });
  941. } else {
  942. $(this)
  943. .children()
  944. .removeClass(self.editClass[0])
  945. .addClass(self.editClass[1])
  946. .parents(self.o.widgets)
  947. .find(self.o.editPlaceholder)
  948. .slideDown(self.o.editSpeed);
  949. }
  950. /**
  951. * Run the callback function.
  952. **/
  953. if (typeof self.o.onEdit == 'function') {
  954. self.o.onEdit.call(this, tWidget);
  955. }
  956. e.preventDefault();
  957. });
  958. /**
  959. * Update the widgets title by using the edit input.
  960. **/
  961. $(self.o.editPlaceholder)
  962. .find('input')
  963. .keyup(function () {
  964. $(this)
  965. .parents(self.o.widgets)
  966. .children('header')
  967. .children('h2')
  968. .text($(this)
  969. .val());
  970. });
  971. /**
  972. * Set a custom style.
  973. **/
  974. self.widget.on(clickEvent, '[data-widget-setstyle]', function (e) {
  975. var val = $(this)
  976. .data('widget-setstyle');
  977. var styles = '';
  978. /**
  979. * Get all other styles, in order to remove it.
  980. **/
  981. $(this)
  982. .parents(self.o.editPlaceholder)
  983. .find('[data-widget-setstyle]')
  984. .each(function () {
  985. styles += $(this)
  986. .data('widget-setstyle') + ' ';
  987. });
  988. /**
  989. * Set the new style.
  990. **/
  991. $(this)
  992. .parents(self.o.widgets)
  993. .attr('data-widget-attstyle', '' + val + '')
  994. .removeClassPrefix('jarviswidget-color-')
  995. .addClass(val);
  996. /**
  997. * Run function for the indicator image.
  998. **/
  999. self._runLoaderWidget($(this));
  1000. /**
  1001. * Lets save the setings.
  1002. **/
  1003. self._saveSettingsWidget();
  1004. e.preventDefault();
  1005. });
  1006. //*****************************************************************//
  1007. /////////////////////////// CUSTOM ACTION ///////////////////////////
  1008. //*****************************************************************//
  1009. /**
  1010. * Allow users to show/hide a edit box.
  1011. **/
  1012. self.widget.on(clickEvent, '.jarviswidget-custom-btn', function (e) {
  1013. var w = $(this)
  1014. .parents(self.o.widgets);
  1015. /**
  1016. * Run function for the indicator image.
  1017. **/
  1018. self._runLoaderWidget($(this));
  1019. /**
  1020. * Start and end custom action.
  1021. **/
  1022. if ($(this)
  1023. .children('.' + self.customClass[0])
  1024. .length) {
  1025. $(this)
  1026. .children()
  1027. .removeClass(self.customClass[0])
  1028. .addClass(self.customClass[1]);
  1029. /**
  1030. * Run the callback function.

Large files files are truncated, but you can click here to view the full file