2df6f30ec2004b3e7b2e8397dd58e7b496433f3c
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Resources / Public / JavaScript / AdminPanel.js
1 /***************************************************************
2 * Admin Panel drag and drop
3 *
4 * Copyright notice
5 *
6 * (c) 2010-2011 Dmitry Dulepov <dmitry@typo3.org>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
25
26 /**
27 * @author Dmitry Dulepov <dmitry@typo3.org>
28 */
29
30 var TYPO3AdminPanel = function() {
31 this.boxElement = null;
32 this.dragging = false;
33 this.dragElement = document;
34 this.previousMouseUpHandler = null;
35 this.previousMouseMoveHandler = null;
36 this.mouseOffset = {
37 x: 0,
38 y: 0
39 };
40 }
41
42 TYPO3AdminPanel.prototype = {
43
44 init: function(headerElementId, boxElementId) {
45 this.boxElement = document.getElementById(boxElementId);
46 this.setInitialPosition();
47 this.setMouseDownHandler(headerElementId);
48 },
49
50 dragStart: function(event) {
51 if (!this.dragging) {
52 if (!event) {
53 event = window.event;
54 }
55 this.dragging = true;
56 this.setMouseOffsets(event);
57 this.setDragHandlers();
58 }
59 },
60
61 dragEnd: function() {
62 if (this.dragging) {
63 this.dragging = false;
64 this.dragElement.onmouseup = this.previousMouseUpHandler;
65 this.dragElement.onmousemove = this.previousMouseMoveHandler;
66 this.setCookie("admPanelPosX", this.boxElement.style.left);
67 this.setCookie("admPanelPosY", this.boxElement.style.top);
68 }
69 },
70
71 drag: function(event) {
72 if (this.dragging) {
73 if (!event) {
74 event = window.event;
75 }
76 this.boxElement.style.left = (event.clientX + this.mouseOffset.x) + "px";
77 this.boxElement.style.top = (event.clientY + this.mouseOffset.y) + "px";
78 }
79 },
80
81 getCookie: function(name) {
82 var dc = document.cookie;
83 var prefix = name + "=";
84 var begin = dc.indexOf("; " + prefix);
85
86 if (begin == -1) {
87 begin = dc.indexOf(prefix);
88 if (begin != 0) {
89 return null;
90 }
91 } else {
92 begin += 2;
93 }
94
95 var end = dc.indexOf(";", begin);
96 if (end == -1) {
97 end = dc.length;
98 }
99
100 return unescape(dc.substring(begin + prefix.length, end));
101 },
102
103 setCookie: function(name, value) {
104 document.cookie = name + "=" + escape(value);
105 },
106
107 setDragHandlers: function() {
108 var _this = this;
109
110 this.previousMouseUpHandler = this.dragElement.onmouseup;
111 this.dragElement.onmouseup = function() {
112 _this.dragEnd.apply(_this, arguments);
113 }
114 this.previousMouseMoveHandler = this.dragElement.onmousemove;
115 this.dragElement.onmousemove = function() {
116 _this.drag.apply(_this, arguments);
117 }
118 },
119
120 setInitialPosition: function() {
121 this.boxElement.style.position = "absolute";
122
123 var pos = this.getCookie("admPanelPosX");
124 if (pos) {
125 this.boxElement.style.left = pos;
126 }
127 pos = this.getCookie("admPanelPosY");
128 if (pos) {
129 this.boxElement.style.top = pos;
130 }
131 },
132
133 setMouseDownHandler: function(headerElementId) {
134 var _this = this, headerElement = document.getElementById(headerElementId);
135 headerElement.onmousedown = function() {
136 _this.dragStart.apply(_this, arguments);
137 }
138 },
139
140 setMouseOffsets: function(event) {
141 this.mouseOffset.x = this.boxElement.offsetLeft - event.clientX;
142 this.mouseOffset.y = this.boxElement.offsetTop - event.clientY;
143 }
144
145 };