forked from France-ioi/AlgoreaPlatform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layout.js
395 lines (384 loc) · 14.1 KB
/
layout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
'use strict';
/*
// Not currently in use
angular.module('algorea')
.directive('viewButton', function () {
return {
restrict: 'A',
template: '<button type="button" class="btn btn-default btn-xs view-button" ng-click="toggleFullscreen();">'+
'<span class="glyphicon glyphicon-{{layout.buttonClass}}"></span>'+
'</button>',
};
});
*/
// loosely based on https://stackoverflow.com/questions/442404/retrieve-the-position-x-y-of-an-html-element
// Compute the absolute top of the element.
function getAbsoluteTop (element) {
var top = 0;
var rect = element.getBoundingClientRect();
var bodyRect = document.body.getBoundingClientRect(),
elemRect = element.getBoundingClientRect();
top = elemRect.top;
return top;
}
// This directive sets the affix class on its single child element
// when the child's absolute top position is negative.
// The height of the container is also adjusted when the child is
// affixed to avoid changes to the page layout.
affixMeDirective.$inject = ['$window', '$timeout'];
function affixMeDirective ($window, $timeout) {
return {
restrict: 'A',
link: function (scope, element, attrs) {
var isEnabled = false;
function onScroll () {
var child = element.children();
var top = getAbsoluteTop(element[0]);
scope.top = top;
if (top >= 0) {
child.removeClass('affix');
} else {
child.addClass('affix');
element.css('height', child[0].offsetHeight);
}
}
function enable () {
if (!isEnabled) {
$timeout(onScroll, 0);
$window.addEventListener("scroll", onScroll);
isEnabled = true;
}
}
function disable () {
if (isEnabled) {
$window.removeEventListener("scroll", onScroll);
var child = element.children();
child.removeClass('affix');
element.css('height', '');
isEnabled = false;
}
}
scope.$watch(attrs.affixMe, function (val) {
if (val) enable(); else disable();
});
scope.$on('$destroy', disable);
}
};
}
angular.module('algorea').directive('affixMe', affixMeDirective);
// Menus states
angular.module('algorea')
.factory('layoutService', ['$rootScope', function ($rootScope) {
function reset () {
$rootScope.affix = 'toolbar';
$rootScope.showNavTopOverlay = false;
}
$rootScope.$on('$stateChangeSuccess', function(event, toState, toParams) {
reset();
});
reset();
return {
openNavTopOverlay: function () {
$rootScope.showNavTopOverlay = true;
},
closeNavTopOverlay: function () {
$rootScope.showNavTopOverlay = false;
},
openSidebarLeftOverlay: function () {
$rootScope.showSidebarLeftOverlay = true;
},
closeSidebarLeftOverlay: function () {
$rootScope.showSidebarLeftOverlay = false;
},
openMobileTopMenu: function () {
$rootScope.mobileNavTopIsOpen = true;
},
closeMobileTopMenu: function () {
$rootScope.mobileNavTopIsOpen = false;
},
openMobileTopMenuOverlay: function () {
$rootScope.showMobileNavTopOverlay = true;
},
closeMobileTopMenuOverlay: function () {
$rootScope.showMobileNavTopOverlay = false;
},
affixToolbar: function () {
$rootScope.affix = 'toolbar';
},
affixHeader: function () {
$rootScope.affix = 'header';
}
};
}]);
angular.module('algorea')
.controller('layoutController', ['$scope', '$window', '$timeout', '$rootScope', '$interval', '$injector', 'itemService', 'pathService', '$state', 'layoutService', function ($scope, $window, $timeout, $rootScope, $interval, $injector, itemService, pathService, $state, layoutService) {
var pane_west = $('.ui-layout-west');
var pane_center = $('.ui-layout-center');
var container = $('#layoutContainer');
var taskMinWidth = 820;
var nonTaskMinWidth = 400;
var mapService = null;
if (config.domains.current.useMap) {
mapService = $injector.get('mapService');
mapService.setClickedCallback(function(path, lastItem) {
if (lastItem.sType == 'Task' || lastItem.sType == 'Course') {
var pathArray = path.split('/');
var selr = pathArray.length;
var sell = selr -1;
var pathParams = pathService.getPathParams();
if (pathParams.basePathStr == path) {
$scope.layout.closeMap();
} else {
$state.go('contents', {path: path,sell:sell,selr:selr});
}
}
});
}
$scope.mapInfos = {
'mapPossible' : true,
'hasMap':false,
'mapOpened' : false
};
$scope.gotoIndex = function() {
var defaultPathStr = config.domains.current.defaultPath;
if (defaultPathStr.substr(0, 10) == '/contents/') {
defaultPathStr = defaultPathStr.substr(10, defaultPathStr.length);
var pathArray = defaultPathStr.split('/');
var selr = pathArray.length;
var sell = selr -1;
}
$state.go('contents', {path: defaultPathStr,sell:null,selr:null});
}
// $scope.layout will be accesset and set by viewButton directive in a subscope, so
// it must be an object, or prototypal inheritance will mess everything
$scope.layout = {
west_is_open : true,
tablesResized: function() {
},
refreshSizes : function() {
},
buttonClass: "fullscreen",
state: "normal",
navTopIsOpen: true,
goFullscreen: function() {
},
goNormal: function() {
},
hasMap: function(hasMap, firstOpening) {
$scope.mapInfos.hasMap = hasMap;
if (!config.domains.current.useMap) return;
if (hasMap == 'always') {
$scope.layout.openMap();
} else if (hasMap == 'never') {
$scope.layout.closeMap();
} else if (hasMap == 'button' && firstOpening) {
$scope.layout.closeMap();
}
},
openMap: function() {
if (!$scope.mapInfos.mapMode && config.domains.current.useMap) {
if ($scope.mapInfos.hasMap == 'button') {
$scope.layout.navTopIsOpen = true;
}
$scope.mapInfos.mapMode = true;
//$('#footer').hide();
$('#view-right').hide();
$('#map').show();
mapService.show();
}
},
closeMap: function() {
if ($scope.mapInfos.mapMode) {
if ($scope.mapInfos.hasMap == 'button') {
$scope.layout.navTopIsOpen = false;
}
//$('#footer').show();
$('#view-right').show();
$('#map').hide();
$scope.mapInfos.mapMode = false;
}
},
openSidebarLeft: function() {
$rootScope.sidebarLeftIsOpen = true;
layoutService.openSidebarLeftOverlay(); // is hidden in CSS when not mobile layout to enable its visibility when resizing window
$scope.layout.closeNavTopOverlay();
if ($rootScope.isMobileLayout) {
$scope.layout.closeMobileNavTop();
}
},
closeSidebarLeft: function() {
$rootScope.sidebarLeftIsOpen = false;
layoutService.closeSidebarLeftOverlay();
},
closeSidebarLeftOverlay: function() {
$scope.layout.closeSidebarLeft();
layoutService.closeSidebarLeftOverlay();
},
openTaskMenu: function() {
layoutService.affixHeader();
layoutService.openNavTopOverlay();
$scope.layout.navTopIsOpen = true;
},
openMobileTaskMenu: function() {
layoutService.openNavTopOverlay();
$scope.layout.navTopIsOpen = true;
layoutService.openMobileTopMenu();
},
closeMobileTaskMenu: function() {
layoutService.closeNavTopOverlay();
$scope.layout.navTopIsOpen = false;
layoutService.closeMobileTopMenu();
},
closeNavTopOverlay: function() {
if ($rootScope.showNavTopOverlay) {
layoutService.closeNavTopOverlay();
layoutService.affixToolbar();
$scope.layout.navTopIsOpen = false;
}
},
openMobileNavTop: function() {
layoutService.openMobileTopMenuOverlay();
layoutService.openMobileTopMenu();
if ($rootScope.isMobileLayout) {
$scope.layout.closeSidebarLeft();
}
},
closeMobileNavTop: function() {
layoutService.closeMobileTopMenuOverlay();
layoutService.closeMobileTopMenu();
},
breadcrumbsClicked: function(event) {
// do not close menu when user clicks on link
if (!config.domains.current.clickableMenu || event.target.parentNode.parentNode.className.indexOf('breadcrumbs-item') != -1 || event.target.className.indexOf('link-arrow') != -1) {
return;
}
},
toggleRight: function() {
$('#sidebar-right').toggleClass('sidebar-right-toggled');
$('#main-titlebar-community').toggleClass('main-titlebar-community-toggled');
$scope.layout.refreshSizes();
},
setRightIcon: function() {
if ($('#sidebar-right').hasClass('sidebar-right-toggled')) {
$('#main-titlebar-community').addClass('main-titlebar-community-toggled');
}
},
closeRight: function() {
$scope.layout.rightOpen = false;
if ($(window).width() < 1100) {
if ($('#sidebar-right').hasClass('sidebar-right-toggled')) {
$scope.layout.toggleRight();
}
} else {
if (!$('#sidebar-right').hasClass('sidebar-right-toggled')) {
$scope.layout.toggleRight();
}
}
},
openRight: function() {
if ($(window).width() < 1100) {
if (!$('#sidebar-right').hasClass('sidebar-right-toggled')) {
$scope.layout.rightOpen = true;
$scope.layout.toggleRight();
}
} else {
if ($('#sidebar-right').hasClass('sidebar-right-toggled')) {
$scope.layout.rightOpen = true;
$scope.layout.toggleRight();
}
}
}
};
$scope.layout.rightIsFullScreen = function(rightIsFullScreen) {
if (rightIsFullScreen == $rootScope.rightIsFullScreen) {
return;
}
$rootScope.rightIsFullScreen = rightIsFullScreen;
$scope.layout.navTopIsOpen = true;
if (rightIsFullScreen) {
if (!$scope.mapInfos.mapMode) {
$scope.layout.navTopIsOpen = false;
}
$scope.layout.closeRight();
$scope.layout.closeSidebarLeft();
}
$scope.layout.refreshSizes();
};
var isCurrentlyOnePage = !config.domains.current.useLeftNavigation;
$scope.layout.isOnePage = function(isOnePage) {
if (config.domains.current.useLeftNavigation) {
if (typeof isOnePage === 'undefined') {
return isCurrentlyOnePage || $rootScope.mapInfos.mapMode;
}
isCurrentlyOnePage = isOnePage;
} else {
return true;
}
};
// inspired from https://github.com/capaj/ng-tools/blob/master/src/debounce.js
// used on onresize for obvious performance reasons
function debounce(fn, timeout, apply){
apply = angular.isUndefined(apply) ? true : apply;
var nthCall = 0;
return function(){ // intercepting fn
var that = this;
var argz = arguments;
nthCall++;
var later = (function(version){
return function(){
if (version === nthCall){
return fn.apply(that, argz);
}
};
})(nthCall);
return $timeout(later, timeout, apply);
};
}
$scope.layout.separateEditorOK = false;
var lastSeparateEditorOK = false;
$scope.layout.refreshSizes = function() {
if ($rootScope.rightIsFullScreen) { // things are handled automatically for everything but the task layout
var availableMainWidth = $('#main-area').width();
var minWidth = $('#task-right').css('min-width');
if (!minWidth) {minWidth = '0px';}
minWidth = parseInt(minWidth.slice(0,-2));
if (!minWidth) {minWidth = 800;}
if (availableMainWidth - 2*minWidth > 40) {
$scope.layout.separateEditorOK = true;
} else {
$scope.layout.separateEditorOK = false;
}
if (lastSeparateEditorOK != $scope.layout.separateEditorOK) {
$timeout($rootScope.apply);
}
lastSeparateEditorOK = $scope.layout.separateEditorOK;
} else {
$scope.layout.separateEditorOK = false;
}
};
var lastWindowWidth = $(window).width();
$scope.layout.onResize = function () {
// reset the opened/closed status of the left sidebar depending on the
// new window width
var newWindowWidth = $(window).width();
lastWindowWidth = newWindowWidth;
$scope.layout.refreshSizes();
$rootScope.isMobileLayout = newWindowWidth < 700 ? true : false;
}
// resizing on window resizing (tamed)
$window.onresize = debounce($scope.layout.onResize, 100, false);
// function to be called at sync end by the service (it's alive. It's alive...)
$rootScope.refreshSizes = $scope.layout.refreshSizes;
// resizing on state change
$rootScope.$on('$viewContentLoaded', function() {
$rootScope.hasSidebarLeft = $state.current.views && $state.current.views.left.template.length > 1 ? true : false;
if ($rootScope.hasSidebarLeft && !$rootScope.isMobileLayout) {$scope.layout.openSidebarLeft();}
$timeout($scope.layout.refreshSizes, 0); // 100 works here, might have to be changed for slow computers
});
$interval($scope.layout.refreshSizes, 1000);
$scope.$on('layout.taskLayoutChange', $scope.layout.refreshSizes);
$rootScope.sidebarLeftIsOpen = true;
$rootScope.showSidebarLeftOverlay = false;
$rootScope.mobileNavTopIsOpen = false;
$rootScope.isMobileLayout = $(window).width() < 700 ? true : false;
}]);