Gitlab CSE Unil

Commit 7059eafd authored by Julien Furrer's avatar Julien Furrer
Browse files

Tons of optimizations and small fixes

parent 5b1a198c
......@@ -43,8 +43,6 @@ class AnObjViewSet(viewsets.ModelViewSet):
Return only owner's anobjs for write actions and
return also shared anobjs to guests for read actions
"""
# return AnObj.objects.filter(owner=self.request.user)
user = self.request.user
q = Q(owner=user)
if self.action == 'list':
......
......@@ -11,7 +11,10 @@ module.exports = function(grunt) {
banner: '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %> - ' +
'Copyright (C) <%= grunt.template.today("yyyy") %> Université de Lausanne - ' +
'see licence */' + grunt.util.linefeed
'see licence */' + grunt.util.linefeed,
compress: {
drop_console: false
}
},
files: [{
expand: true,
......
......@@ -165,10 +165,12 @@ function ($, paper, Signal, Config, view) {
getAutoSave: function() { return _autoSave; },
loadData: function (url) {
console.time("loadData");
$.ajax({
url: url,
dataType: "json",
success: function(annotableData) {
console.timeEnd("loadData");
_events.annotableLoaded.dispatch(annotableData);
},
error: function(){
......
......@@ -76,25 +76,31 @@ define([
});
var ADIM = {
events: {
imageLoaded: view.events.imageLoaded
},
view: {
_view: view,
resize: view.resize,
showLayer: view.showLayer,
hideLayer: view.hideLayer,
toggleLayer: view.toggleLayer
},
io: io,
ui: ui,
tools: tools,
config: config
};
window[config.adim_global_varname] = ADIM;
if (config.ready && typeof config.ready === 'function') {
config.ready(ADIM);
}
// The ``adim_global_varname`` var in not used in the code.
// The global ADIM var is neither used directly (window.ADIM or just ADIM)
// The idea of passing an ADIM object to a callback defined in config was interesting
// but is currently not used. The whole stuff will be commented for now.
//
//var ADIM = {
// events: {
// imageLoaded: view.events.imageLoaded
// },
// view: {
// _view: view,
// resize: view.resize,
// showLayer: view.showLayer,
// hideLayer: view.hideLayer,
// toggleLayer: view.toggleLayer
// },
// io: io,
// ui: ui,
// tools: tools,
// config: config
//};
//
//// window[config.adim_global_varname] = ADIM; // <-- FIXME: should be removed, it could be a security issue
//
//if (config.ready && typeof config.ready === 'function') {
// config.ready(ADIM);
//}
});
\ No newline at end of file
......@@ -18,7 +18,6 @@
* @CREATION-DATE: 01.09.14
*
*/
define([
"jquery",
"paper",
......@@ -37,84 +36,86 @@ define([
"helper/exporter"
],
function ($, paper, Signal, config, view, io, tools, attributes, ui, Users, theme, fileUploader, exporter) {
var _events = {};
var CANVAS_EL_ID = 'my-canvas';
console.timeEnd("main app loading");
function initSignalRouting() {
// ----- Events Listeners -----------------------------
tools.events.annotationAdded.add(function(annotation) {
io.saveAnnotations(annotation);
});
tools.events.annotationChanged.add(function(annotations) {
io.saveAnnotations(annotations);
});
tools.events.annotationAdded.add(function(annotation) {
io.saveAnnotations(annotation);
});
attributes.events.annotationChanged.add(function(annotations, forceAll) {
if (forceAll) {
io.saveUserAnnotations();
} else {
tools.events.annotationChanged.add(function(annotations) {
io.saveAnnotations(annotations);
}
});
});
view.events.annotationPropertyChanged.add(function(annotation){
io.saveAnnotations([annotation]);
});
attributes.events.annotationChanged.add(function(annotations, forceAll) {
if (forceAll) {
io.saveUserAnnotations();
} else {
io.saveAnnotations(annotations);
}
});
view.events.annotationRemoved.add(function(annotId) {
io.removeAnnotation(annotId);
});
view.events.annotationPropertyChanged.add(function(annotation){
io.saveAnnotations([annotation]);
});
ui.events.saveButClick.add(function(){
io.saveUserAnnotations();
});
view.events.annotationRemoved.add(function(annotId) {
io.removeAnnotation(annotId);
});
ui.events.autoSaveChanged.add(function(autoSave){
//io.setAutoSave(autoSave);
});
ui.events.saveButClick.add(function(){
io.saveUserAnnotations();
});
/**
* When an annotation is created, it has a temporary id until it is saved
* to the DB. After saving, we use the temporary id to retrieve the Annotation
* and give it the new id, generated by th DB
*/
io.events.annotationAdded.add(function(data){
// Unserialize item
var itemData = JSON.parse(data.item);
// PlacedSymbol are serialized as an array of two items,
// We want the second one
if ($.isArray(itemData[0])) {
itemData = itemData[1];
}
ui.events.autoSaveChanged.add(function(autoSave){
//io.setAutoSave(autoSave);
});
var tId = itemData[1].data.tId;
if (tId) {
var item = view.getItemByTempId(tId);
if (item) {
item.data.id = data.id;
delete(item.data.tId); // FIXME: It doesn't work, tID is still in the data
/**
* When an annotation is created, it has a temporary id until it is saved
* to the DB. After saving, we use the temporary id to retrieve the Annotation
* and give it the new id, generated by th DB
*/
io.events.annotationAdded.add(function(data){
// Unserialize item
var itemData = JSON.parse(data.item);
// PlacedSymbol are serialized as an array of two items,
// We want the second one
if ($.isArray(itemData[0])) {
itemData = itemData[1];
}
}
if (config.autoSaveThumbnail) {
sendThumbnail();
}
});
var tId = itemData[1].data.tId;
if (tId) {
var item = view.getItemByTempId(tId);
if (item) {
item.data.id = data.id;
delete(item.data.tId); // FIXME: It doesn't work, tID is still in the data
}
}
if (config.autoSaveThumbnail) {
sendThumbnail();
}
});
io.events.annotationSaved.add(function(data){
if (config.autoSaveThumbnail) {
sendThumbnail();
}
});
io.events.annotationRemoved.add(function(){
if (config.autoSaveThumbnail) {
sendThumbnail();
}
});
io.events.annotationSaved.add(function(data){
if (config.autoSaveThumbnail) {
sendThumbnail();
}
});
io.events.annotationRemoved.add(function(){
if (config.autoSaveThumbnail) {
sendThumbnail();
}
});
}
initSignalRouting();
var sendThumbnail = (function() {
var throttleDelay = 5 * 1000, // Waiting time before executing job
......@@ -164,6 +165,7 @@ define([
$(function(){
console.time("main dom ready");
//theme.updateTheme();
if (!view.init(CANVAS_EL_ID)) {
......@@ -171,11 +173,6 @@ define([
return;
}
if (config.mode === config.MODE_EDIT) {
var userLayer = view.getOrCreateUserLayer(config.user.id, config.user.username);
userLayer.opacity = 1;
}
attributes.init();
ui.init();
......@@ -296,50 +293,59 @@ define([
}
});
// Stries
var concordantstrTool = tools.tools['concordantstr'];
var discordantstrTool = tools.tools['discordantstr'];
var cmsareaTool = tools.tools['cmsarea'];
// FIXME: A supprimer
//// Stries
//var concordantstrTool = tools.tools['concordantstr'];
//var discordantstrTool = tools.tools['discordantstr'];
//var cmsareaTool = tools.tools['cmsarea'];
// ----- Initialise les tools avec les données de l'annotable -----
io.events.annotableLoaded.add(function(annotableData){
// Load the image
// view.loadImage({url: annotableData.image_url, bgUrl: annotableData.image_bg_url},
// Add known users to the Users module
Users.addUsers([{id: annotableData.owner, username: annotableData.owner_name}]);
//Users.addUsers(annotableData.members);
//Users.addUsers(annotableData.members); // members are only loaded if needed. this statement should be removed
// Load the image
console.time("loadImage");
view.loadImage({url: config.annotable.image},
function(){
console.timeEnd("loadImage");
console.time("loadAnnotations");
view.loadAnnotations(annotableData.annotations, config.user.id);
console.timeEnd("loadAnnotations");
}
);
});
console.timeEnd("main dom ready");
});
var ADIM = {
events: {
imageLoaded: view.events.imageLoaded
},
view: {
_view: view,
resize: view.resize,
showLayer: view.showLayer,
hideLayer: view.hideLayer,
toggleLayer: view.toggleLayer
},
io: io,
ui: ui,
tools: tools,
config: config
};
window[config.adim_global_varname] = ADIM;
window.DBG = Users;
if (config.ready && typeof config.ready === 'function') {
config.ready(ADIM);
}
// The ``adim_global_varname`` var in not used in the code.
// The global ADIM var is neither used directly (window.ADIM or just ADIM)
// The idea of passing an ADIM object to a callback defined in config was interesting
// but is currently not used. The whole stuff will be commented for now.
//var ADIM = {
// events: {
// imageLoaded: view.events.imageLoaded
// },
// view: {
// _view: view,
// resize: view.resize,
// showLayer: view.showLayer,
// hideLayer: view.hideLayer,
// toggleLayer: view.toggleLayer
// },
// io: io,
// ui: ui,
// tools: tools,
// config: config
//};
//
// window[config.adim_global_varname] = ADIM; // <-- FIXME: should be removed, it could be a security issue
//
//if (config.ready && typeof config.ready === 'function') {
// config.ready(ADIM);
//}
});
\ No newline at end of file
......@@ -119,6 +119,7 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
view.events.imageLoaded.add(function(evt) {
adjustCanvasToImage(evt.raster);
$("#page-loader").remove();
console.timeEnd("complete app");
});
view.events.annotationsLoaded.add(function() {
......@@ -129,6 +130,8 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
updateSaveButState();
});
view.events.userLayerVisibilityChanged.add(onUserLayerVisChange);
tools.events.annotationSelected.add(function(item) {
// ----- Update Properties Panel -----
// disable properties panel if there is no item or more than one
......@@ -201,7 +204,6 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
}
});
io.events.loadingError.add(function(err){
var pageLoader = $("#page-loader");
if (pageLoader.length) {
......@@ -275,10 +277,7 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
// Initialize reserved width and height
updateReservedSpace();
//_reservedWidth = ( (config.mode === config.MODE_EDIT ? _toolsCol.outerWidth() : 0 ) +
// ( (config.ui.show_side_col) ? _sideCol.outerWidth() + 24 : 50 )
//);
//_reservedHeight = ( $("#canvas-col").offset().top + _canvasToolBar.outerHeight() + 20 );
// The amount of window width needed for navbar elements other than anobj title
var _$mainNavBar = $("#main-navbar");
......@@ -296,12 +295,44 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
return value;
})();
_$w.on('resize', function(){
adjustCanvasToImage();
_$mainNavBar.find(".adim-image-name .text").css("max-width", _$w.width() - _navBarTitleDelta);
// Window Resize event (debounced version)
// -------------------
(function(){ // isolate to avoid useless pollution of current scope
var $b = $("body"),
bW = $b.outerWidth(), bH = $b.outerHeight(),
startResize = _.debounce(function(){
if (!$b.data("resize-fixed")) {
$b.css({
width: bW, maxWidth: bW, minWidth: bW,
height: bH, maxHeight: bH, minHeight: bH
});
$b.data("resize-fixed", true);
}
}, 50, true),
afterResize = _.debounce(function(){
$b.css({
width: "auto", maxWidth: "none", minWidth: 0,
height: "auto", maxHeight: "none", minHeight: 0
});
$b.data("resize-fixed", false);
bW = $b.outerWidth();
bH = $b.outerHeight();
adjustCanvasToImage();
_$mainNavBar.find(".adim-image-name .text").css("max-width", _$w.width() - _navBarTitleDelta);
}, 100)
;
_$w.on('resize', function(event){
startResize();
afterResize();
//adjustCanvasToImage();
//_$mainNavBar.find(".adim-image-name .text").css("max-width", _$w.width() - _navBarTitleDelta);
});
})();
// TODO: à quoi sert ce code ?
// Update the scrollbar of the panels that needs it. Depends on the available height
$("#side-col-panel-group .panel-collapse")
.on('hidden.bs.collapse shown.bs.collapse', function () {
_adjustCanvasFrame();
......@@ -321,27 +352,31 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
updateViewMode(mode);
});
$(".adim-view-mod-tabs").find("[data-toggle=tooltip]").tooltip({
placement: 'bottom',
delay: { "show": 500, "hide": 50 }
});
// Fullscreen mode
(function(enabled){
if (!enabled) return;
var el = $('body').get(0);
var requestFullscreen =
el.requestFullscreen ? el.requestFullscreen :
el.mozRequestFullScreen ? el.mozRequestFullScreen :
el.webkitRequestFullscreen ? el.webkitRequestFullscreen :
el.msRequestFullscreen ? el.msRequestFullscreen : null;
;
if (requestFullscreen) {
$("#navbut-fullscreen").removeClass("hidden")
.find("a").click(function(event){
event.preventDefault;
requestFullscreen.call($('body').get(0));
return false;
});
}
})();
//// Fullscreen mode
//(function(enabled){
// if (!enabled) return;
// var el = $('body').get(0);
// var requestFullscreen =
// el.requestFullscreen ? el.requestFullscreen :
// el.mozRequestFullScreen ? el.mozRequestFullScreen :
// el.webkitRequestFullscreen ? el.webkitRequestFullscreen :
// el.msRequestFullscreen ? el.msRequestFullscreen : null;
// ;
// if (requestFullscreen) {
// $("#navbut-fullscreen").removeClass("hidden")
// .find("a").click(function(event){
// event.preventDefault;
// requestFullscreen.call($('body').get(0));
// return false;
// });
// }
//
//})();
$("#but-zoom-fit").on('click', zoomFitContent);
$("#but-zoom-100").on('click', function(event) {
......@@ -515,18 +550,26 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
* Theses values are used to resize the canvas when the size of the window is changed.
* used by adjustCanvasToImage()
*/
function updateReservedSpace() {
_reservedWidth = (
// Tools column
( _toolsCol.is(":visible") ? _toolsCol.outerWidth() : 0 ) +
// Side column
( ! config.ui.show_side_col ? 50 :
_sideCol.is(".collapsed") ? 32 : _sideCol.outerWidth()
) +
24
);
_reservedHeight = ( $("#canvas-col").offset().top + _canvasToolBar.outerHeight() + 20 );
}
var updateReservedSpace = function(){
var colTop = $("#canvas-col").offset().top,
toolsWidth = _toolsCol.outerWidth(),
tbHeight = _canvasToolBar.outerHeight();
updateReservedSpace = function () {
//if (!colTop) colTop = $("#canvas-col").offset().top;
_reservedWidth = (
// Tools column
( _toolsCol.is(":visible") ? toolsWidth : 0 ) +
// Side column
( ! config.ui.show_side_col ? 50 :
_sideCol.is(".collapsed") ? 32 : _sideCol.outerWidth()
) +
24
);
_reservedHeight = (colTop + tbHeight + 20 );
};
updateReservedSpace();
};
/**
* Update the view mode: 'edit' or 'review'
......@@ -892,6 +935,7 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
// Auto adjust zoom to fit content
var imageLayer = view.getLayerByName('image');
var raster = imageLayer.children[1];
if (!raster) return;
var s = paper.view.viewSize.divide(raster.size);
attributes.setZoom(Math.min(s.width, s.height), 'center');
paper.view.draw();
......@@ -915,6 +959,35 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
].join(""));
/**
* Build the list of users' layer in the Users Panel
*/
function updateUserLayers() {
if (!config.ui.show_users_results) return;
$.each(view.getAllUsersLayers(), function(i, layer) {
if (annotList.find("li[data-layer-id=" + layer.id + "]").length == 0)
$(layerListItemTpl({
id:layer.id,
visibleClass:visibleClass,
name: layer.data.owner || layer.name
}))
.prependTo(annotList);
});
}
var getListItem = _.memoize(
function (layer) {
return annotList.find("li[data-layer-id=" + layer.id + "]");
},
function(l){ return l.id }
);
function onUserLayerVisChange(layer, visible) {
updateUserLayerState(getListItem(layer), visible);
}
function selectLayerListItem(item, exclusive) {
var $item = $(item);
//if ($item.hasClass("selected")) {
......@@ -931,14 +1004,12 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
selectedItems.removeClass("selected");
$item.addClass("selected");
//}
var layer = view.getLayerByName("id:" + $item.data("layerId"));
var layer = view.getLayerByName($item.data("layerId"));
if (layer)
layer.activate();
}
function toggleLayer(listItem, show) {
var visible = view.toggleLayer("id:" + listItem.data("layerId"), show);
function updateUserLayerState(listItem, visible) {
listItem
.toggleClass("layer-hidden", !visible)
.find(".layer-status")
......@@ -947,6 +1018,10 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
;
}
function toggleLayer(listItem, show) {
view.toggleLayer(listItem.data("layerId"), show);
}
function toggleAll(show) {
var s = show;
annotList.find(".list-group-item").each(function(i, layer){
......@@ -955,7 +1030,8 @@ function($, _, Signal, paper, config, view, io, tools, attributes, Users, export
});
}