Debian Codegraph @

src/js/Rickshaw.Graph.Behavior.Series.Toggle.js

raw
1 Rickshaw.namespace('Rickshaw.Graph.Behavior.Series.Toggle');
2
3 Rickshaw.Graph.Behavior.Series.Toggle = function(args) {
4
5 this.graph = args.graph;
6 this.legend = args.legend;
7
8 var self = this;
9
10 this.addAnchor = function(line) {
11
12 var anchor = document.createElement('a');
13 anchor.innerHTML = '✔';
14 anchor.classList.add('action');
15 line.element.insertBefore(anchor, line.element.firstChild);
16
17 anchor.onclick = function(e) {
18 if (line.series.disabled) {
19 line.series.enable();
20 line.element.classList.remove('disabled');
21 } else {
22 if (this.graph.series.filter(function(s) { return !s.disabled }).length <= 1) return;
23 line.series.disable();
24 line.element.classList.add('disabled');
25 }
26
27 self.graph.update();
28
29 }.bind(this);
30
31 var label = line.element.getElementsByTagName('span')[0];
32 label.onclick = function(e){
33
34 var disableAllOtherLines = line.series.disabled;
35 if ( ! disableAllOtherLines ) {
36 for ( var i = 0; i < self.legend.lines.length; i++ ) {
37 var l = self.legend.lines[i];
38 if ( line.series === l.series ) {
39 // noop
40 } else if ( l.series.disabled ) {
41 // noop
42 } else {
43 disableAllOtherLines = true;
44 break;
45 }
46 }
47 }
48
49 // show all or none
50 if ( disableAllOtherLines ) {
51
52 // these must happen first or else we try ( and probably fail ) to make a no line graph
53 line.series.enable();
54 line.element.classList.remove('disabled');
55
56 self.legend.lines.forEach(function(l){
57 if ( line.series === l.series ) {
58 // noop
59 } else {
60 l.series.disable();
61 l.element.classList.add('disabled');
62 }
63 });
64
65 } else {
66
67 self.legend.lines.forEach(function(l){
68 l.series.enable();
69 l.element.classList.remove('disabled');
70 });
71
72 }
73
74 self.graph.update();
75
76 };
77
78 };
79
80 if (this.legend) {
81
82 var $ = jQuery;
83 if (typeof $ != 'undefined' && $(this.legend.list).sortable) {
84
85 $(this.legend.list).sortable( {
86 start: function(event, ui) {
87 ui.item.bind('no.onclick',
88 function(event) {
89 event.preventDefault();
90 }
91 );
92 },
93 stop: function(event, ui) {
94 setTimeout(function(){
95 ui.item.unbind('no.onclick');
96 }, 250);
97 }
98 });
99 }
100
101 this.legend.lines.forEach( function(l) {
102 self.addAnchor(l);
103 } );
104 }
105
106 this._addBehavior = function() {
107
108 this.graph.series.forEach( function(s) {
109
110 s.disable = function() {
111
112 if (self.graph.series.length <= 1) {
113 throw('only one series left');
114 }
115
116 s.disabled = true;
117 };
118
119 s.enable = function() {
120 s.disabled = false;
121 };
122 } );
123 };
124 this._addBehavior();
125
126 this.updateBehaviour = function () { this._addBehavior() };
127
128 };