/*! AdIm - v0.0.0 - 2015-04-16 - Copyright (C) 2015 Université de Lausanne - see licence */
/*! AdIm - v0.0.0 - 2015-04-17 - Copyright (C) 2015 Université de Lausanne - see licence */
define('adim/io',["jquery","paper","signals","adim/config","adim/view"],function(a,b,c,d,e){function f(){}function g(b,c,e,f){b=parseInt(b,10);var g=!b;f=parseInt(f,10)||0;var h={item:c,order:f};if(e)for(var i in e)h[i]=e[i];var j,k=d.api.annotations;g?(j="POST",a.extend(h,{annotable:d.annotable.id,owner:d.user.id,owner_id:d.user.id})):(j="PATCH",k+=b+"/"),a.ajax({url:k,type:j,dataType:"json",data:h,success:function(a,b,c){201===c.status?l.annotationAdded.dispatch(a):200===c.status&&l.annotationSaved.dispatch(a)},error:function(){l.loadingError.dispatch({msg:"Erreur lors de l'enregistrement d'une annotation"})}})}function h(b,c){var e=new FormData;return e.append("aid",b),e.append("file",c),a.ajax({type:"POST",url:d.annotable.thumbnail,data:e,processData:!1,contentType:!1}).done(function(){})}function i(a){for(var b,c=0;b=a[c];c++)g(b.id,b.itemStr,b.prop,b.index)}function j(b){a.ajax({url:d.api.annotations+parseInt(b,10)+"/",type:"DELETE",success:function(a,c,d){204===d.status&&l.annotationRemoved.dispatch(b)},error:function(a){404!==a.status&&l.loadingError.dispatch({msg:"Erreur lors de la suppression d'une annotation"})}})}function k(b,c){return a.ajax({url:d.api.annotables+parseInt(b,10)+"/",type:"PATCH",dataType:"json",data:{name:c},success:function(a){l.annotableChanged.dispatch(a)}})}var l={annotableLoaded:new c,annotableChanged:new c,annotationsLoaded:new c,loadingError:new c,annotationAdded:new c,annotationSaved:new c,annotationRemoved:new c},m=!1;return{init:f,events:l,setAutoSave:function(a){return m=!!a},getAutoSave:function(){return m},loadData:function(b){console.time("loadData"),a.ajax({url:b,dataType:"json",success:function(a){console.timeEnd("loadData"),l.annotableLoaded.dispatch(a)},error:function(){l.loadingError.dispatch({msg:"Une erreur est survenue lors du chargement des données."})}})},loadSharedAnnotations:function(b){var c=a.Deferred();return a.ajax({url:b,dataType:"json",success:function(a){c.resolve(a)},error:function(){var a={msg:"Une erreur est survenue lors du chargement des données."};l.loadingError.dispatch(a),c.reject(a)}}),c},saveUserAnnotations:function(a){a=a||d.user.id;var b=e.getUserAnnnotationsSerialized(a);return i(b),!0},saveAnnotations:function(a,b){if(!m&&!b)return!1;var c=e.getAnnotationsSerialized(a);return i(c),!0},removeAnnotation:j,saveThumbnail:h,setAnnotableName:k}});
/*! AdIm - v0.0.0 - 2015-04-16 - Copyright (C) 2015 Université de Lausanne - see licence */
/*! AdIm - v0.0.0 - 2015-04-17 - Copyright (C) 2015 Université de Lausanne - see licence */
/*! AdIm - v0.0.0 - 2015-04-16 - Copyright (C) 2015 Université de Lausanne - see licence */
/*! AdIm - v0.0.0 - 2015-04-17 - Copyright (C) 2015 Université de Lausanne - see licence */
define('adim/io',["jquery","paper","signals","adim/config","adim/view"],function(a,b,c,d,e){function f(){}function g(b,c,e,f){b=parseInt(b,10);var g=!b;f=parseInt(f,10)||0;var h={item:c,order:f};if(e)for(var i in e)h[i]=e[i];var j,k=d.api.annotations;g?(j="POST",a.extend(h,{annotable:d.annotable.id,owner:d.user.id,owner_id:d.user.id})):(j="PATCH",k+=b+"/"),a.ajax({url:k,type:j,dataType:"json",data:h,success:function(a,b,c){201===c.status?l.annotationAdded.dispatch(a):200===c.status&&l.annotationSaved.dispatch(a)},error:function(){l.loadingError.dispatch({msg:"Erreur lors de l'enregistrement d'une annotation"})}})}function h(b,c){var e=new FormData;return e.append("aid",b),e.append("file",c),a.ajax({type:"POST",url:d.annotable.thumbnail,data:e,processData:!1,contentType:!1}).done(function(){})}function i(a){for(var b,c=0;b=a[c];c++)g(b.id,b.itemStr,b.prop,b.index)}function j(b){a.ajax({url:d.api.annotations+parseInt(b,10)+"/",type:"DELETE",success:function(a,c,d){204===d.status&&l.annotationRemoved.dispatch(b)},error:function(a){404!==a.status&&l.loadingError.dispatch({msg:"Erreur lors de la suppression d'une annotation"})}})}function k(b,c){return a.ajax({url:d.api.annotables+parseInt(b,10)+"/",type:"PATCH",dataType:"json",data:{name:c},success:function(a){l.annotableChanged.dispatch(a)}})}var l={annotableLoaded:new c,annotableChanged:new c,annotationsLoaded:new c,loadingError:new c,annotationAdded:new c,annotationSaved:new c,annotationRemoved:new c},m=!1;return{init:f,events:l,setAutoSave:function(a){return m=!!a},getAutoSave:function(){return m},loadData:function(b){console.time("loadData"),a.ajax({url:b,dataType:"json",success:function(a){console.timeEnd("loadData"),l.annotableLoaded.dispatch(a)},error:function(){l.loadingError.dispatch({msg:"Une erreur est survenue lors du chargement des données."})}})},loadSharedAnnotations:function(b){var c=a.Deferred();return a.ajax({url:b,dataType:"json",success:function(a){c.resolve(a)},error:function(){var a={msg:"Une erreur est survenue lors du chargement des données."};l.loadingError.dispatch(a),c.reject(a)}}),c},saveUserAnnotations:function(a){a=a||d.user.id;var b=e.getUserAnnnotationsSerialized(a);return i(b),!0},saveAnnotations:function(a,b){if(!m&&!b)return!1;var c=e.getAnnotationsSerialized(a);return i(c),!0},removeAnnotation:j,saveThumbnail:h,setAnnotableName:k}});
/*! AdIm - v0.0.0 - 2015-04-16 - Copyright (C) 2015 Université de Lausanne - see licence */
/*! AdIm - v0.0.0 - 2015-04-17 - Copyright (C) 2015 Université de Lausanne - see licence */