app.js 15.8 KB
Newer Older
A
Abdullah Almsaeed 已提交
1 2
/*! AdminLTE app.js
 * ================
A
almasaeed2010 已提交
3 4 5 6 7 8 9
 * Main JS application file for AdminLTE v2. This file
 * should be included in all pages. It controls some layout
 * options and implements exclusive AdminLTE plugins.
 *
 * @Author  Almsaeed Studio
 * @Support <http://www.almsaeedstudio.com>
 * @Email   <support@almsaeedstudio.com>
10
 * @version 2.1.0
A
almasaeed2010 已提交
11 12 13
 * @license MIT <http://opensource.org/licenses/MIT>
 */

14 15
'use strict';

A
almasaeed2010 已提交
16 17 18 19 20 21 22 23 24
//Make sure jQuery has been loaded before app.js
if (typeof jQuery === "undefined") {
  throw new Error("AdminLTE requires jQuery");
}

/* AdminLTE
 *
 * @type Object
 * @description $.AdminLTE is the main object for the template's app.
A
Abdullah Almsaeed 已提交
25 26 27 28
 *              It's used for implementing functions and options related
 *              to the template. Keeping everything wrapped in an object
 *              prevents conflict with other plugins and is a better
 *              way to organize our code.
A
almasaeed2010 已提交
29
 */
A
Abdullah Almsaeed 已提交
30
$.AdminLTE = {};
A
almasaeed2010 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

/* --------------------
 * - AdminLTE Options -
 * --------------------
 * Modify these options to suit your implementation
 */
$.AdminLTE.options = {
  //Add slimscroll to navbar menus
  //This requires you to load the slimscroll plugin
  //in every page before app.js
  navbarMenuSlimscroll: true,
  navbarMenuSlimscrollWidth: "3px", //The width of the scroll bar
  navbarMenuHeight: "200px", //The height of the inner menu
  //Sidebar push menu toggle button selector
  sidebarToggleSelector: "[data-toggle='offcanvas']",
  //Activate sidebar push menu
  sidebarPushMenu: true,
A
Abdullah Almsaeed 已提交
48
  //Activate sidebar slimscroll if the fixed layout is set (requires SlimScroll Plugin)
49
  sidebarSlimScroll: true,
A
almasaeed2010 已提交
50 51 52 53 54
  //BoxRefresh Plugin
  enableBoxRefresh: true,
  //Bootstrap.js tooltip
  enableBSToppltip: true,
  BSTooltipSelector: "[data-toggle='tooltip']",
55
  //Enable Fast Click. Fastclick.js creates a more
A
Abdullah Almsaeed 已提交
56
  //native touch experience with touch devices. If you
A
Abdullah Almsaeed 已提交
57
  //choose to enable the plugin, make sure you load the script
58 59
  //before AdminLTE's app.js
  enableFastclick: true,
A
almasaeed2010 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  //Box Widget Plugin. Enable this plugin
  //to allow boxes to be collapsed and/or removed
  enableBoxWidget: true,
  //Box Widget plugin options
  boxWidgetOptions: {
    boxWidgetIcons: {
      //The icon that triggers the collapse event
      collapse: 'fa fa-minus',
      //The icon that trigger the opening event
      open: 'fa fa-plus',
      //The icon that triggers the removing event
      remove: 'fa fa-times'
    },
    boxWidgetSelectors: {
      //Remove button selector
      remove: '[data-widget="remove"]',
      //Collapse button selector
      collapse: '[data-widget="collapse"]'
    }
  },
80 81 82 83 84 85 86
  //Direct Chat plugin options
  directChat: {
    //Enable direct chat by default
    enable: true,
    //The button to open and close the chat contacts pane
    contactToggleSelector: '[data-widget="chat-pane-toggle"]'
  },
A
almasaeed2010 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
  //Define the set of colors to use globally around the website
  colors: {
    lightBlue: "#3c8dbc",
    red: "#f56954",
    green: "#00a65a",
    aqua: "#00c0ef",
    yellow: "#f39c12",
    blue: "#0073b7",
    navy: "#001F3F",
    teal: "#39CCCC",
    olive: "#3D9970",
    lime: "#01FF70",
    orange: "#FF851B",
    fuchsia: "#F012BE",
    purple: "#8E24AA",
    maroon: "#D81B60",
    black: "#222222",
A
Abdullah Almsaeed 已提交
104
    gray: "#d2d6de"
A
Abdullah Almsaeed 已提交
105 106 107 108 109 110 111 112 113
  },
  //The standard screen sizes that bootstrap uses.
  //If you change these in the variables.less file, change
  //them here too.
  screenSizes: {
    xs: 480,
    sm: 768,
    md: 992,
    lg: 1200
A
almasaeed2010 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127
  }
};

