diff --git a/inc/catc.php b/inc/catc.php index d07597e..a5c5fa8 100644 --- a/inc/catc.php +++ b/inc/catc.php @@ -133,6 +133,7 @@ class CATC extends Main } public function setNote($iCourseId, $asOps) { + if(is_string($asOps)) $asOps = json_decode($asOps, true); $oNote = new Note($this->oDb, $this->oAuth->getUserId(), $iCourseId); $sError = $oNote->setNote($asOps); $bSuccess = ($sError==''); diff --git a/masks/course.html b/masks/course.html index ef45d41..ddc5120 100644 --- a/masks/course.html +++ b/masks/course.html @@ -84,8 +84,8 @@ oCATC.pageInit = function(asHash, bFirstPage) { loadDocs(); }; -oCATC.onQuitPage = function() { - return save(true); +oCATC.onQuitPage = function(sExitMode) { + return save(true, (sExitMode==self.consts.exitmodes.closing)); }; function loadDocs() { @@ -133,12 +133,16 @@ function noteFeedback(sType, sMsg) { } }; -function save(bForce) { +function save(bForce, bOnUnload) { + bForce = bForce || false; + bOnUnload = bOnUnload || false; + if(bOnUnload) bForce = true; + if(typeof oSaveTimer != 'undefined') clearTimeout(oSaveTimer); var bSave = (oEditor.keystrokes % 20 == 0 || bForce); if(bSave) { - if(self.tmp('saving')) { + if(self.tmp('saving') && !bOnUnload) { oSaveTimer = setTimeout(function(){save(true);}, 500); } else { @@ -146,22 +150,24 @@ function save(bForce) { if(self.tmp('id_course') != 0) { self.tmp('saving', true); noteFeedback('info', 'Saving...'); - getInfo( + Tools.ajax( 'set_note', - function(sDesc, asData) { - noteFeedback('notice', 'Note saved ('+asData.led_time+')'); + function(asData) { self.tmp('saving', false); + var sMsg = 'Note saved ('+asData.led_time+')'; + noteFeedback('notice', sMsg) || oCATC.feedback('success', sMsg); }, - { - id: self.tmp('id_course'), - content: sContent - }, + {id: self.tmp('id_course'), content: sContent}, function(sError) { - noteFeedback('error', 'Not saved! An error occured: '+sError); + var sMsg = 'Not saved! An error occured: '+sError; + noteFeedback('error', sMsg) || oCATC.feedback('error', sMsg);; self.tmp('saving', false); oSaveTimer = setTimeout(save, 1000); }, - 'POST' + false, + 'POST', + 'json', + bOnUnload ); } else noteFeedback('error', 'No Course ID'); diff --git a/readme.md b/readme.md index 8c5d61a..a58c6b8 100644 --- a/readme.md +++ b/readme.md @@ -8,4 +8,4 @@ Prise de notes pour les cours du Collège des Arts Thérapeutiques Chinois * [ ] Quick view of muscles / nerves schemas * [x] Dictionary * [x] Progress bar -* [ ] Save note on F5/quit \ No newline at end of file +* [x] Save note on F5/quit \ No newline at end of file diff --git a/scripts/catc.js b/scripts/catc.js index 22afe9b..5d66246 100644 --- a/scripts/catc.js +++ b/scripts/catc.js @@ -5,6 +5,7 @@ function CATC(asGlobals) this.consts.hash_sep = '-'; this.consts.default_page = 'workshops'; this.consts.title = 'CATC'; + this.consts.exitmodes = {same_page:'SP', new_page:'NP', closing:'LE'}; this.consts.root = location.protocol+'//'+location.host+location.pathname; this.init = function() @@ -22,6 +23,9 @@ function CATC(asGlobals) //on window resize $(window).resize(self.onResize).resize(); + //on Quit / F5 + $(window).on('unload', function(){self.onQuitPage(self.consts.exitmodes.closing);}); + //Hash management self.resetTmpFunctions(); $(window) @@ -34,23 +38,6 @@ function CATC(asGlobals) $.each(asVars, function(sKey, oValue){self.vars(sKey, oValue)}); }; - /* Scrollbar */ - - /*this.scrollbar = function(sPos) - { - var $Cv = $('#main'); - if(!self.vars('mobile')) - { - if(typeof self.vars('scrollbar') === 'undefined') - { - $Cv.tinyscrollbar({axis:'y', thumbSizeMin:'20', thumbSize:'20'}); - self.vars('scrollbar', $Cv.data("plugin_tinyscrollbar")); - } - else self.vars('scrollbar').update(sPos); - } - else $Cv.unbind("tinyscrollbar"); - }*/ - /* Menu */ this.initMenu = function() @@ -181,24 +168,11 @@ function CATC(asGlobals) ); }; - this.setSideElemVisibility = function() { - if(self.elem.$Side) { - var iHeight = 0; - var iMaxHeight = self.elem.$Side.height(); - self.elem.$Side.children().each(function(){ - $(this).toggle(iMaxHeight - iHeight > 0); - iHeight += $(this).outerHeight(true); - }); - } - }; - /* Events */ this.onResize = function() { self.vars('mobile', $('body').css('min-width')=='120px'); - self.setSideElemVisibility(); - //self.scrollbar(); }; this.onHashChange = function() @@ -211,7 +185,7 @@ function CATC(asGlobals) { self.pageInit = function(asHash, bFirstPage){console.log('no init for the page: '+asHash.page)}; self.onSamePageMove = function(asHash){return false}; - self.onQuitPage = function(){return true}; + self.onQuitPage = function(sExitMode){return true}; self.onFeedback = function(sType, sMsg){Tools.feedback(sType, sMsg);}; }; @@ -251,21 +225,6 @@ function CATC(asGlobals) if(bReboot) location.reload(); }; - this.getVars = function(fOnSuccess) - { - fOnSuccess = fOnSuccess || function(){}; - getInfo - ( - 'vars', - function(asData) - { - self.updateVars(asData.vars); - fOnSuccess(); - }, - {} - ); - }; - /* Page Switching */ this.loadHome = function(asData) { @@ -316,7 +275,7 @@ function CATC(asGlobals) else { var bSamePage = (sCurrPage == sNextPage); - if(self.onQuitPage(bSamePage) && !bSamePage || self.onSamePageMove(asHash)) + if(self.onQuitPage(self.consts.exitmodes[bSamePage?'same_page':'new_page']) && !bSamePage || self.onSamePageMove(asHash)) { //Delete tmp variables self.vars('tmp', {}); @@ -340,7 +299,6 @@ function CATC(asGlobals) self.elem.$Main = self.elem.$Container.find('#main'); self.elem.$Menu = self.elem.$Container.find('#menu'); - self.elem.$Side = self.elem.$Container.find('#side'); if(self.vars.log_in) self.initMenu(); self.splash(self.elem.$Main, $Dom, asHash, bFirstPage); //first page diff --git a/scripts/common.js b/scripts/common.js index b19048a..c59ff34 100644 --- a/scripts/common.js +++ b/scripts/common.js @@ -8,10 +8,11 @@ var Tools = { return asForm; }, - ajax: function(sAction, fOnSuccess, oData, fOnFail, $Loader, sType, sDataType) + ajax: function(sAction, fOnSuccess, oData, fOnFail, $Loader, sType, sDataType, bBeacon) { sType = sType || 'GET'; sDataType = sDataType || 'json'; + bBeacon = bBeacon || false; oData = oData || {}; var asData = {}; @@ -19,6 +20,7 @@ var Tools = { asData = this.serialize(oData); } else asData = oData; + asData['a'] = sAction; if($Loader) { @@ -30,32 +32,40 @@ var Tools = { .html($('', {'class':'onlyimg', src: Config.paths.dir.theme_image+"ajax.svg"})); } - asData['a'] = sAction; - return $.ajax({ - url: oCATC.consts.context.process_page, - type: sType, - data: asData, - dataType: sDataType - }) - .done(function(oData) { - if(oData.result==oCATC.consts.error) { - if(!fOnFail) Tools.feedback('error', oData.desc); - else fOnFail(oData.desc); - } - else fOnSuccess(oData.data, oData.desc); - - if($Loader) { - $Loader - .removeClass() - .addClass($Loader.data('load_class')) - .html($Loader.data('load_html')); - } - }) - .fail(function(jqXHR, textStatus, errorThrown) { - if(!fOnFail) Tools.feedback('error', textStatus+' '+errorThrown); - else fOnFail(textStatus); - } - ); + if(bBeacon) { + var oFD = new FormData(); + $.each(asData, function(sKey, sValue){ + if($.type(sValue) != 'string') sValue = JSON.stringify(sValue); + oFD.append(sKey, sValue); + }); + return navigator.sendBeacon(oCATC.consts.context.process_page, oFD); + } + else { + return $.ajax({ + url: oCATC.consts.context.process_page, + type: sType, + data: asData, + dataType: sDataType + }) + .done(function(oData) { + if(oData.result==oCATC.consts.error) { + if(!fOnFail) Tools.feedback('error', oData.desc); + else fOnFail(oData.desc); + } + else fOnSuccess(oData.data, oData.desc); + + if($Loader) { + $Loader + .removeClass() + .addClass($Loader.data('load_class')) + .html($Loader.data('load_html')); + } + }) + .fail(function(jqXHR, textStatus, errorThrown) { + if(!fOnFail) Tools.feedback('error', textStatus+' '+errorThrown); + else fOnFail(textStatus); + }); + } }, getIcon: function(sIcon, bFull){ @@ -118,42 +128,6 @@ function addInput(form, name, type, value) document.forms[form].appendChild(registerInput); } -function getInfo(action, fOnSuccess, vars, fOnError, sType/*, bProcessIcon*/) -{ - if(!vars) vars = {}; - sType = sType || 'GET'; - //bProcessIcon = bProcessIcon || false; - //if(bProcessIcon) self.addBufferIcon(); - - vars['a'] = action; - $.ajax( - { - url: oCATC.consts.context.process_page, - type:sType, - data: vars, - dataType: 'json' - }) - .done(function(oData) - { - if(oData.result==oCATC.consts.error) - { - if(!fOnError) console.log(oData.desc); - else fOnError(oData.desc); - } - else - { - //if(bProcessIcon) self.resetIcon(); - fOnSuccess(oData.desc, oData.data); - } - }) - .fail(function(jqXHR, textStatus, errorThrown) - { - //if(bProcessIcon) self.resetIcon(); - if(!fOnError) console.log(textStatus+' '+errorThrown); - else fOnError(textStatus); - }); -} - function addPunctuation(sMsg) { var asPunctuations = ['?', '!', '.', ',', ':', ';', '-', '/'];