was.wm.js – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 // Load the window manager desktop icons.
2 function loadWindowManagerIcons(path) {
3 // Search and load all nucleons.
4 $.get(path, function(nucleons) {
5 var ix = 0, iy = 0;
6 var states = {};
7 var cookie = Cookies.get('window-manager-icons');
8 if(cookie)
9 states = JSON.parse(cookie);
10 $.each(nucleons, function(index, file) {
11 // Skip files starting with full stop (POSIX).
12 if (/^\./.test(file)) return;
13 $.get(path + '/' + file, function(data) {
14 data = $(data).get(0);
15 var nucleon = data.getAttribute('data-target');
16  
17 if(typeof states[nucleon] !== 'undefined') {
18 var sx = states[nucleon].x,
19 sy = states[nucleon].y;
20  
21 data.style.webkitTransform =
22 data.style.transform =
23 'translate(' + sx + 'px, ' + sy + 'px)';
24  
25 // Update the position attributes
26 data.setAttribute('data-x', sx);
27 data.setAttribute('data-y', sy);
28  
29 $('#window-manager-desktop').append(data);
30 return;
31 }
32  
33 // Translate the desktop icons so they do not overlap.
34 data.style.webkitTransform =
35 data.style.transform =
36 'translate(' + ix + 'px, ' + iy + 'px)';
37  
38 // Update the position attributes
39 data.setAttribute('data-x', ix);
40 data.setAttribute('data-y', iy);
41  
42 $('#window-manager-desktop').append(data);
43 // Translate by the desktop icon size.
44 ix += 64;
45 });
46 });
47 });
48 }
49  
50 // Open a window manager window.
51 function openWindowManagerWindow(nucleon) {
52 // If the nucleon already exists, then do not append the content again.
53 if($('#' + nucleon).length)
54 return;
55  
56 // Store the icon.
57 var nucleon_icon = $('#' + nucleon + '-window-manager-button-icon').attr('src');
58  
59 // Change the nucleon icon to a loading icon.
60 $('#' + nucleon + '-window-manager-button-icon').attr('src', '/img/loader.gif');
61  
62 // Disable button for successive clicks.
63 $('#' + nucleon + '-window-manager-button').prop('disabled', true);
64  
65 // Load the nucleon.
66 $.get('/' + nucleon + '/index.html', function(data) {
67 // Get the DOM object.
68 data = $(data).get(0);
69  
70 // Select all new scripts that have to be inserted into the DOM.
71 var nucleonScripts = [];
72 $(data).find('script').each(function() {
73 var src = $(this).attr('src');
74 if(src === undefined)
75 return;
76 nucleonScripts[src] = $(this);
77 });
78 // Remove scripts from the nucleon if they are already added to the DOM.
79 $(document).find('script').each(function() {
80 var src = $(this).attr('src');
81 if(nucleonScripts[src] === undefined)
82 return;
83 nucleonScripts[src].remove();
84 });
85  
86 // Restore windows to their saved position.
87 var cookie = Cookies.get('window-manager-windows');
88 if(cookie) {
89 var states = JSON.parse(cookie);
90 if(typeof states[nucleon] !== 'undefined') {
91 var x = states[nucleon].x,
92 y = states[nucleon].y,
93 wm = $(data).find('.window-manager-window').get(0);
94  
95 wm.style.webkitTransform =
96 wm.style.transform =
97 'translate(' + x + 'px, ' + y + 'px)';
98  
99 // Update the position attributes
100 wm.setAttribute('data-x', x);
101 wm.setAttribute('data-y', y);
102 }
103 }
104 $(data).hide().appendTo('#window-manager-desktop').fadeIn(750);
105 }).done(function(data) {
106 // Change the nucleon icon back.
107 $('#' + nucleon + '-window-manager-button-icon').attr('src', nucleon_icon);
108 // Enable the button.
109 $('#' + nucleon + '-window-manager-button').prop('disabled', false);
110 });
111 }
112  
113 // Retrieve the top-most window manager window z-index.
114 function getTopWindowIndex() {
115 return Math.max.apply(null,
116 $.map($('.window-manager-window'), function(e, n) {
117 if ($(e).css('position') != 'static')
118 return parseInt($(e).css('z-index')) || 1;
119 })
120 );
121 }
122  
123 // Draggable and moveable windows.
124 $(document).ready(() => {
125 // Window manager windows.
126 interact('.window-manager-window')
127 .draggable({
128 // enable inertial throwing
129 inertia: true,
130 // keep the element within the area of it's parent
131 restrict: {
132 restriction: $('#window-manager-desktop').get(0),
133 endOnly: true,
134 elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
135 },
136  
137 // enable autoScroll
138 autoScroll: true,
139  
140 onmove: dragMoveListener,
141 onend: function (event) {
142 event.target.style.opacity = 1;
143 },
144 onstart: function(event) {
145 // Move windows to top on drag.
146 event.target.style.zIndex = getTopWindowIndex() + 1;
147 event.target.style.opacity = 0.5;
148 }
149 })
150 .allowFrom('.panel-heading');
151  
152 // Window manager icons.
153 interact('.window-manager-icon')
154 .draggable({
155 // enable inertial throwing
156 inertia: false,
157 // keep the element within the area of it's parent
158 restrict: {
159 restriction: $('#window-manager-desktop').get(0),
160 endOnly: true,
161 elementRect: { top: 0, left: 0, bottom: 1, right: 1 }
162 },
163 snap: {
164 targets: [
165 interact.createSnapGrid({ x: 64, y: 64 })
166 ],
167 range: Infinity,
168 relativePoints: [ { x: 0, y: 0 } ]
169 },
170 // enable autoScroll
171 autoScroll: true,
172  
173 onmove: dragMoveListener,
174 onend: function (event) {
175 event.target.style.opacity = 1;
176 },
177 onstart: function(event) {
178 event.target.style.opacity = 0.5;
179 }
180 });
181  
182 // Closing windows.
183 $(document).on('click', '.window-manager-close-button', function(event) {
184 var wm = $('#' + $(this).data('target'));
185 wm.fadeOut(750, function() {
186 wm.remove();
187 });
188 });
189  
190 // Windows clicking brings to front.
191 $(document).on('click', '.window-manager-window', function(event) {
192 $(event.target)
193 .closest('.window-manager-window')
194 .css('z-index', getTopWindowIndex() + 1);
195 });
196  
197 function dragMoveListener (event) {
198 var target = event.target,
199 // keep the dragged position in the data-x/data-y attributes
200 x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx,
201 y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
202  
203 // translate the element
204 target.style.webkitTransform =
205 target.style.transform =
206 'translate(' + x + 'px, ' + y + 'px)';
207  
208 // update the position attributes
209 target.setAttribute('data-x', x);
210 target.setAttribute('data-y', y);
211  
212 if(target.classList.contains('window-manager-window')) {
213 // Save the window position.
214 var states = {};
215 var cookie = Cookies.get('window-manager-windows');
216 if(cookie)
217 states = JSON.parse(cookie);
218  
219 states[$(event.target).data('target')] = {
220 x: x,
221 y: y
222 };
223  
224 Cookies.set('window-manager-windows', states, { path: '' });
225 }
226  
227 if(target.classList.contains('window-manager-icon')) {
228 // Save the icon position.
229 var states = {};
230 var cookie = Cookies.get('window-manager-icons');
231 if(cookie)
232 states = JSON.parse(cookie);
233  
234 states[$(event.target).data('target')] = {
235 x: x,
236 y: y
237 };
238  
239 Cookies.set('window-manager-icons', states, { path: '' });
240 }
241 }
242  
243 // this is used later in the resizing and gesture demos
244 window.dragMoveListener = dragMoveListener;
245 });