summaryrefslogtreecommitdiff
path: root/src/static/js/draggable.js
blob: 8d1975459fa6ce13c04dcd8122ec79c73da5fab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
 * This code is mostly from the old Etherpad. Please help us to comment this code. 
 * This helps other people to understand this code better and helps them to improve it.
 * TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
 */

/**
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

function makeDraggable(jqueryNodes, eventHandler)
{
  jqueryNodes.each(function()
  {
    var node = $(this);
    var state = {};
    var inDrag = false;

    function dragStart(evt)
    {
      if (inDrag)
      {
        return;
      }
      inDrag = true;
      if (eventHandler('dragstart', evt, state) !== false)
      {
        $(document).bind('mousemove', dragUpdate);
        $(document).bind('mouseup', dragEnd);
      }
      evt.preventDefault();
      return false;
    }

    function dragUpdate(evt)
    {
      if (!inDrag)
      {
        return;
      }
      eventHandler('dragupdate', evt, state);
      evt.preventDefault();
      return false;
    }

    function dragEnd(evt)
    {
      if (!inDrag)
      {
        return;
      }
      inDrag = false;
      try
      {
        eventHandler('dragend', evt, state);
      }
      finally
      {
        $(document).unbind('mousemove', dragUpdate);
        $(document).unbind('mouseup', dragEnd);
        evt.preventDefault();
      }
      return false;
    }
    node.bind('mousedown', dragStart);
  });
}

function makeResizableVPane(top, sep, bottom, minTop, minBottom, callback)
{
  if (minTop === undefined) minTop = 0;
  if (minBottom === undefined) minBottom = 0;

  makeDraggable($(sep), function(eType, evt, state)
  {
    if (eType == 'dragstart')
    {
      state.startY = evt.pageY;
      state.topHeight = $(top).height();
      state.bottomHeight = $(bottom).height();
      state.minTop = minTop;
      state.maxTop = (state.topHeight + state.bottomHeight) - minBottom;
    }
    else if (eType == 'dragupdate')
    {
      var change = evt.pageY - state.startY;

      var topHeight = state.topHeight + change;
      if (topHeight < state.minTop)
      {
        topHeight = state.minTop;
      }
      if (topHeight > state.maxTop)
      {
        topHeight = state.maxTop;
      }
      change = topHeight - state.topHeight;

      var bottomHeight = state.bottomHeight - change;
      var sepHeight = $(sep).height();

      var totalHeight = topHeight + sepHeight + bottomHeight;
      topHeight = 100.0 * topHeight / totalHeight;
      sepHeight = 100.0 * sepHeight / totalHeight;
      bottomHeight = 100.0 * bottomHeight / totalHeight;

      $(top).css('bottom', 'auto');
      $(top).css('height', topHeight + "%");
      $(sep).css('top', topHeight + "%");
      $(bottom).css('top', (topHeight + sepHeight) + '%');
      $(bottom).css('height', 'auto');
      if (callback) callback();
    }
  });
}

function makeResizableHPane(left, sep, right, minLeft, minRight, sepWidth, sepOffset, callback)
{
  if (minLeft === undefined) minLeft = 0;
  if (minRight === undefined) minRight = 0;

  makeDraggable($(sep), function(eType, evt, state)
  {
    if (eType == 'dragstart')
    {
      state.startX = evt.pageX;
      state.leftWidth = $(left).width();
      state.rightWidth = $(right).width();
      state.minLeft = minLeft;
      state.maxLeft = (state.leftWidth + state.rightWidth) - minRight;
    }
    else if (eType == 'dragend' || eType == 'dragupdate')
    {
      var change = evt.pageX - state.startX;

      var leftWidth = state.leftWidth + change;
      if (leftWidth < state.minLeft)
      {
        leftWidth = state.minLeft;
      }
      if (leftWidth > state.maxLeft)
      {
        leftWidth = state.maxLeft;
      }
      change = leftWidth - state.leftWidth;

      var rightWidth = state.rightWidth - change;
      newSepWidth = sepWidth;
      if (newSepWidth == undefined) newSepWidth = $(sep).width();
      newSepOffset = sepOffset;
      if (newSepOffset == undefined) newSepOffset = 0;

      if (change == 0)
      {
        if (rightWidth != minRight || state.lastRightWidth == undefined)
        {
          state.lastRightWidth = rightWidth;
          rightWidth = minRight;
        }
        else
        {
          rightWidth = state.lastRightWidth;
          state.lastRightWidth = minRight;
        }
        change = state.rightWidth - rightWidth;
        leftWidth = change + state.leftWidth;
      }

      var totalWidth = leftWidth + newSepWidth + rightWidth;
      leftWidth = 100.0 * leftWidth / totalWidth;
      newSepWidth = 100.0 * newSepWidth / totalWidth;
      newSepOffset = 100.0 * newSepOffset / totalWidth;
      rightWidth = 100.0 * rightWidth / totalWidth;

      $(left).css('right', 'auto');
      $(left).css('width', leftWidth + "%");
      $(sep).css('left', (leftWidth + newSepOffset) + "%");
      $(right).css('left', (leftWidth + newSepWidth) + '%');
      $(right).css('width', 'auto');
      if (callback) callback();
    }
  });
}

exports.makeDraggable = makeDraggable;