text: ﻿/** 
   * @widget Tree 
   * @plugin Lazy Loading
   */
  gj.tree.plugins.lazyLoading = {
      config: {
          base: {
  
              paramNames: {
  
                  /** The name of the parameter that is going to send the parent identificator.
                   * Lazy Loading needs to be enabled in order this parameter to be in use.
                   * @alias paramNames.parentId
                   * @type string
                   * @default "parentId"
                   */
                  parentId: 'parentId'
              },
  
              /** Enables lazy loading
                * @type Boolean
                * @default false
                * @example Material.Design <!-- tree -->
                * <div id="tree"></div>
                * <script>
                *     $('#tree').tree({
                *         dataSource: '/Locations/LazyGet',
                *         primaryKey: 'id',
                *         lazyLoading: true
                *     });
                * </script>
                */
              lazyLoading: false
          }
      },
  
      private: {
          nodeDataBound: function ($tree, $node, id, record) {
              var data = $tree.data(),
                  $expander = $node.find('> [data-role="wrapper"] > [data-role="expander"]');
  
              if (record.hasChildren) {
                  $expander.empty().append(data.icons.expand);
              }
          },
  
          createDoneHandler: function ($tree, $node) {
              return function (response) {
                  var i, $expander, $list, data = $tree.data();
                  if (typeof (response) === 'string' && JSON) {
                      response = JSON.parse(response);
                  }
                  if (response && response.length) {
                      $list = $('<ul />').addClass(data.style.list);
                      $node.append($list);
                      for (i = 0; i < response.length; i++) {
                          $tree.addNode(response[i], $list);
                      }
                      $expander = $node.find('>[data-role="wrapper"]>[data-role="expander"]'),
                      $expander.attr('data-mode', 'open');
                      $expander.empty().append(data.icons.collapse);
                      gj.tree.events.dataBound($tree);
                  }
              };
          },
  
          expand: function ($tree, $node, id) {
              var ajaxOptions, data = $tree.data(), params = {},
                  $children = $node.find('>ul>li');
  
              if (!$children || !$children.length) {
                  if (typeof (data.dataSource) === 'string') {
                      params[data.paramNames.parentId] = id;
                      ajaxOptions = { url: data.dataSource, data: params };
                      if ($tree.xhr) {
                          $tree.xhr.abort();
                      }
                      $tree.xhr = $.ajax(ajaxOptions).done(gj.tree.plugins.lazyLoading.private.createDoneHandler($tree, $node)).fail($tree.createErrorHandler());
                  }
              }
          }
      },
  
      public: {},
  
      events: {},
  
      configure: function ($tree, fullConfig, clientConfig) {
          if (clientConfig.lazyLoading) {
              $tree.on('nodeDataBound', function (e, $node, id, record) {
                  gj.tree.plugins.lazyLoading.private.nodeDataBound($tree, $node, id, record);
              });
              $tree.on('expand', function (e, $node, id) {
                  gj.tree.plugins.lazyLoading.private.expand($tree, $node, id);
              });
          }
      }
  };
  