352 lines
8.4 KiB
JavaScript
352 lines
8.4 KiB
JavaScript
function CATC(asGlobals)
|
|
{
|
|
self = this;
|
|
this.consts = asGlobals.consts;
|
|
this.consts.hash_sep = '-';
|
|
this.consts.default_page = 'workshops';
|
|
this.consts.title = 'CATC';
|
|
this.consts.root = location.protocol+'//'+location.host+location.pathname;
|
|
|
|
this.init = function()
|
|
{
|
|
//Variables & constants from php
|
|
self.vars('tmp', 'object');
|
|
self.vars('page', 'string');
|
|
self.vars('template', 'boolean');
|
|
self.updateVars(asGlobals.vars);
|
|
|
|
//page elem
|
|
self.elem = {};
|
|
self.elem.$Container = $('#container');
|
|
|
|
//on window resize
|
|
$(window).resize(self.onResize).resize();
|
|
|
|
//Hash management
|
|
self.resetTmpFunctions();
|
|
$(window)
|
|
.bind('hashchange', self.onHashChange)
|
|
.trigger('hashchange');
|
|
};
|
|
|
|
this.updateVars = function(asVars)
|
|
{
|
|
$.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()
|
|
{
|
|
self.elem.$Menu.find('.fa-home').attr('href', '#'+self.consts.default_page);
|
|
self.elem.$Menu.show('fast');
|
|
};
|
|
|
|
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()
|
|
{
|
|
var asHash = self.getHash();
|
|
self.switchPage(asHash);
|
|
};
|
|
|
|
this.resetTmpFunctions = function()
|
|
{
|
|
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.onFeedback = function(sType, sMsg){Tools.feedback(sType, sMsg);};
|
|
};
|
|
|
|
this.feedback = function(sType, sMsg) {
|
|
self.onFeedback(sType, sMsg);
|
|
};
|
|
|
|
/* Hash Handling */
|
|
|
|
this.getHash = function()
|
|
{
|
|
var sHash = self.hash();
|
|
var asHash = sHash.split(self.consts.hash_sep);
|
|
var sPage = asHash.shift() || '';
|
|
return {hash:sHash, page:sPage, items:asHash};
|
|
};
|
|
|
|
this.setHash = function(sPage, asItems, bReboot)
|
|
{
|
|
bReboot = bReboot || false;
|
|
sPage = sPage || '';
|
|
asItems = asItems || [];
|
|
if(typeof asItems == 'string') asItems = [asItems];
|
|
if(sPage != '')
|
|
{
|
|
var sItems = (asItems.length > 0)?self.consts.hash_sep+asItems.join(self.consts.hash_sep):'';
|
|
self.hash(sPage+sItems, bReboot);
|
|
}
|
|
};
|
|
|
|
this.hash = function(hash, bReboot)
|
|
{
|
|
bReboot = bReboot || false;
|
|
if(!hash) return window.location.hash.slice(1);
|
|
else window.location.hash = '#'+hash;
|
|
|
|
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) {
|
|
self.vars('log_in', asData.log_in);
|
|
self.vars('id', asData.id);
|
|
|
|
$(window).off('keyup');
|
|
|
|
if(self.vars('log_in')) {
|
|
//Setup menu
|
|
self.initMenu();
|
|
|
|
//Swap to default page
|
|
self.setHash(self.consts.default_page);
|
|
}
|
|
};
|
|
|
|
this.setPageTitle = function(sTitle) {
|
|
$('#main_title').find('h1').text(sTitle);
|
|
};
|
|
|
|
this.getActionLink = function(sAction, oVars)
|
|
{
|
|
if(!oVars) oVars = {};
|
|
sVars = '';
|
|
for(i in oVars)
|
|
{
|
|
sVars += '&'+i+'='+oVars[i];
|
|
}
|
|
return self.consts.process_page+'?a='+sAction+sVars;
|
|
};
|
|
|
|
this.switchPage = function(asHash)
|
|
{
|
|
var sCurrPage = self.vars('page');
|
|
var sNextPage = asHash.page;
|
|
var bLoggedIn = self.vars('log_in');
|
|
var sDefaultPage = bLoggedIn?self.consts.default_page:'logon';
|
|
|
|
if( asHash.hash == '' ||
|
|
sNextPage == '' ||
|
|
!bLoggedIn && sNextPage != sDefaultPage ||
|
|
bLoggedIn && sNextPage == 'logon'
|
|
)
|
|
{
|
|
self.setHash(sDefaultPage); //force first page
|
|
}
|
|
else
|
|
{
|
|
var bSamePage = (sCurrPage == sNextPage);
|
|
if(self.onQuitPage(bSamePage) && !bSamePage || self.onSamePageMove(asHash))
|
|
{
|
|
//Delete tmp variables
|
|
self.vars('tmp', {});
|
|
|
|
//disable tmp functions
|
|
self.resetTmpFunctions();
|
|
|
|
//Officially a new page
|
|
var bFirstPage = (sCurrPage == '');
|
|
self.vars('page', sNextPage);
|
|
|
|
//Update Page Title
|
|
var sDetail = asHash.items[0] || '';
|
|
document.title = self.consts.title+' - '+sNextPage+' '+sDetail;
|
|
|
|
//Replacing DOM
|
|
var $Dom = $(self.consts.pages[sNextPage]);
|
|
if(bFirstPage)
|
|
{
|
|
self.elem.$Container.html($(self.consts.pages['template']));
|
|
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
|
|
}
|
|
else
|
|
{
|
|
self.elem.$Main.stop().fadeTo('fast', 0, function(){self.splash(self.elem.$Main, $Dom, asHash, bFirstPage);}); //Switching page
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
this.splash = function($FadeInElem, $Dom, asHash, bFirstPage)
|
|
{
|
|
//Switch main content
|
|
$FadeInElem.empty();
|
|
$FadeInElem.html($Dom);
|
|
|
|
//Show main
|
|
$FadeInElem.fadeTo('fast', 1, function(){self.pageInit(asHash, bFirstPage);});
|
|
};
|
|
|
|
/* Variables Handling */
|
|
|
|
this.vars = function(sVarName, oValue)
|
|
{
|
|
var asTypes = {boolean:false, string:'', integer:0, array:[], object:{}};
|
|
var asVarName = (typeof sVarName == 'object')?sVarName:[sVarName];
|
|
var sFirstVarName = asVarName[0];
|
|
|
|
//Get, only name parameter
|
|
if(typeof oValue == 'undefined')
|
|
{
|
|
return getElem(self.vars, asVarName);
|
|
}
|
|
//Init, name & type / default value
|
|
else if(typeof oValue !== 'undefined' && typeof self.vars[sFirstVarName] === 'undefined')
|
|
{
|
|
self.vars[sFirstVarName] = (typeof asTypes[oValue] !== 'undefined')?asTypes[oValue]:oValue;
|
|
return self.vars[sFirstVarName];
|
|
}
|
|
//Set, name & value
|
|
else if(typeof oValue !== 'undefined' && typeof self.vars[sFirstVarName] !== 'undefined')
|
|
{
|
|
setElem(self.vars, asVarName, oValue);
|
|
return getElem(self.vars, asVarName);
|
|
}
|
|
return null;
|
|
};
|
|
|
|
this.tmp = function(sVarName, oValue)
|
|
{
|
|
var asVarName = (typeof sVarName == 'object')?sVarName:[sVarName];
|
|
asVarName.unshift('tmp');
|
|
return self.vars(asVarName, oValue);
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
class Editor {
|
|
constructor(sEditorId, bReadOnly) {
|
|
this.id = 0;
|
|
this.keystrokes = 0;
|
|
this.readOnly = bReadOnly || false;
|
|
this.sCursorPos = '';
|
|
|
|
//DOM Elements
|
|
this.$Editor = $(sEditorId);
|
|
|
|
this.onKeyStroke = function(e){};
|
|
|
|
this.oQuill = new Quill(sEditorId, {
|
|
theme: 'snow',
|
|
placeholder: 'Notes',
|
|
readOnly: bReadOnly/*,
|
|
modules: {
|
|
toolbar: [['bold', 'italic', 'underline'], [{ 'list': 'ordered'}, { 'list': 'bullet' }], ['clean']]
|
|
}*/
|
|
});
|
|
|
|
this._initEvents();
|
|
this._postInit();
|
|
}
|
|
|
|
_initEvents() {
|
|
//Key strokes
|
|
this.$Editor.keydown((e) => {
|
|
if($.inArray(e.which, [13, 37, 38, 39, 40]) == -1) this.onKeyStroke(e);
|
|
});
|
|
|
|
//On text modification
|
|
this.oQuill.on('text-change', (delta, oldDelta, sSource) => {this._incKeyStrokes();});
|
|
}
|
|
|
|
_postInit(iPage) {
|
|
if(!this.readOnly) this.oQuill.focus();
|
|
}
|
|
|
|
open(iCourseId) {
|
|
Tools.ajax(
|
|
'get_note',
|
|
(asData) => {
|
|
this.id = asData.id;
|
|
this.oQuill.setContents(asData.ops);
|
|
this._postInit();
|
|
},
|
|
{id: iCourseId}
|
|
);
|
|
}
|
|
|
|
_incKeyStrokes() {
|
|
this.keystrokes += 1;
|
|
}
|
|
|
|
getContent() {
|
|
return this.oQuill.getContents().ops;
|
|
}
|
|
|
|
isEmpty() {
|
|
const rEmpty = /^(<p>(<br>|<br\/>|<br\s\/>|\s+|)<\/p>|)$/gm;
|
|
return rEmpty.test(this.oQuill.getText().trim());
|
|
}
|
|
|
|
quill2Html(asDelta) {
|
|
var tempCont = document.createElement('div');
|
|
(new Quill(tempCont)).setContents(asDelta);
|
|
return tempCont.getElementsByClassName('ql-editor')[0].innerHTML;
|
|
}
|
|
} |