1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 base.requireStylesheet('ui.drag_handle');
10 base.exportTo('ui', function() {
13 * Detects when user clicks handle determines new height of container based
14 * on user's vertical mouse move and resizes the target.
16 * @extends {HTMLDivElement}
17 * You will need to set target to be the draggable element
19 var DragHandle = ui.define('x-drag-handle');
21 DragHandle.prototype = {
22 __proto__: HTMLDivElement.prototype,
24 decorate: function() {
25 this.lastMousePos_ = 0;
26 this.onMouseMove_ = this.onMouseMove_.bind(this);
27 this.onMouseUp_ = this.onMouseUp_.bind(this);
28 this.addEventListener('mousedown', this.onMouseDown_);
29 this.target_ = undefined;
30 this.horizontal = true;
31 this.observer_ = new WebKitMutationObserver(
32 this.didTargetMutate_.bind(this));
33 this.targetSizesByModeKey_ = {};
37 return this.target_.className == '' ? '.' : this.target_.className;
45 this.observer_.disconnect();
46 this.target_ = target;
49 this.observer_.observe(this.target_, {
51 attributeFilter: ['class']
56 return this.horizontal_;
62 this.className = 'horizontal-drag-handle';
64 this.className = 'vertical-drag-handle';
68 return !this.horizontal_;
75 forceMutationObserverFlush_: function() {
76 var records = this.observer_.takeRecords();
78 this.didTargetMutate_(records);
81 didTargetMutate_: function(e) {
82 var modeSize = this.targetSizesByModeKey_[this.modeKey_];
83 if (modeSize !== undefined) {
84 this.setTargetSize_(modeSize);
88 // If we hadn't previously sized the target, then just remove any manual
89 // sizing that we applied.
90 this.target_.style[this.targetStyleKey_] = '';
93 get targetStyleKey_() {
94 return this.horizontal_ ? 'height' : 'width';
97 getTargetSize_: function() {
98 // If style is not set, start off with computed height.
99 var targetStyleKey = this.targetStyleKey_;
100 if (!this.target_.style[targetStyleKey]) {
101 this.target_.style[targetStyleKey] =
102 window.getComputedStyle(this.target_)[targetStyleKey];
104 var size = parseInt(this.target_.style[targetStyleKey]);
105 this.targetSizesByModeKey_[this.modeKey_] = size;
109 setTargetSize_: function(s) {
110 this.target_.style[this.targetStyleKey_] = s + 'px';
111 this.targetSizesByModeKey_[this.modeKey_] = s;
114 applyDelta_: function(delta) {
115 // Apply new size to the container.
116 var curSize = this.getTargetSize_();
118 if (this.target_ === this.nextSibling) {
119 newSize = curSize + delta;
121 newSize = curSize - delta;
123 this.setTargetSize_(newSize);
126 onMouseMove_: function(e) {
127 // Compute the difference in height position.
128 var curMousePos = this.horizontal_ ? e.clientY : e.clientX;
129 var delta = this.lastMousePos_ - curMousePos;
131 this.applyDelta_(delta);
133 this.lastMousePos_ = curMousePos;
138 onMouseDown_: function(e) {
141 this.forceMutationObserverFlush_();
142 this.lastMousePos_ = this.horizontal_ ? e.clientY : e.clientX;
143 document.addEventListener('mousemove', this.onMouseMove_);
144 document.addEventListener('mouseup', this.onMouseUp_);
149 onMouseUp_: function(e) {
150 document.removeEventListener('mousemove', this.onMouseMove_);
151 document.removeEventListener('mouseup', this.onMouseUp_);
157 DragHandle: DragHandle