corrade-nucleus-nucleons – Blame information for rev 42

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