4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
12 * Movable mixin. Makes controls movable absolute and relative to other elements.
14 * @mixin tinymce.ui.Movable
16 define("tinymce/ui/Movable", [
18 ], function(DomUtils) {
21 function calculateRelativePosition(ctrl, targetElm, rel) {
22 var ctrlElm, pos, x, y, selfW, selfH, targetW, targetH, viewport;
24 viewport = DomUtils.getViewPort();
27 pos = DomUtils.getPos(targetElm);
37 ctrlElm = ctrl.getEl();
38 selfW = ctrlElm.offsetWidth;
39 selfH = ctrlElm.offsetHeight;
42 targetW = targetElm.offsetWidth;
43 targetH = targetElm.offsetHeight;
46 rel = (rel || '').split('');
58 y += Math.round(targetH / 2);
62 x += Math.round(targetW / 2);
75 y -= Math.round(selfH / 2);
79 x -= Math.round(selfW / 2);
92 * Tests various positions to get the most suitable one.
95 * @param {DOMElement} elm Element to position against.
96 * @param {Array} rels Array with relative positions.
97 * @return {String} Best suitable relative position.
99 testMoveRel: function(elm, rels) {
100 var viewPortRect = DomUtils.getViewPort();
102 for (var i = 0; i < rels.length; i++) {
103 var pos = calculateRelativePosition(this, elm, rels[i]);
106 if (pos.x > 0 && pos.x + pos.w < viewPortRect.w && pos.y > 0 && pos.y + pos.h < viewPortRect.h) {
110 if (pos.x > viewPortRect.x && pos.x + pos.w < viewPortRect.w + viewPortRect.x &&
111 pos.y > viewPortRect.y && pos.y + pos.h < viewPortRect.h + viewPortRect.y) {
121 * Move relative to the specified element.
124 * @param {Element} elm Element to move relative to.
125 * @param {String} rel Relative mode. For example: br-tl.
126 * @return {tinymce.ui.Control} Current control instance.
128 moveRel: function(elm, rel) {
129 if (typeof(rel) != 'string') {
130 rel = this.testMoveRel(elm, rel);
133 var pos = calculateRelativePosition(this, elm, rel);
134 return this.moveTo(pos.x, pos.y);
138 * Move by a relative x, y values.
141 * @param {Number} dx Relative x position.
142 * @param {Number} dy Relative y position.
143 * @return {tinymce.ui.Control} Current control instance.
145 moveBy: function(dx, dy) {
146 var self = this, rect = self.layoutRect();
148 self.moveTo(rect.x + dx, rect.y + dy);
154 * Move to absolute position.
157 * @param {Number} x Absolute x position.
158 * @param {Number} y Absolute y position.
159 * @return {tinymce.ui.Control} Current control instance.
161 moveTo: function(x, y) {
164 // TODO: Move this to some global class
165 function contrain(value, max, size) {
170 if (value + size > max) {
172 return value < 0 ? 0 : value;
178 if (self.settings.constrainToViewport) {
179 var viewPortRect = DomUtils.getViewPort(window);
180 var layoutRect = self.layoutRect();
182 x = contrain(x, viewPortRect.w + viewPortRect.x, layoutRect.w);
183 y = contrain(y, viewPortRect.h + viewPortRect.y, layoutRect.h);
186 if (self._rendered) {
187 self.layoutRect({x: x, y: y}).repaint();
193 self.fire('move', {x: x, y: y});