/* ------------------
 * - Implementation -
 * ------------------
 * The next block of code implements AdminLTE's
 * functions and plugins as specified by the
 * options above.
 */
$(function () {
  //Easy access to options
  var o = $.AdminLTE.options;

A
Abdullah Almsaeed 已提交
128 129 130
  //Set up the object
  _init();

A
almasaeed2010 已提交
131 132 133
  //Activate the layout maker
  $.AdminLTE.layout.activate();

A
Abdullah Almsaeed 已提交
134 135 136
  //Enable sidebar tree view controls
  $.AdminLTE.tree('.sidebar');

A
almasaeed2010 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  //Add slimscroll to navbar dropdown
  if (o.navbarMenuSlimscroll && typeof $.fn.slimscroll != 'undefined') {
    $(".navbar .menu").slimscroll({
      height: "200px",
      alwaysVisible: false,
      size: "3px"
    }).css("width", "100%");
  }

  //Activate sidebar push menu
  if (o.sidebarPushMenu) {
    $.AdminLTE.pushMenu(o.sidebarToggleSelector);
  }

  //Activate Bootstrap tooltip
  if (o.enableBSToppltip) {
    $(o.BSTooltipSelector).tooltip();
  }

  //Activate box widget
  if (o.enableBoxWidget) {
    $.AdminLTE.boxWidget.activate();
  }
160 161 162

  //Activate fast click
  if (o.enableFastclick && typeof FastClick != 'undefined') {
163 164
    FastClick.attach(document.body);
  }
A
almasaeed2010 已提交
165

166 167 168 169 170 171 172 173
  //Activate direct chat widget
  if (o.directChat.enable) {
    $(o.directChat.contactToggleSelector).click(function () {
      var box = $(this).parents('.direct-chat').first();
      box.toggleClass('direct-chat-contacts-open');
    });
  }

A
almasaeed2010 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  /*
   * INITIALIZE BUTTON TOGGLE
   * ------------------------
   */
  $('.btn-group[data-toggle="btn-toggle"]').each(function () {
    var group = $(this);
    $(this).find(".btn").click(function (e) {
      group.find(".btn.active").removeClass("active");
      $(this).addClass("active");
      e.preventDefault();
    });

  });
});

A
Abdullah Almsaeed 已提交
189 190 191
/* ----------------------------------
 * - Initialize the AdminLTE Object -
 * ----------------------------------
A
almasaeed2010 已提交
192 193
 * All AdminLTE functions are implemented below.
 */
