~openerp-dev/openerp-web/trunk-view_improvement-psi

« back to all changes in this revision

Viewing changes to addons/web_graph/static/lib/flotr2/js/plugins/handles.js

  • Committer: Jitendra Prajapati(OpenERP)
  • Date: 2014-01-28 05:37:19 UTC
  • mfrom: (3870.1.48 web)
  • Revision ID: prajapatijitendra7969@gmail.com-20140128053719-keddm9112a4cl8mr
[MERGE]with main branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/** 
2
 
 * Selection Handles Plugin
3
 
 *
4
 
 * Depends upon options.selection.mode
5
 
 *
6
 
 * Options
7
 
 *  show - True enables the handles plugin.
8
 
 *  drag - Left and Right drag handles
9
 
 *  scroll - Scrolling handle
10
 
 */
11
 
(function () {
12
 
 
13
 
var D = Flotr.DOM;
14
 
 
15
 
Flotr.addPlugin('handles', {
16
 
 
17
 
  options: {
18
 
    show: false,
19
 
    drag: true,
20
 
    scroll: true
21
 
  },
22
 
 
23
 
  callbacks: {
24
 
    'flotr:afterinit': init,
25
 
    'flotr:select': handleSelect,
26
 
    'flotr:mousedown': reset,
27
 
    'flotr:mousemove': mouseMoveHandler
28
 
  }
29
 
 
30
 
});
31
 
 
32
 
 
33
 
function init() {
34
 
 
35
 
  var
36
 
    options = this.options,
37
 
    handles = this.handles,
38
 
    el = this.el,
39
 
    scroll, left, right, container;
40
 
 
41
 
  if (!options.selection.mode || !options.handles.show || 'ontouchstart' in el) return;
42
 
 
43
 
  handles.initialized = true;
44
 
 
45
 
  container = D.node('<div class="flotr-handles"></div>');
46
 
  options = options.handles;
47
 
 
48
 
  // Drag handles
49
 
  if (options.drag) {
50
 
    right = D.node('<div class="flotr-handles-handle flotr-handles-drag flotr-handles-right"></div>');
51
 
    left  = D.node('<div class="flotr-handles-handle flotr-handles-drag flotr-handles-left"></div>');
52
 
    D.insert(container, right);
53
 
    D.insert(container, left);
54
 
    D.hide(left);
55
 
    D.hide(right);
56
 
    handles.left = left;
57
 
    handles.right = right;
58
 
 
59
 
    this.observe(left, 'mousedown', function () {
60
 
      handles.moveHandler = leftMoveHandler;
61
 
    });
62
 
    this.observe(right, 'mousedown', function () {
63
 
      handles.moveHandler = rightMoveHandler;
64
 
    });
65
 
  }
66
 
 
67
 
  // Scroll handle
68
 
  if (options.scroll) {
69
 
    scroll = D.node('<div class="flotr-handles-handle flotr-handles-scroll"></div>');
70
 
    D.insert(container, scroll);
71
 
    D.hide(scroll);
72
 
    handles.scroll = scroll;
73
 
    this.observe(scroll, 'mousedown', function () {
74
 
      handles.moveHandler = scrollMoveHandler;
75
 
    });
76
 
  }
77
 
 
78
 
  this.observe(document, 'mouseup', function() {
79
 
    handles.moveHandler = null;
80
 
  });
81
 
 
82
 
  D.insert(el, container);
83
 
}
84
 
 
85
 
 
86
 
function handleSelect(selection) {
87
 
 
88
 
  if (!this.handles.initialized) return;
89
 
 
90
 
  var
91
 
    handles = this.handles,
92
 
    options = this.options.handles,
93
 
    left = handles.left,
94
 
    right = handles.right,
95
 
    scroll = handles.scroll;
96
 
 
97
 
  if (options) {
98
 
    if (options.drag) {
99
 
      positionDrag(this, left, selection.x1);
100
 
      positionDrag(this, right, selection.x2);
101
 
    }
102
 
 
103
 
    if (options.scroll) {
104
 
      positionScroll(
105
 
        this,
106
 
        scroll,
107
 
        selection.x1,
108
 
        selection.x2
109
 
      );
110
 
    }
111
 
  }
112
 
}
113
 
 
114
 
function positionDrag(graph, handle, x) {
115
 
 
116
 
  D.show(handle);
117
 
 
118
 
  var size = D.size(handle),
119
 
    l = Math.round(graph.axes.x.d2p(x) - size.width / 2),
120
 
    t = (graph.plotHeight - size.height) / 2;
121
 
 
122
 
  D.setStyles(handle, {
123
 
    'left' : l+'px',
124
 
    'top'  : t+'px'
125
 
  });
126
 
}
127
 
 
128
 
function positionScroll(graph, handle, x1, x2) {
129
 
 
130
 
  D.show(handle);
131
 
 
132
 
  var size = D.size(handle),
133
 
    l = Math.round(graph.axes.x.d2p(x1)),
134
 
    t = (graph.plotHeight) - size.height / 2,
135
 
    w = (graph.axes.x.d2p(x2) - graph.axes.x.d2p(x1));
136
 
 
137
 
  D.setStyles(handle, {
138
 
    'left' : l+'px',
139
 
    'top'  : t+'px',
140
 
    'width': w+'px'
141
 
  });
142
 
}
143
 
 
144
 
function reset() {
145
 
 
146
 
  if (!this.handles.initialized) return;
147
 
 
148
 
  var
149
 
    handles = this.handles;
150
 
  if (handles) {
151
 
    D.hide(handles.left);
152
 
    D.hide(handles.right);
153
 
    D.hide(handles.scroll);
154
 
  }
155
 
}
156
 
 
157
 
function mouseMoveHandler(e, position) {
158
 
 
159
 
  if (!this.handles.initialized) return;
160
 
  if (!this.handles.moveHandler) return;
161
 
 
162
 
  var
163
 
    delta = position.dX,
164
 
    selection = this.selection.selection,
165
 
    area = this.selection.getArea(),
166
 
    handles = this.handles;
167
 
 
168
 
  handles.moveHandler(area, delta);
169
 
  checkSwap(area, handles);
170
 
 
171
 
  this.selection.setSelection(area);
172
 
}
173
 
 
174
 
function checkSwap (area, handles) {
175
 
  var moveHandler = handles.moveHandler;
176
 
  if (area.x1 > area.x2) {
177
 
    if (moveHandler == leftMoveHandler) {
178
 
      moveHandler = rightMoveHandler;
179
 
    } else if (moveHandler == rightMoveHandler) {
180
 
      moveHandler = leftMoveHandler;
181
 
    }
182
 
    handles.moveHandler = moveHandler;
183
 
  }
184
 
}
185
 
 
186
 
function leftMoveHandler(area, delta) {
187
 
  area.x1 += delta;
188
 
}
189
 
 
190
 
function rightMoveHandler(area, delta) {
191
 
  area.x2 += delta;
192
 
}
193
 
 
194
 
function scrollMoveHandler(area, delta) {
195
 
  area.x1 += delta;
196
 
  area.x2 += delta;
197
 
}
198
 
 
199
 
})();