Passed
Push — master ( edbfbd...89a048 )
by Alexey
05:17
created

inji.onLoad   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 36
rs 8.5806
c 0
b 0
f 0
1
/**
2
 *  BootTree Treeview plugin for Bootstrap.
3
 *
4
 *  Based on BootSnipp TreeView Example by Sean Wessell
5
 *  URL:	http://bootsnipp.com/snippets/featured/bootstrap-30-treeview
6
 *
7
 *	Revised code by Leo "LeoV117" Myers
8
 *
9
 */
10
inji.onLoad(function () {
11
  $.fn.extend({
12
    treeview: function () {
13
      return this.each(function () {
14
        // Initialize the top levels;
15
        var tree = $(this);
16
        //skip alredy loaded
17
        if (tree.hasClass('treeview-tree')) {
18
          return;
19
        }
20
        tree.addClass('treeview-tree');
21
        tree.find('li').has("ul").each(function () {
22
          var branch = $(this); //li with children ul
23
          var icon = branch.data('treeview-icon-closed');
24
          if (!icon) {
25
            icon = 'glyphicon glyphicon-chevron-right';
26
          }
27
          branch.prepend("<i class='tree-indicator " + icon + "'></i>");
28
          branch.addClass('tree-branch closed');
29
          branch.on('click', function (e) {
30
            if (this == e.target) {
31
              var icon = $(this).children('i:first');
32
              
33
              var closedIcon = branch.data('treeview-icon-closed');
34
              if (!closedIcon) {
35
                closedIcon = 'glyphicon glyphicon-chevron-right';
36
              }
37
              var openedIcon = branch.data('treeview-icon-opened');
38
              if (!openedIcon) {
39
                openedIcon = 'glyphicon glyphicon-chevron-down';
40
              }
41
42
              icon.toggleClass(openedIcon + " " + closedIcon);
43
              $(this).children().children().toggle();
44
              $(this).toggleClass('opened closed');
45
            }
46
          })
47
          if (branch.find('li.active').length || $(this).hasClass('active')) {
48
            branch.click();
49
          }
50
          branch.children().children().toggle();
51
52
          branch.children('.tree-indicator').click(function (e) {
53
            branch.click();
54
            e.preventDefault();
55
          });
56
        });
57
        tree.find('li').not(":has(ul)").each(function () {
58
          var icon = $(this).data('treeview-icon-linear');
59
          console.log(icon);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
60
          if (!icon) {
61
            icon = 'glyphicon glyphicon-minus';
62
          }
63
          $(this).prepend("<i class='tree-indicator " + icon + "'></i>");
64
        });
65
      });
66
    }
67
  });
68
69
  /**
70
   *	The following snippet of code automatically converst
71
   *	any '.treeview' DOM elements into a treeview component.
72
   */
73
  $(window).on('load', function () {
74
    $('.treeview').each(function () {
75
      var tree = $(this);
76
      tree.treeview();
77
    });
78
  });
79
});