A
Abdullah Almsaeed 已提交
194
function _init() {
A
almasaeed2010 已提交
195

A
Abdullah Almsaeed 已提交
196 197 198 199 200 201 202 203 204 205 206 207
  /* Layout
   * ======
   * Fixes the layout height in case min-height fails.
   *
   * @type Object
   * @usage $.AdminLTE.layout.activate()
   *        $.AdminLTE.layout.fix()
   *        $.AdminLTE.layout.fixSidebar()
   */
  $.AdminLTE.layout = {
    activate: function () {
      var _this = this;
A
almasaeed2010 已提交
208 209
      _this.fix();
      _this.fixSidebar();
A
Abdullah Almsaeed 已提交
210 211 212 213 214 215 216 217 218 219 220 221 222 223
      $(window, ".wrapper").resize(function () {
        _this.fix();
        _this.fixSidebar();
      });
    },
    fix: function () {
      //Get window height and the wrapper height
      var neg = $('.main-header').outerHeight() + $('.main-footer').outerHeight();
      var window_height = $(window).height();
      var sidebar_height = $(".sidebar").height();
      //Set the min-height of the content and sidebar based on the
      //the height of the document.
      if ($("body").hasClass("fixed")) {
        $(".content-wrapper, .right-side").css('min-height', window_height - $('.main-footer').outerHeight());
A
Abdullah Almsaeed 已提交
224
      } else {
A
Abdullah Almsaeed 已提交
225 226 227 228 229
        if (window_height >= sidebar_height) {
          $(".content-wrapper, .right-side").css('min-height', window_height - neg);
        } else {
          $(".content-wrapper, .right-side").css('min-height', sidebar_height);
        }
A
Abdullah Almsaeed 已提交
230
      }
A
Abdullah Almsaeed 已提交
231 232 233 234 235 236 237 238 239 240
    },
    fixSidebar: function () {
      //Make sure the body tag has the .fixed class
      if (!$("body").hasClass("fixed")) {
        if (typeof $.fn.slimScroll != 'undefined') {
          $(".sidebar").slimScroll({destroy: true}).height("auto");
        }
        return;
      } else if (typeof $.fn.slimScroll == 'undefined' && console) {
        console.error("Error: the fixed layout requires the slimscroll plugin!");
A
almasaeed2010 已提交
241
      }
A
Abdullah Almsaeed 已提交
242 243 244 245 246 247 248 249 250 251 252 253
      //Enable slimscroll for fixed layout
      if ($.AdminLTE.options.sidebarSlimScroll) {
        if (typeof $.fn.slimScroll != 'undefined') {
          //Distroy if it exists
          $(".sidebar").slimScroll({destroy: true}).height("auto");
          //Add slimscroll
          $(".sidebar").slimscroll({
            height: ($(window).height() - $(".main-header").height()) + "px",
            color: "rgba(0,0,0,0.2)",
            size: "3px"
          });
        }
A
almasaeed2010 已提交
254 255
      }
    }
A
Abdullah Almsaeed 已提交
256
  };
A
almasaeed2010 已提交
257

A
Abdullah Almsaeed 已提交
258 259 260 261 262 263 264 265 266 267
  /* PushMenu()
   * ==========
   * Adds the push menu functionality to the sidebar.
   *
   * @type Function
   * @usage: $.AdminLTE.pushMenu("[data-toggle='offcanvas']")
   */
  $.AdminLTE.pushMenu = function (toggleBtn) {
    //Get the screen sizes
    var screenSizes = this.options.screenSizes;
A
Abdullah Almsaeed 已提交
268

A
Abdullah Almsaeed 已提交
269 270 271
    //Enable sidebar toggle
    $(toggleBtn).click(function (e) {
      e.preventDefault();
A
Abdullah Almsaeed 已提交
272

A
Abdullah Almsaeed 已提交
273 274 275
      //Enable sidebar push menu
      if ($(window).width() > (screenSizes.sm - 1)) {
        $("body").toggleClass('sidebar-collapse');
A
Abdullah Almsaeed 已提交
276
      }
A
Abdullah Almsaeed 已提交
277 278 279 280 281 282 283 284 285 286
      //Handle sidebar push menu for small screens
      else {
        if ($("body").hasClass('sidebar-open')) {
          $("body").removeClass('sidebar-open');
          $("body").removeClass('sidebar-collapse')
        } else {
          $("body").addClass('sidebar-open');
        }
      }
    });
A
Abdullah Almsaeed 已提交
287

A
Abdullah Almsaeed 已提交
288 289 290 291 292 293
    $(".content-wrapper").click(function () {
      //Enable hide menu when clicking on the content-wrapper on small screens
      if ($(window).width() <= (screenSizes.sm - 1) && $("body").hasClass("sidebar-open")) {
        $("body").removeClass('sidebar-open');
      }
    });
A
almasaeed2010 已提交
294

A
Abdullah Almsaeed 已提交
295
  };
A
Abdullah Almsaeed 已提交
296

A
Abdullah Almsaeed 已提交
297 298 299 300 301 302 303 304 305 306
  /* Tree()
   * ======
   * Converts the sidebar into a multilevel
   * tree view menu.
   *
   * @type Function
   * @Usage: $.AdminLTE.tree('.sidebar')
   */
  $.AdminLTE.tree = function (menu) {
    var _this = this;
A
almasaeed2010 已提交
307

A
Abdullah Almsaeed 已提交
308 309 310 311
    $("li a", $(menu)).click(function (e) {
      //Get the clicked link and the next element
      var $this = $(this);
      var checkElement = $this.next();
A
almasaeed2010 已提交
312

A
Abdullah Almsaeed 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
      //Check if the next element is a menu and is visible
      if ((checkElement.is('.treeview-menu')) && (checkElement.is(':visible'))) {
        //Close the menu
        checkElement.slideUp('normal', function () {
          checkElement.removeClass('menu-open');
          //Fix the layout in case the sidebar stretches over the height of the window
          //_this.layout.fix();
        });
        checkElement.parent("li").removeClass("active");
      }
      //If the menu is not visible
      else if ((checkElement.is('.treeview-menu')) && (!checkElement.is(':visible'))) {
        //Get the parent menu
        var parent = $this.parents('ul').first();
        //Close all open menus within the parent
        var ul = parent.find('ul:visible').slideUp('normal');
        //Remove the menu-open class from the parent
        ul.removeClass('menu-open');
        //Get the parent li
        var parent_li = $this.parent("li");
A
almasaeed2010 已提交
333

A
Abdullah Almsaeed 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347
        //Open the target menu and add the menu-open class
        checkElement.slideDown('normal', function () {
          //Add the class active to the parent li
          checkElement.addClass('menu-open');
          parent.find('li.active').removeClass('active');
          parent_li.addClass('active');
          //Fix the layout in case the sidebar stretches over the height of the window
          _this.layout.fix();
        });
      }
      //if this isn't a link, prevent the page from being redirected
      if (checkElement.is('.treeview-menu')) {
        e.preventDefault();
      }
A
almasaeed2010 已提交
348
    });
A
Abdullah Almsaeed 已提交
349
  };
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  
  /* ControlSidebar
   * ==============
   * Control the all of the control sidebar transitions
   * 
   * @type Object
   * @usage $.Admin.controlSidebar.activate(options)
   */
  $.AdminLTE.controlSidebar = {
    //Default settings
    defaults: {
      toggleBtnSelector: "[data-toggle='control-sidebar']",
      selector: ".control-sidebar",
      slide: false,
      animationSpeed: 300
    },
    //Initiate the object
    activate: function (options) {
      var settings = $.extend({}, options, this.defaults);
    },
    //Close the control sidebar
    close: function () {
      
    },
    //Open the control sidebar
    open: function () {
      
    },
    //If the slide option is set, this
    //function is used to open the sidebar
    slideIn: function() {
      
    },
    //The complement funtion to slideIn
    slideOut: function () {}
  };
A
almasaeed2010 已提交
386

A
Abdullah Almsaeed 已提交
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  /* BoxWidget
   * =========
   * BoxWidget is plugin to handle collapsing and
   * removing boxes from the screen.
   *
   * @type Object
   * @usage $.AdminLTE.boxWidget.activate()
   *        Set all of your option in the main $.AdminLTE.options object
   */
  $.AdminLTE.boxWidget = {
    activate: function () {
      var o = $.AdminLTE.options;
      var _this = this;
      //Listen for collapse event triggers
      $(o.boxWidgetOptions.boxWidgetSelectors.collapse).click(function (e) {
        e.preventDefault();
        _this.collapse($(this));
A
almasaeed2010 已提交
404
      });
A
Abdullah Almsaeed 已提交
405 406 407 408 409

      //Listen for remove event triggers
      $(o.boxWidgetOptions.boxWidgetSelectors.remove).click(function (e) {
        e.preventDefault();
        _this.remove($(this));
A
almasaeed2010 已提交
410
      });
A
Abdullah Almsaeed 已提交
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    },
    collapse: function (element) {
      //Find the box parent
      var box = element.parents(".box").first();
      //Find the body and the footer
      var bf = box.find(".box-body, .box-footer");
      if (!box.hasClass("collapsed-box")) {
        //Convert minus into plus
        element.children(".fa-minus").removeClass("fa-minus").addClass("fa-plus");
        bf.slideUp(300, function () {
          box.addClass("collapsed-box");
        });
      } else {
        //Convert plus into minus
        element.children(".fa-plus").removeClass("fa-plus").addClass("fa-minus");
        bf.slideDown(300, function () {
          box.removeClass("collapsed-box");
        });
      }
    },
    remove: function (element) {
      //Find the box parent
      var box = element.parents(".box").first();
      box.slideUp();
    },
    options: $.AdminLTE.options.boxWidgetOptions
  };
}
A
almasaeed2010 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451

