was.wm.js – Blame information for rev 7

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