/* ------------------
 * - Custom Plugins -
 * ------------------
 * All custom plugins are defined below.
 */

/*
 * BOX REFRESH BUTTON
 * ------------------
 * This is a custom plugin to use with the compenet BOX. It allows you to add
 * a refresh button to the box. It converts the box's state to a loading state.
 *
A
Abdullah Almsaeed 已提交
452
 * @type plugin
A
almasaeed2010 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
 * @usage $("#box-widget").boxRefresh( options );
 */
(function ($) {

  $.fn.boxRefresh = function (options) {

    // Render options
    var settings = $.extend({
      //Refressh button selector
      trigger: ".refresh-btn",
      //File source to be loaded (e.g: ajax/src.php)
      source: "",
      //Callbacks
      onLoadStart: function (box) {
      }, //Right after the button has been clicked
      onLoadDone: function (box) {
      } //When the source has been loaded

    }, options);

    //The overlay
A
Abdullah Almsaeed 已提交
474
    var overlay = $('<div class="overlay"><div class="fa fa-refresh fa-spin"></div></div>');
A
almasaeed2010 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

    return this.each(function () {
      //if a source is specified
      if (settings.source === "") {
        if (console) {
          console.log("Please specify a source first - boxRefresh()");
        }
        return;
      }
      //the box
      var box = $(this);
      //the button
      var rBtn = box.find(settings.trigger).first();

      //On trigger click
      rBtn.click(function (e) {
        e.preventDefault();
        //Add loading overlay
        start(box);

        //Perform ajax call
        box.find(".box-body").load(settings.source, function () {
          done(box);
        });
      });
    });

    function start(box) {
      //Add overlay and loading img
      box.append(overlay);

      settings.onLoadStart.call(box);
    }

    function done(box) {
      //Remove overlay and loading img
      box.find(overlay).remove();

      settings.onLoadDone.call(box);
    }

  };

})(jQuery);

/*
 * TODO LIST CUSTOM PLUGIN
 * -----------------------
 * This plugin depends on iCheck plugin for checkbox and radio inputs
 *
 * @type plugin
 * @usage $("#todo-widget").todolist( options );
 */
(function ($) {

  $.fn.todolist = function (options) {
    // Render options
    var settings = $.extend({
      //When the user checks the input
      onCheck: function (ele) {
      },
      //When the user unchecks the input
      onUncheck: function (ele) {
      }
    }, options);

    return this.each(function () {

      if (typeof $.fn.iCheck != 'undefined') {
        $('input', this).on('ifChecked', function (event) {
          var ele = $(this).parents("li").first();
          ele.toggleClass("done");
          settings.onCheck.call(ele);
        });

        $('input', this).on('ifUnchecked', function (event) {
          var ele = $(this).parents("li").first();
          ele.toggleClass("done");
          settings.onUncheck.call(ele);
        });
      } else {
        $('input', this).on('change', function (event) {
          var ele = $(this).parents("li").first();
          ele.toggleClass("done");
          settings.onCheck.call(ele);
        });
      }
    });
  };
A
almasaeed2010 已提交
564
}(jQuery));