From 28e6e167ee24e23af9625ebc2b3e3ad579257630 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Sat, 21 Sep 2024 17:23:21 +0200 Subject: [PATCH] Introduce some pre-commit hooks --- .phplint.yml | 7 + .pre-commit-config.yaml | 26 + .prettierignore | 2 + .yamllint.yaml | 9 + composer.json | 52 +- config.yml | 4 +- setup.cfg | 4 + static/main.css | 17 +- static/main.js | 1534 ++++++++++++++++++++++----------------- static/mysc_objects.js | 663 ++++++++--------- 10 files changed, 1275 insertions(+), 1043 deletions(-) create mode 100644 .phplint.yml create mode 100644 .pre-commit-config.yaml create mode 100644 .prettierignore create mode 100644 .yamllint.yaml create mode 100644 setup.cfg diff --git a/.phplint.yml b/.phplint.yml new file mode 100644 index 0000000..924ccad --- /dev/null +++ b/.phplint.yml @@ -0,0 +1,7 @@ +path: ./ +jobs: 10 +extensions: + - php +exclude: + - vendor +warning: true diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..e3728d2 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,26 @@ +# Pre-commit hooks to run tests and ensure code is cleaned. +# See https://pre-commit.com for more information +--- +repos: + - repo: https://github.com/adrienverge/yamllint + rev: v1.32.0 + hooks: + - id: yamllint + ignore: .github/ + - repo: https://github.com/pre-commit/mirrors-prettier + rev: v2.7.1 + hooks: + - id: prettier + - repo: https://github.com/digitalpulp/pre-commit-php.git + rev: 1.4.0 + hooks: + - id: php-stan + files: ^(?!example/).*\.(php)$ + args: ["--configuration=phpstan.neon"] + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: check-executables-have-shebangs + stages: [manual] + - id: check-json + exclude: (.vscode|.devcontainer) diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..8ae21dc --- /dev/null +++ b/.prettierignore @@ -0,0 +1,2 @@ +/static/lib/* +/locales/* diff --git a/.yamllint.yaml b/.yamllint.yaml new file mode 100644 index 0000000..1288913 --- /dev/null +++ b/.yamllint.yaml @@ -0,0 +1,9 @@ +extends: default + +ignore: | + static/lib/* + +rules: + line-length: + max: 100 + level: warning diff --git a/composer.json b/composer.json index d1e3e0a..6f5a9d0 100644 --- a/composer.json +++ b/composer.json @@ -1,29 +1,29 @@ { - "name": "brenard/mysc", - "description": "My Sweetcase", - "type": "project", - "require": { - "brenard/eesyphp": "dev-master" - }, - "license": "GPL3", - "autoload": { - "psr-4": { - "MySC\\": "src/" - } - }, - "authors": [ - { - "name": "Benjamin Renard", - "email": "brenard@zionetrix.net" - } - ], - "minimum-stability": "dev", - "config": { - "allow-plugins": { - "php-http/discovery": true - } - }, - "require-dev": { - "phpstan/phpstan": "2.0.x-dev" + "name": "brenard/mysc", + "description": "My Sweetcase", + "type": "project", + "require": { + "brenard/eesyphp": "dev-master" + }, + "license": "GPL3", + "autoload": { + "psr-4": { + "MySC\\": "src/" } + }, + "authors": [ + { + "name": "Benjamin Renard", + "email": "brenard@zionetrix.net" + } + ], + "minimum-stability": "dev", + "config": { + "allow-plugins": { + "php-http/discovery": true + } + }, + "require-dev": { + "phpstan/phpstan": "2.0.x-dev" + } } diff --git a/config.yml b/config.yml index e87a2b8..3a01643 100644 --- a/config.yml +++ b/config.yml @@ -116,8 +116,8 @@ db: options: null # Date/Datetime format in database (strptime format) - date_format: '%s' - datetime_format: '%s' + date_format: "%s" + datetime_format: "%s" # Postgresql #dsn: "pgsql:host=localhost;port=5432;dbname=items" diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..52a5bf3 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,4 @@ +[codespell] +ignore-words-list=fro,hass +exclude-file=.codespell-exclusions +quiet-level=2 diff --git a/static/main.css b/static/main.css index a8fd8f2..8ec3fe1 100644 --- a/static/main.css +++ b/static/main.css @@ -1,8 +1,10 @@ -body{ +body { margin-top: 4em; } -div.panel-heading, li.list-group-item, a { +div.panel-heading, +li.list-group-item, +a { cursor: pointer; } @@ -19,7 +21,7 @@ div.panel-heading, li.list-group-item, a { } .checkable:before { - content: '\2713'; + content: "\2713"; margin-right: 0.2em; font-style: italic; color: #999; @@ -43,12 +45,14 @@ div.panel-heading, li.list-group-item, a { font-size: 1.5em; } -.add_thing_label, #edit_thing_label { +.add_thing_label, +#edit_thing_label { width: 80%; display: inline-block; } -.add_thing_nb, #edit_thing_nb { +.add_thing_nb, +#edit_thing_nb { width: 18%; display: inline-block; } @@ -58,7 +62,8 @@ div.panel-heading, li.list-group-item, a { background: rgba(66, 215, 252, 0.95); } -.alertify .ajs-header, .alertify .ajs-footer { +.alertify .ajs-header, +.alertify .ajs-footer { background-color: #4e5d6c; } diff --git a/static/main.js b/static/main.js index e7c3adb..a0839db 100644 --- a/static/main.js +++ b/static/main.js @@ -39,185 +39,207 @@ alertify.defaults = { cancel: "ajs-cancel btn btn-default", }, // global hooks - hooks:{ + hooks: { // invoked before initializing any dialog - preinit:function(instance){}, + preinit: function (instance) {}, // invoked after initializing any dialog - postinit:function(instance){}, -}, + postinit: function (instance) {}, + }, }; -$('#cats').collapse({ - toggle: false +$("#cats").collapse({ + toggle: false, }); -on_title_click=function(event) { - if (event.target.tagName=='BUTTON') { +on_title_click = function (event) { + if (event.target.tagName == "BUTTON") { return; } - var title=$(this); - var panel=title.parent().parent(); - var panel_collapse=panel.find('.panel-collapse'); - var show=!panel_collapse.hasClass('in'); - $('.panel-collapse').each(function(idx,div) { - $(div).collapse('hide'); + var title = $(this); + var panel = title.parent().parent(); + var panel_collapse = panel.find(".panel-collapse"); + var show = !panel_collapse.hasClass("in"); + $(".panel-collapse").each(function (idx, div) { + $(div).collapse("hide"); }); - if (show) - panel_collapse.collapse('show'); + if (show) panel_collapse.collapse("show"); set_location( - title.data('scase').name, - show ? title.data('cat').name : null, - title.data('trash') ? 'trash' : null + title.data("scase").name, + show ? title.data("cat").name : null, + title.data("trash") ? "trash" : null ); -} +}; /*********************** * Add scase **********************/ -on_add_scase_btn_click=function(event) { +on_add_scase_btn_click = function (event) { navbar_collapse_hide(); - $('#add_scase_modal').modal('show'); -} + $("#add_scase_modal").modal("show"); +}; -on_valid_add_scase_modal=function (e) { +on_valid_add_scase_modal = function (e) { e.preventDefault(); - var name=$('#add_scase_name')[0].value; - if (name=='') { + var name = $("#add_scase_name")[0].value; + if (name == "") { alertify.notify("Vous devez saisir le nom de la valise !", "error", 5); return; } - var nameshake=scases.byName(name); + var nameshake = scases.byName(name); if (nameshake) { if (nameshake.removed) { - alertify.notify("Une valise de ce nom existe dans la corbeille !", "error", 5); - } - else { + alertify.notify( + "Une valise de ce nom existe dans la corbeille !", + "error", + 5 + ); + } else { alertify.notify("Cette valise existe déjà !", "error", 5); } return; } - var scase=scases.newSCase(name); + var scase = scases.newSCase(name); if (scase) { scases.save(); show_scase(scase); auto_sync_local_data(); } - $('#add_scase_modal').modal('hide'); -} + $("#add_scase_modal").modal("hide"); +}; -on_show_add_scase_modal=function () { - $('#add_scase_name').focus(); -} +on_show_add_scase_modal = function () { + $("#add_scase_name").focus(); +}; -on_close_add_scase_modal=function () { - $('#add_scase_modal form')[0].reset(); -} +on_close_add_scase_modal = function () { + $("#add_scase_modal form")[0].reset(); +}; /*********************** * Rename scase **********************/ -on_rename_scase_btn_click=function(event) { +on_rename_scase_btn_click = function (event) { navbar_collapse_hide(); - $('#rename_scase_name')[0].value=$('#cats').data('scase'); - $('#rename_scase_modal').modal('show'); -} + $("#rename_scase_name")[0].value = $("#cats").data("scase"); + $("#rename_scase_modal").modal("show"); +}; -on_valid_rename_scase_modal=function (e) { +on_valid_rename_scase_modal = function (e) { e.preventDefault(); - var name=$('#rename_scase_name')[0].value; - if (name=='') { - alertify.notify("Vous devez saisir le nouveau nom de la valise !", "error", 5); + var name = $("#rename_scase_name")[0].value; + if (name == "") { + alertify.notify( + "Vous devez saisir le nouveau nom de la valise !", + "error", + 5 + ); return; } - if ($('#cats').data('scase')!=name) { - var nameshake=scases.byName(name); + if ($("#cats").data("scase") != name) { + var nameshake = scases.byName(name); if (nameshake) { if (nameshake.removed) { - alertify.notify("Une valise portant ce nom existe dans la corbeille !", "error", 5); - } - else { + alertify.notify( + "Une valise portant ce nom existe dans la corbeille !", + "error", + 5 + ); + } else { alertify.notify("Une valise portant ce nom existe déjà !", "error", 5); } return; } - var scase=scases.renameSCase($('#cats').data('scase'),name); + var scase = scases.renameSCase($("#cats").data("scase"), name); if (scase) { scases.save(); show_scase(scase); auto_sync_local_data(); - } - else { - alertify.notify('Une erreur est survenue en renomant la valise...', "error", 5); + } else { + alertify.notify( + "Une erreur est survenue en renomant la valise...", + "error", + 5 + ); } } - $('#rename_scase_modal').modal('hide'); -} + $("#rename_scase_modal").modal("hide"); +}; -on_show_rename_scase_modal=function () { - $('#rename_scase_name').focus(); -} +on_show_rename_scase_modal = function () { + $("#rename_scase_name").focus(); +}; -on_close_rename_scase_modal=function () { - $('#rename_scase_modal form')[0].reset(); -} +on_close_rename_scase_modal = function () { + $("#rename_scase_modal form")[0].reset(); +}; /*********************** * Copy scase **********************/ -on_copy_scase_btn_click=function(event) { +on_copy_scase_btn_click = function (event) { navbar_collapse_hide(); - $('#copy_scase_modal').modal('show'); -} + $("#copy_scase_modal").modal("show"); +}; -on_valid_copy_scase_modal=function (e) { +on_valid_copy_scase_modal = function (e) { e.preventDefault(); - var name=$('#copy_scase_name')[0].value; - if (name=='') { - alertify.notify("Vous devez saisir le nom de la nouvelle valise !", "error", 5); + var name = $("#copy_scase_name")[0].value; + if (name == "") { + alertify.notify( + "Vous devez saisir le nom de la nouvelle valise !", + "error", + 5 + ); return; } - var nameshake=scases.byName(name); + var nameshake = scases.byName(name); if (nameshake) { if (nameshake.removed) { - alertify.notify("Une valise portant ce nom existe dans la corbeille !", "error", 5); - } - else { + alertify.notify( + "Une valise portant ce nom existe dans la corbeille !", + "error", + 5 + ); + } else { alertify.notify("Une valise portant ce nom existe déjà !", "error", 5); } return; } - var scase=scases.copySCase($('#cats').data('scase'),name); + var scase = scases.copySCase($("#cats").data("scase"), name); if (scase) { scases.save(); show_scase(scase); auto_sync_local_data(); + } else { + alertify.notify( + "Une erreur est survenue en copiant la valise...", + "error", + 5 + ); } - else { - alertify.notify('Une erreur est survenue en copiant la valise...', "error", 5); - } - $('#copy_scase_modal').modal('hide'); -} + $("#copy_scase_modal").modal("hide"); +}; -on_show_copy_scase_modal=function () { - $('#copy_scase_name').focus(); -} +on_show_copy_scase_modal = function () { + $("#copy_scase_name").focus(); +}; -on_close_copy_scase_modal=function () { - $('#copy_scase_modal form')[0].reset(); -} +on_close_copy_scase_modal = function () { + $("#copy_scase_modal form")[0].reset(); +}; /*********************** * Reset scase **********************/ -on_reset_scase_btn_click=function(event) { +on_reset_scase_btn_click = function (event) { navbar_collapse_hide(); - var scase=scases.byName($('#cats').data('scase')); + var scase = scases.byName($("#cats").data("scase")); if (scase) { alertify.confirm( `Réinitialisation de la valise ${scase.name}`, `Voulez-vous vraiment réinitialiser la valise ${scase.name} ?`, - function(data) { + function (data) { scases.resetSCase(scase.name); scases.save(); show_scase(scase); @@ -226,18 +248,18 @@ on_reset_scase_btn_click=function(event) { null ); } -} +}; /*********************** * Delete scase **********************/ -on_delete_scase_btn_click=function(event) { +on_delete_scase_btn_click = function (event) { navbar_collapse_hide(); - var scase=scases.byName($('#cats').data('scase')); + var scase = scases.byName($("#cats").data("scase")); if (scase) { alertify.confirm( `Suppression de la valise ${scase.name}`, `Voulez-vous vraiment supprimer la valise ${scase.name} ?`, - function(data) { + function (data) { scases.removeSCase(scase.name); scases.save(); show_scases(); @@ -246,16 +268,16 @@ on_delete_scase_btn_click=function(event) { null ); } -} +}; -on_restore_scase_btn_click=function(event) { +on_restore_scase_btn_click = function (event) { navbar_collapse_hide(); - var scase=event.data.scase; + var scase = event.data.scase; if (scase) { alertify.confirm( `Restauration de la valise ${scase.name}`, `Voulez-vous vraiment restaurer la valise ${scase.name} ?`, - function(data) { + function (data) { scase.restore(); scases.save(); show_scases(); @@ -263,121 +285,135 @@ on_restore_scase_btn_click=function(event) { null ); } -} +}; -on_scase_trash_btn_click=function(event) { +on_scase_trash_btn_click = function (event) { event.preventDefault(); navbar_collapse_hide(); - var scase=scases.byName($('#cats').data('scase')); + var scase = scases.byName($("#cats").data("scase")); if (scase) { show_scase_trash(scase); } -} +}; /*********************** * Add cat **********************/ -on_add_cat_btn_click=function(event) { +on_add_cat_btn_click = function (event) { navbar_collapse_hide(); - $('#add_cat_modal').modal('show'); -} + $("#add_cat_modal").modal("show"); +}; -on_valid_add_cat_modal=function (e) { +on_valid_add_cat_modal = function (e) { e.preventDefault(); - var name=$('#add_cat_name')[0].value; - if (name=='') { + var name = $("#add_cat_name")[0].value; + if (name == "") { alertify.notify("Vous devez saisir le nom de la catégorie !", "error", 5); return; } - var scase=scases.byName($('#cats').data('scase')); + var scase = scases.byName($("#cats").data("scase")); if (scase) { - var nameshake=scase.cats.byName(name); + var nameshake = scase.cats.byName(name); if (nameshake) { if (nameshake.removed) { - alertify.notify("Une catégorie portant ce nom existe dans la corbeille !", "error", 5); - } - else { - alertify.notify("Une catégorie portant ce nom existe déjà !", "error", 5); + alertify.notify( + "Une catégorie portant ce nom existe dans la corbeille !", + "error", + 5 + ); + } else { + alertify.notify( + "Une catégorie portant ce nom existe déjà !", + "error", + 5 + ); } return; } - var cat=scase.cats.newCat(name); + var cat = scase.cats.newCat(name); if (cat) { scases.save(); - show_scase(scase,cat.name); + show_scase(scase, cat.name); auto_sync_local_data(); } } - $('#add_cat_modal').modal('hide'); -} + $("#add_cat_modal").modal("hide"); +}; -on_show_add_cat_modal=function () { - $('#add_cat_name').focus(); -} +on_show_add_cat_modal = function () { + $("#add_cat_name").focus(); +}; -on_close_add_cat_modal=function () { - $('#add_cat_modal form')[0].reset(); -} +on_close_add_cat_modal = function () { + $("#add_cat_modal form")[0].reset(); +}; /*********************** * Rename cat **********************/ -on_rename_cat_btn_click=function(event) { +on_rename_cat_btn_click = function (event) { navbar_collapse_hide(); - $('#rename_cat_modal').data('cat',event.data.cat.name); - $('#rename_cat_name')[0].value=event.data.cat.name; - $('#rename_cat_modal').modal('show'); -} + $("#rename_cat_modal").data("cat", event.data.cat.name); + $("#rename_cat_name")[0].value = event.data.cat.name; + $("#rename_cat_modal").modal("show"); +}; -on_valid_rename_cat_modal=function (e) { +on_valid_rename_cat_modal = function (e) { e.preventDefault(); - var name=$('#rename_cat_name')[0].value; - if (name=='') { - alertify.notify("Vous devez saisir le nouveau nom de la catégorie !", "error", 5); + var name = $("#rename_cat_name")[0].value; + if (name == "") { + alertify.notify( + "Vous devez saisir le nouveau nom de la catégorie !", + "error", + 5 + ); return; } - var scase=scases.byName($('#cats').data('scase')); + var scase = scases.byName($("#cats").data("scase")); if (scase) { if (scase.cats.byName(name)) { - var namesake=scase.cats.byName(name); + var namesake = scase.cats.byName(name); if (namesake.removed) { - alertify.notify("Une catégorie de se nom existe dans la corbeille !", "error", 5); - } - else { + alertify.notify( + "Une catégorie de se nom existe dans la corbeille !", + "error", + 5 + ); + } else { alertify.notify("Cette catégorie existe déjà !", "error", 5); } return; } - var cat=scase.cats.renameCat($('#rename_cat_modal').data('cat'),name); + var cat = scase.cats.renameCat($("#rename_cat_modal").data("cat"), name); if (cat) { scases.save(); - show_scase(scase,cat.name); + show_scase(scase, cat.name); auto_sync_local_data(); } } - $('#rename_cat_modal').modal('hide'); -} + $("#rename_cat_modal").modal("hide"); +}; -on_show_rename_cat_modal=function () { - $('#rename_cat_name').focus(); -} +on_show_rename_cat_modal = function () { + $("#rename_cat_name").focus(); +}; -on_close_rename_cat_modal=function () { - $('#rename_cat_modal form')[0].reset(); -} +on_close_rename_cat_modal = function () { + $("#rename_cat_modal form")[0].reset(); +}; /*********************** * Delete cat **********************/ -on_delete_cat_btn_click=function(event) { +on_delete_cat_btn_click = function (event) { navbar_collapse_hide(); - var scase=scases.byName($('#cats').data('scase')); + var scase = scases.byName($("#cats").data("scase")); if (scase) { - var cat=event.data.cat.name; + var cat = event.data.cat.name; alertify.confirm( `Suppression de la catégorie ${cat}`, `Voulez-vous vraiment supprimer la catégorie ${cat} ?`, - function(data) { + function (data) { scase.cats.removeCat(cat); scases.save(); show_scase(scase); @@ -386,17 +422,17 @@ on_delete_cat_btn_click=function(event) { null ); } -} +}; -on_restore_cat_btn_click=function(event) { +on_restore_cat_btn_click = function (event) { navbar_collapse_hide(); - var scase=scases.byName($('#cats').data('scase')); + var scase = scases.byName($("#cats").data("scase")); if (scase) { - var cat=event.data.cat.name; + var cat = event.data.cat.name; alertify.confirm( `Restauration de la catégorie ${cat}`, `Voulez-vous vraiment restaurer la catégorie ${cat} ?`, - function(data) { + function (data) { scase.cats.restoreCat(cat); scases.save(); show_scase(scase); @@ -405,77 +441,85 @@ on_restore_cat_btn_click=function(event) { null ); } -} +}; /************************ * Check/Uncheck thing ***********************/ -on_li_click=function(event) { - if (event.target.tagName!='LI') return; - var li=$(this); - var ul=li.parent(); - var scase=scases.byName($('#cats').data('scase')); +on_li_click = function (event) { + if (event.target.tagName != "LI") return; + var li = $(this); + var ul = li.parent(); + var scase = scases.byName($("#cats").data("scase")); if (scase) { - var cat=scase.cats.byName(ul.data('cat')); + var cat = scase.cats.byName(ul.data("cat")); if (cat) { - var thing=cat.byLabel(li.data('label')); + var thing = cat.byLabel(li.data("label")); if (thing) { - li.toggleClass('checked'); - thing.setChecked(li.hasClass('checked')); + li.toggleClass("checked"); + thing.setChecked(li.hasClass("checked")); scases.save(); auto_sync_local_data(); } - show_scase(scase,cat.name); + show_scase(scase, cat.name); } } -} +}; /*********************** * Add thing **********************/ -on_li_add_click=function(event) { - var li=$(this); - var cat=li.parent().data('cat'); - var modal=$('#add_thing_modal'); - modal.data('cat',cat); - modal.modal('show'); -} +on_li_add_click = function (event) { + var li = $(this); + var cat = li.parent().data("cat"); + var modal = $("#add_thing_modal"); + modal.data("cat", cat); + modal.modal("show"); +}; -on_valid_add_thing_modal=function (e) { +on_valid_add_thing_modal = function (e) { e.preventDefault(); - var modal=$('#add_thing_modal'); - var scase=scases.byName($('#cats').data('scase')); + var modal = $("#add_thing_modal"); + var scase = scases.byName($("#cats").data("scase")); if (scase) { - var cat=scase.cats.byName(modal.data('cat')); + var cat = scase.cats.byName(modal.data("cat")); if (cat) { - var things=[]; - var labels=[]; - var error=false; - var add_thing_nbs=$('input.add_thing_nb'); - $('input.add_thing_label').each(function(idx,input) { - var label=$(input).val(); - if (label && label!='') { - if (labels.indexOf(label)>-1) { - alertify.notify("Deux élements ne peuvent porter le même nom !", "error", 5); - error=true; + var things = []; + var labels = []; + var error = false; + var add_thing_nbs = $("input.add_thing_nb"); + $("input.add_thing_label").each(function (idx, input) { + var label = $(input).val(); + if (label && label != "") { + if (labels.indexOf(label) > -1) { + alertify.notify( + "Deux élements ne peuvent porter le même nom !", + "error", + 5 + ); + error = true; return; } if (cat.byLabel(label)) { - alertify.notify("L'élément '"+label+"' existe déjà !", "error", 5); - error=true; + alertify.notify( + "L'élément '" + label + "' existe déjà !", + "error", + 5 + ); + error = true; return; } - var nb=1; + var nb = 1; if (add_thing_nbs[idx]) { - nb=parseInt($(add_thing_nbs[idx]).val()); - if (!nb || nb==0) { - nb=1; + nb = parseInt($(add_thing_nbs[idx]).val()); + if (!nb || nb == 0) { + nb = 1; } } things.push({ - 'label': label, - 'nb': nb + label: label, + nb: nb, }); labels.push(label); } @@ -483,315 +527,386 @@ on_valid_add_thing_modal=function (e) { if (error) { return; } - if (things.length==0) { - alertify.notify("Vous devez saisir au moins un nom d'élément !", "error", 5); + if (things.length == 0) { + alertify.notify( + "Vous devez saisir au moins un nom d'élément !", + "error", + 5 + ); return; } for (idx in things) { - cat.newThing(things[idx]['label'], things[idx]['nb']); + cat.newThing(things[idx]["label"], things[idx]["nb"]); } scases.save(); - show_scase(scase,cat.name); + show_scase(scase, cat.name); auto_sync_local_data(); } } - modal.modal('hide'); -} + modal.modal("hide"); +}; -on_show_add_thing_modal=function () { - $('.add_thing_other').remove(); - $('input.add_thing_label').val(''); - $('input.add_thing_nb').val(''); - $('input.add_thing_label').first().focus(); -} +on_show_add_thing_modal = function () { + $(".add_thing_other").remove(); + $("input.add_thing_label").val(""); + $("input.add_thing_nb").val(""); + $("input.add_thing_label").first().focus(); +}; -on_close_add_thing_modal=function () { - $('#add_thing_modal form')[0].reset(); -} +on_close_add_thing_modal = function () { + $("#add_thing_modal form")[0].reset(); +}; -on_add_thing_label_focus=function(event) { - if ($('input.add_thing_label').last()[0] == event.target) { - var new_input_group=$('
'); - var new_input_label=$(''); - var new_input_nb=$(''); +on_add_thing_label_focus = function (event) { + if ($("input.add_thing_label").last()[0] == event.target) { + var new_input_group = $('
'); + var new_input_label = $( + '' + ); + var new_input_nb = $( + '' + ); new_input_group.append(new_input_label); - new_input_group.append(' '); + new_input_group.append(" "); new_input_group.append(new_input_nb); - new_input_label.bind('focus', on_add_thing_label_focus); + new_input_label.bind("focus", on_add_thing_label_focus); $(event.target).parent().after(new_input_group); } -} - +}; /*********************** * Edit thing **********************/ -on_edit_thing_btn_click=function(event) { +on_edit_thing_btn_click = function (event) { navbar_collapse_hide(); - $('#edit_thing_modal').data('cat',event.data.cat.name); - $('#edit_thing_modal').data('thing',event.data.thing.label); - $('#edit_thing_label').val(event.data.thing.label); - $('#edit_thing_nb').val(event.data.thing.nb); - $('#edit_thing_modal').modal('show'); -} + $("#edit_thing_modal").data("cat", event.data.cat.name); + $("#edit_thing_modal").data("thing", event.data.thing.label); + $("#edit_thing_label").val(event.data.thing.label); + $("#edit_thing_nb").val(event.data.thing.nb); + $("#edit_thing_modal").modal("show"); +}; -on_valid_edit_thing_modal=function (e) { +on_valid_edit_thing_modal = function (e) { e.preventDefault(); - var label=$('#edit_thing_label').val(); - if (label=='') { - alertify.notify("Vous devez saisir le nouveau nom de l'élément !", "error", 5); + var label = $("#edit_thing_label").val(); + if (label == "") { + alertify.notify( + "Vous devez saisir le nouveau nom de l'élément !", + "error", + 5 + ); return; } - var nb=parseInt($('#edit_thing_nb').val()); - if (!nb || nb==0) { - nb=1; + var nb = parseInt($("#edit_thing_nb").val()); + if (!nb || nb == 0) { + nb = 1; } - var scase=scases.byName($('#cats').data('scase')); + var scase = scases.byName($("#cats").data("scase")); if (scase) { - var cat=scase.cats.byName($('#edit_thing_modal').data('cat')); + var cat = scase.cats.byName($("#edit_thing_modal").data("cat")); if (cat) { - if (label!=$('#edit_thing_modal').data('thing')) { - var namesake=cat.byLabel(label); + if (label != $("#edit_thing_modal").data("thing")) { + var namesake = cat.byLabel(label); if (namesake) { if (namesake.removed) { - alertify.notify("Un élément de ce nom existe dans la corbeille !", "error", 5); - } - else { + alertify.notify( + "Un élément de ce nom existe dans la corbeille !", + "error", + 5 + ); + } else { alertify.notify("Un élément de ce nom existe déjà !", "error", 5); } return; } - var thing=cat.renameThing($('#edit_thing_modal').data('thing'),label); - } - else { - var thing=cat.byLabel(label); + var thing = cat.renameThing( + $("#edit_thing_modal").data("thing"), + label + ); + } else { + var thing = cat.byLabel(label); } if (thing) { thing.setNb(nb); scases.save(); - show_scase(scase,cat.name); + show_scase(scase, cat.name); auto_sync_local_data(); } } } - $('#edit_thing_modal').modal('hide'); -} + $("#edit_thing_modal").modal("hide"); +}; -on_show_edit_thing_modal=function () { - $('#edit_thing_label').focus(); -} +on_show_edit_thing_modal = function () { + $("#edit_thing_label").focus(); +}; -on_close_edit_thing_modal=function () { - $('#edit_thing_modal form')[0].reset(); -} +on_close_edit_thing_modal = function () { + $("#edit_thing_modal form")[0].reset(); +}; /*********************** * Delete thing **********************/ -on_delete_thing_btn_click=function(event) { +on_delete_thing_btn_click = function (event) { navbar_collapse_hide(); - var scase=scases.byName($('#cats').data('scase')); + var scase = scases.byName($("#cats").data("scase")); if (scase) { - var cat=scase.cats.byName(event.data.cat.name); + var cat = scase.cats.byName(event.data.cat.name); if (cat) { - var thing=event.data.thing.label; + var thing = event.data.thing.label; alertify.confirm( `Suppression de l'élément ${thing}`, `Voulez-vous vraiment supprimer l'élément ${thing} ?`, - function(data) { + function (data) { cat.removeThing(thing); scases.save(); - show_scase(scase,cat.name); + show_scase(scase, cat.name); auto_sync_local_data(); }, null ); } } -} +}; -on_restore_thing_btn_click=function(event) { +on_restore_thing_btn_click = function (event) { navbar_collapse_hide(); - var scase=scases.byName($('#cats').data('scase')); + var scase = scases.byName($("#cats").data("scase")); if (scase) { - var cat=scase.cats.byName(event.data.cat.name); + var cat = scase.cats.byName(event.data.cat.name); if (cat) { - var thing=event.data.thing.label; + var thing = event.data.thing.label; alertify.confirm( `Restauration de l'élément ${thing}`, `Voulez-vous vraiment restaurer l'élément ${thing} ?`, - function(data) { + function (data) { cat.restoreThing(thing); scases.save(); - show_scase(scase,cat.name); + show_scase(scase, cat.name); auto_sync_local_data(); }, null ); } } -} +}; /******************** * Show one scase *******************/ -show_cat=function(scase, cat, displayed) { - var panel=$('
'); - var panel_heading=$(''); - var panel_title=$('

'+cat.name+'

'); - panel_title.data('scase', scase); - panel_title.data('cat', cat); - panel_title.bind('click',on_title_click); +show_cat = function (scase, cat, displayed) { + var panel = $('
'); + var panel_heading = $(''); + var panel_title = $('

' + cat.name + "

"); + panel_title.data("scase", scase); + panel_title.data("cat", cat); + panel_title.bind("click", on_title_click); - var stats=cat.stats(); - var tag=$(''); - if (stats.things==stats.done) { - tag.append($('')); - } - else { - tag.append($(''+stats.done+' / '+stats.things+'')); + var stats = cat.stats(); + var tag = $(''); + if (stats.things == stats.done) { + tag.append( + $( + '' + ) + ); + } else { + tag.append( + $('' + stats.done + " / " + stats.things + "") + ); } - var delete_btn=$(''); - delete_btn.bind('click',{'cat': cat},on_delete_cat_btn_click); + var delete_btn = $( + '' + ); + delete_btn.bind("click", { cat: cat }, on_delete_cat_btn_click); tag.append(delete_btn); - var rename_btn=$(''); - rename_btn.bind('click',{'cat': cat},on_rename_cat_btn_click); + var rename_btn = $( + '' + ); + rename_btn.bind("click", { cat: cat }, on_rename_cat_btn_click); tag.append(rename_btn); panel_title.append(tag); - panel_heading.append(panel_title); panel.append(panel_heading); - var panel_collapse=$('
'); + var panel_collapse = $( + '
' + ); if (displayed) { - panel_collapse.addClass('in'); + panel_collapse.addClass("in"); } - var ul=$(''); + var ul = $(''); for (idx in cat.things) { if (cat.things[idx].removed) { continue; } - var li=$('
  • '+cat.things[idx].label+'
  • '); - if (cat.things[idx].nb>1) { - li.append(' ('+cat.things[idx].nb+')'); + var li = $( + '
  • ' + + cat.things[idx].label + + "
  • " + ); + if (cat.things[idx].nb > 1) { + li.append(" (" + cat.things[idx].nb + ")"); } if (cat.things[idx].checked) { - li.addClass('checked'); + li.addClass("checked"); } - li.bind('click',on_li_click); + li.bind("click", on_li_click); - var li_actions=$(''); + var li_actions = $(''); - var delete_el_btn=$(''); - delete_el_btn.bind('click',{'cat': cat,'thing': cat.things[idx]},on_delete_thing_btn_click); + var delete_el_btn = $( + '' + ); + delete_el_btn.bind( + "click", + { cat: cat, thing: cat.things[idx] }, + on_delete_thing_btn_click + ); li_actions.append(delete_el_btn); - var edit_el_btn=$(''); - edit_el_btn.bind('click',{'cat': cat,'thing': cat.things[idx]},on_edit_thing_btn_click); + var edit_el_btn = $( + '' + ); + edit_el_btn.bind( + "click", + { cat: cat, thing: cat.things[idx] }, + on_edit_thing_btn_click + ); li_actions.append(edit_el_btn); li.append(li_actions); ul.append(li); } - var li=$('
  • Ajouter un élément
  • '); - li.bind('click',on_li_add_click); + var li = $( + '
  • Ajouter un élément
  • ' + ); + li.bind("click", on_li_add_click); ul.append(li); panel_collapse.append(ul); panel.append(panel_collapse); - $('#cats').append(panel); -} + $("#cats").append(panel); +}; -show_scase=function(scase,display_cat) { - clear_page('

    '+scase.name+'

    '); - scase.cats.each(function(idx,cat) { +show_scase = function (scase, display_cat) { + clear_page( + '

    ' + + scase.name + + '

    ' + ); + scase.cats.each(function (idx, cat) { if (cat.removed) { return; } - show_cat(scase, cat,(cat.name==display_cat)); + show_cat(scase, cat, cat.name == display_cat); }); set_location(scase.name, display_cat); - show_menu('scase'); -} + show_menu("scase"); +}; -on_back_to_scases_btn_click=function(e) { +on_back_to_scases_btn_click = function (e) { e.preventDefault(); navbar_collapse_hide(); show_scases(); -} +}; /******************** * Show scase trash *******************/ -show_scase_trash=function(scase,display_cat) { - clear_page('

    '+scase.name+' : Corbeille

    '); +show_scase_trash = function (scase, display_cat) { + clear_page( + '

    ' + + scase.name + + ' : Corbeille

    ' + ); - $('#content h3 #back_btn').bind('click', {'scase': scase}, function(event) { + $("#content h3 #back_btn").bind("click", { scase: scase }, function (event) { show_scase(event.data.scase); }); - scase.cats.each(function(idx,cat) { - show_cat_trash(scase, cat, (cat.name==display_cat)); + scase.cats.each(function (idx, cat) { + show_cat_trash(scase, cat, cat.name == display_cat); }); - if ($('#cats .panel').length==0) { - $('#content').append('

    La corbeille est vide.

    '); + if ($("#cats .panel").length == 0) { + $("#content").append('

    La corbeille est vide.

    '); } - set_location(scase.name, display_cat, 'trash'); - show_menu('scase'); -} + set_location(scase.name, display_cat, "trash"); + show_menu("scase"); +}; -show_cat_trash=function(scase, cat,displayed) { - var panel=$('
    '); - var panel_heading=$(''); - var panel_title=$('

    '+cat.name+'

    '); - panel_title.data('scase', scase); - panel_title.data('cat', cat); - panel_title.data('trash', true); +show_cat_trash = function (scase, cat, displayed) { + var panel = $('
    '); + var panel_heading = $(''); + var panel_title = $('

    ' + cat.name + "

    "); + panel_title.data("scase", scase); + panel_title.data("cat", cat); + panel_title.data("trash", true); - var tag=$(''); + var tag = $(''); panel_title.append(tag); - panel_heading.append(panel_title); panel.append(panel_heading); - if (cat.removed) { - var stats=cat.stats(); - tag.append($(''+stats.things+'')); + var stats = cat.stats(); + tag.append($('' + stats.things + "")); - var restore_btn=$(''); - restore_btn.bind('click',{'cat': cat},on_restore_cat_btn_click); + var restore_btn = $( + '' + ); + restore_btn.bind("click", { cat: cat }, on_restore_cat_btn_click); tag.append(restore_btn); - } - else { - var deleted_things=[]; + } else { + var deleted_things = []; for (idx in cat.things) { if (cat.things[idx].removed) { deleted_things.push(cat.things[idx]); } } - if (deleted_things.length==0) { + if (deleted_things.length == 0) { return true; } - panel_title.bind('click',on_title_click); - tag.append($(''+deleted_things.length+'')); + panel_title.bind("click", on_title_click); + tag.append($('' + deleted_things.length + "")); - var panel_collapse=$('
    '); + var panel_collapse = $( + '
    ' + ); if (displayed) { - panel_collapse.addClass('in'); + panel_collapse.addClass("in"); } - var ul=$(''); + var ul = $(''); for (idx in deleted_things) { - var li=$('
  • '+deleted_things[idx].label+'
  • '); - var li_actions=$(''); + var li = $( + '
  • ' + + deleted_things[idx].label + + "
  • " + ); + var li_actions = $(''); - var restore_el_btn=$(''); - restore_el_btn.bind('click',{'cat': cat,'thing': deleted_things[idx]},on_restore_thing_btn_click); + var restore_el_btn = $( + '' + ); + restore_el_btn.bind( + "click", + { cat: cat, thing: deleted_things[idx] }, + on_restore_thing_btn_click + ); li_actions.append(restore_el_btn); li.append(li_actions); @@ -802,127 +917,147 @@ show_cat_trash=function(scase, cat,displayed) { panel.append(panel_collapse); } - $('#cats').append(panel); -} + $("#cats").append(panel); +}; -on_back_to_scase_btn_click=function(e) { +on_back_to_scase_btn_click = function (e) { e.preventDefault(); navbar_collapse_hide(); show_scase(e.data.scase); -} +}; /******************** * Show scases *******************/ -show_scases=function() { +show_scases = function () { clear_page('

    Vos valises

    '); - scases.each(function(idx,scase) { + scases.each(function (idx, scase) { if (scase.removed) { return; } - var stats=scase.stats(); - var tag=''; - if (stats.things==stats.done) { - tag+=''; + var stats = scase.stats(); + var tag = ''; + if (stats.things == stats.done) { + tag += + ''; + } else { + tag += + '' + stats.done + " / " + stats.things + ""; } - else { - tag+=''+stats.done+' / '+stats.things+''; - } - tag+=''; + tag += ""; - var li=$('
  • '+scase.name+''+tag+'
  • '); - li.bind('click',on_scase_click); - $('#scases').append(li); + var li = $( + '
  • ' + + scase.name + + "" + + tag + + "
  • " + ); + li.bind("click", on_scase_click); + $("#scases").append(li); }); set_location(); - show_menu('scases'); -} + show_menu("scases"); +}; -on_scase_click=function(event) { - var li=$(this); - var scase=scases.byName(li.data('name')); +on_scase_click = function (event) { + var li = $(this); + var scase = scases.byName(li.data("name")); show_scase(scase); -} +}; /******************** * Show scases trash *******************/ -show_scases_trash=function() { - clear_page('

    Corbeille

    '); - $('#content h3 #back_btn').bind('click', function(event) { +show_scases_trash = function () { + clear_page( + '

    Corbeille

    ' + ); + $("#content h3 #back_btn").bind("click", function (event) { show_scases(); }); - scases.each(function(idx,scase) { + scases.each(function (idx, scase) { if (!scase.removed) { return; } - var stats=scase.stats(); - var tags=$(''); - tags.append(''+stats.things+''); + var stats = scase.stats(); + var tags = $(''); + tags.append('' + stats.things + ""); - var restore_btn=$(''); - restore_btn.bind('click',{'scase': scase},on_restore_scase_btn_click); + var restore_btn = $( + '' + ); + restore_btn.bind("click", { scase: scase }, on_restore_scase_btn_click); tags.append(restore_btn); - var li=$('
  • '+scase.name+'
  • '); + var li = $( + '
  • ' + + scase.name + + "
  • " + ); li.append(tags); - $('#scases').append(li); + $("#scases").append(li); }); - if ($('#scases li').length==0) { - $('#content').append('

    Aucune valise dans la corbeille.

    '); + if ($("#scases li").length == 0) { + $("#content").append( + '

    Aucune valise dans la corbeille.

    ' + ); } - show_menu('scases'); -} + show_menu("scases"); +}; -on_scases_trash_btn=function(e) { +on_scases_trash_btn = function (e) { e.preventDefault(); navbar_collapse_hide(); show_scases_trash(); -} +}; -clear_page=function(new_content) { +clear_page = function (new_content) { if (new_content) { - $('#content').html(new_content); + $("#content").html(new_content); + } else { + $("#content").html(""); } - else { - $('#content').html(''); - } -} +}; /************************ * Show menu ***********************/ -show_menu=function(menu) { - $('.menu').css('display','none'); - $('.menu-'+menu).css('display','block'); -} +show_menu = function (menu) { + $(".menu").css("display", "none"); + $(".menu-" + menu).css("display", "block"); +}; /******************* * pleaseWaitDialog *******************/ -pleaseWaitShow=function() { - $('#please_wait_modal').modal('show'); -} +pleaseWaitShow = function () { + $("#please_wait_modal").modal("show"); +}; -pleaseWaitHide=function() { - $('#please_wait_modal').modal('hide'); -} +pleaseWaitHide = function () { + $("#please_wait_modal").modal("hide"); +}; /**************** * Nav bars ****************/ -navbar_collapse_hide=function() { - if ($('#navbar-top-collapse').hasClass('in')) { - $('#navbar-top-collapse').collapse('hide'); +navbar_collapse_hide = function () { + if ($("#navbar-top-collapse").hasClass("in")) { + $("#navbar-top-collapse").collapse("hide"); } -} +}; /******************** * Clear local data ********************/ -clear_local_data=function() { +clear_local_data = function () { navbar_collapse_hide(); alertify.confirm( "Suppression de toutes les données locales", @@ -930,85 +1065,99 @@ clear_local_data=function() { on_confirm_clear_local_data, null ); -} +}; -on_confirm_clear_local_data=function(data) { +on_confirm_clear_local_data = function (data) { delete localStorage.scases; location.reload(); -} +}; /******************** * Clear local data ********************/ -load_example_data=function() { +load_example_data = function () { navbar_collapse_hide(); alertify.confirm( "Chargement des données d'exemple", "Etes-vous sûre de vouloir charger les données d'exemple à la place de vos propres données (action irréversible) ?", - function() { + function () { delete localStorage.scases; - scases=new SCaseList(); + scases = new SCaseList(); scases.importExampleData(); scases.save(); show_scases(); }, null ); -} +}; /******************************* * Import/Export local data *******************************/ -export_local_data=function() { +export_local_data = function () { navbar_collapse_hide(); - $('#export_local_data').attr('href','data:application/json;base64,' + btoa(JSON.stringify(scases.export()))); -} + $("#export_local_data").attr( + "href", + "data:application/json;base64," + btoa(JSON.stringify(scases.export())) + ); +}; -import_local_data=function() { +import_local_data = function () { navbar_collapse_hide(); - var input=$(''); - input.css('display', 'none'); - input.bind('change',{'input': input},function(e) { + var input = $(''); + input.css("display", "none"); + input.bind("change", { input: input }, function (e) { pleaseWaitShow(); - var input=e.data.input; - var file=input.prop('files')[0]; + var input = e.data.input; + var file = input.prop("files")[0]; if (file) { var reader = new FileReader(); - $(reader).bind('load', function(e) { + $(reader).bind("load", function (e) { if ( - $.type(e.target.result)!='string' - || ! e.target.result.startsWith('data:application/json;base64,') + $.type(e.target.result) != "string" || + !e.target.result.startsWith("data:application/json;base64,") ) { pleaseWaitHide(); - alertify.notify('Fichier.', "error", 5); + alertify.notify("Fichier.", "error", 5); return; } try { - json_data=atob(e.target.result.replace('data:application/json;base64,','')); - data=JSON.parse(json_data); - } - catch (e) { + json_data = atob( + e.target.result.replace("data:application/json;base64,", "") + ); + data = JSON.parse(json_data); + } catch (e) { pleaseWaitHide(); - alertify.notify('Impossible de décodé le fichier.', "error", 5); + alertify.notify("Impossible de décodé le fichier.", "error", 5); return; } pleaseWaitHide(); alertify.confirm( "Importation depuis un fichier", "Etes-vous sûre de vouloir écraser vos données locales par celle issues de ce fichier ?", - function() { - var backData=scases.export(); - scases=new SCaseList(); + function () { + var backData = scases.export(); + scases = new SCaseList(); if (scases.loadFromJsonData(data)) { alertify.notify("Le fichier a bien été importé.", "success", 3); - } - else { - alertify.notify("Une erreur est survenue en chargeant ce fichier. Restauration des données précédentes...", "error", 5); + } else { + alertify.notify( + "Une erreur est survenue en chargeant ce fichier. Restauration des données précédentes...", + "error", + 5 + ); if (scases.loadFromJsonData(backData)) { - alertify.notify("Les données précédentes ont bien été restaurées.", "success", 5); - } - else { - alertify.notify("Une erreur est survenue en restaurant les données précédentes.", "error", 5); + alertify.notify( + "Les données précédentes ont bien été restaurées.", + "success", + 5 + ); + } else { + alertify.notify( + "Une erreur est survenue en restaurant les données précédentes.", + "error", + 5 + ); } } show_scases(); @@ -1019,374 +1168,397 @@ import_local_data=function() { reader.readAsDataURL(file); } }); - $('body').append(input); + $("body").append(input); input[0].click(); -} +}; /****************************** * Authentication ******************************/ -show_user=function() { +show_user = function () { if (user.connected()) { - $('#login').parent().css('display', 'none'); - $('#logout').parent().css('display', 'block'); - $('#username').html(user.name); + $("#login").parent().css("display", "none"); + $("#logout").parent().css("display", "block"); + $("#username").html(user.name); + } else { + $("#login").parent().css("display", "block"); + $("#logout").parent().css("display", "none"); + $("#username").html(""); } - else { - $('#login').parent().css('display', 'block'); - $('#logout').parent().css('display', 'none'); - $('#username').html(""); - } -} +}; -on_login_button_click=function() { +on_login_button_click = function () { if (user.connected()) return; navbar_collapse_hide(); - $('#login_modal').modal('show'); -} + $("#login_modal").modal("show"); +}; -on_valid_login_modal=function (e) { +on_valid_login_modal = function (e) { e.preventDefault(); - var username=$('#login_username').val(); - var password=$('#login_password').val(); + var username = $("#login_username").val(); + var password = $("#login_password").val(); if (!username || !password) { - alertify.notify("Vous devez saisir votre nom d'utilisateur et votre mot de passe !", "error", 5); + alertify.notify( + "Vous devez saisir votre nom d'utilisateur et votre mot de passe !", + "error", + 5 + ); return; } - $.post( - 'login', - { - username: username, - password: password, - } - ).done(function(data) { - if (data.username && data.token) { - user.token = data.token; - user.username = data.username; - user.name = data.name?data.name:username; - user.save(); - $('#login_modal').modal('hide'); - show_user(); - alertify.notify("Connecté.", "success", 3); - propose_sync_local_data(); - } - else { - alertify.notify("Nom d'utilisateur ou mot de passe.", "error", 5); - } - }).fail(function() { - alertify.notify('Une erreur est survenue en vous identifiant. Merci de réessayer ultèrieument.', "error", 5); - }); -} + $.post("login", { + username: username, + password: password, + }) + .done(function (data) { + if (data.username && data.token) { + user.token = data.token; + user.username = data.username; + user.name = data.name ? data.name : username; + user.save(); + $("#login_modal").modal("hide"); + show_user(); + alertify.notify("Connecté.", "success", 3); + propose_sync_local_data(); + } else { + alertify.notify("Nom d'utilisateur ou mot de passe.", "error", 5); + } + }) + .fail(function () { + alertify.notify( + "Une erreur est survenue en vous identifiant. Merci de réessayer ultèrieument.", + "error", + 5 + ); + }); +}; -on_show_login_modal=function () { - $('#login_username').val(user.username?user.username:""); - if (user.username) - $('#login_password').focus(); - else - $('#login_username').focus(); -} +on_show_login_modal = function () { + $("#login_username").val(user.username ? user.username : ""); + if (user.username) $("#login_password").focus(); + else $("#login_username").focus(); +}; -on_close_login_modal=function () { - $('#login_modal form')[0].reset(); -} +on_close_login_modal = function () { + $("#login_modal form")[0].reset(); +}; -on_logout_button_click=function() { +on_logout_button_click = function () { if (!user.connected()) return; navbar_collapse_hide(); alertify.confirm( "Déconnexion", "Voulez-vous vraiment vous déconnecter ?", - function(data) { - $.post( - 'logout', - { - username: user.username, - token: user.token, - } - ).done(function(data) { - if (data.success) { - user.reset(); - user.save(); - show_user(); - alertify.notify("Déconnecté.", "success", 3); - } - else { - alertify.notify("Une erreur est survenue en vous déconnectant. Merci de réessayer ultèrieument.", "error", 5); - } - }).fail(function() { - alertify.notify('Une erreur est survenue en vous déconnectant. Merci de réessayer ultèrieument.', "error", 5); - }); + function (data) { + $.post("logout", { + username: user.username, + token: user.token, + }) + .done(function (data) { + if (data.success) { + user.reset(); + user.save(); + show_user(); + alertify.notify("Déconnecté.", "success", 3); + } else { + alertify.notify( + "Une erreur est survenue en vous déconnectant. Merci de réessayer ultèrieument.", + "error", + 5 + ); + } + }) + .fail(function () { + alertify.notify( + "Une erreur est survenue en vous déconnectant. Merci de réessayer ultèrieument.", + "error", + 5 + ); + }); }, null ); -} +}; -is_connected=function() { +is_connected = function () { return user && user.token; -} - +}; /******************************* * Sync local data with server *******************************/ -sync_local_data = function(callback) { +sync_local_data = function (callback) { if (!is_connected()) { if (callback) callback(false); return; } - $.post( - 'sync', - { - username: user.username, - token: user.token, - data: JSON.stringify(scases.export()), - } - ).done(function(data) { - if (data.scases) { - var backData=scases.export(); - scases=new SCaseList(); - if (scases.loadFromJsonData(data)) { - scases.save(); - alertify.notify("Données synchronisées.", "success", 3); - if (callback) callback(true); - } - else { - alertify.notify("Une erreur est survenue en chargeant les données issues du serveur. Restauration des données précédentes...", "error", 5); - if (scases.loadFromJsonData(backData)) { - alertify.notify("Les données précédentes ont bien été restaurées.", "success", 5); - } - else { - alertify.notify("Une erreur est survenue en restaurant les données précédentes.", "error", 5); + $.post("sync", { + username: user.username, + token: user.token, + data: JSON.stringify(scases.export()), + }) + .done(function (data) { + if (data.scases) { + var backData = scases.export(); + scases = new SCaseList(); + if (scases.loadFromJsonData(data)) { + scases.save(); + alertify.notify("Données synchronisées.", "success", 3); + if (callback) callback(true); + } else { + alertify.notify( + "Une erreur est survenue en chargeant les données issues du serveur. Restauration des données précédentes...", + "error", + 5 + ); + if (scases.loadFromJsonData(backData)) { + alertify.notify( + "Les données précédentes ont bien été restaurées.", + "success", + 5 + ); + } else { + alertify.notify( + "Une erreur est survenue en restaurant les données précédentes.", + "error", + 5 + ); + } + if (callback) callback(false); } + } else { + alertify.notify( + "Une erreur est survenue en synchronisant vos données. Merci de réessayer ultèrieument.", + "error", + 5 + ); if (callback) callback(false); } - } - else { - alertify.notify("Une erreur est survenue en synchronisant vos données. Merci de réessayer ultèrieument.", "error", 5); + }) + .fail(function (xhr, status, error) { + if (xhr.status == 401) { + user.token = null; + user.save(); + show_user(); + alertify.notify( + "Votre session semble avoir expirée, merci de vous réauthentifier.", + "error", + 8 + ); + $("#login_modal").modal("show"); + } else { + alertify.notify( + "Une erreur est survenue en synchronisant vos données. Merci de réessayer ultèrieument.", + "error", + 5 + ); + } if (callback) callback(false); - } - }).fail(function(xhr, status, error) { - if (xhr.status == 401) { - user.token = null; - user.save(); - show_user(); - alertify.notify("Votre session semble avoir expirée, merci de vous réauthentifier.", "error", 8); - $('#login_modal').modal('show'); - } - else { - alertify.notify("Une erreur est survenue en synchronisant vos données. Merci de réessayer ultèrieument.", "error", 5); - } - if (callback) callback(false); - }); -} + }); +}; _auto_sync_timeout = false; -auto_sync_local_data = function() { +auto_sync_local_data = function () { if (_auto_sync_timeout) clearTimeout(_auto_sync_timeout); if (!is_connected() || !navigator.onLine) return; _auto_sync_timeout = setTimeout(sync_local_data, 3000); -} -on_sync_local_data_btn_click=function() { +}; +on_sync_local_data_btn_click = function () { navbar_collapse_hide(); if (!is_connected()) { - alertify.notify("Vous devez vous connecter avant de pouvoir synchroniser vos données.", "error", 5); - $('#login_modal').modal('show'); + alertify.notify( + "Vous devez vous connecter avant de pouvoir synchroniser vos données.", + "error", + 5 + ); + $("#login_modal").modal("show"); return; } pleaseWaitShow(); - sync_local_data(function(success) { + sync_local_data(function (success) { pleaseWaitHide(); }); -} -propose_sync_local_data = function() { +}; +propose_sync_local_data = function () { alertify.confirm( "Synchronisation de vos valises depuis le serveur", "Voulez-vous synchroniser vos valises depuis le serveur ?", - function() { + function () { pleaseWaitShow(); - sync_local_data( - function(success) { - pleaseWaitHide(); - if (!success) return; - if (scases.count() == 0) - propose_example_data(); - else - refresh_location(); - } - ); + sync_local_data(function (success) { + pleaseWaitHide(); + if (!success) return; + if (scases.count() == 0) propose_example_data(); + else refresh_location(); + }); }, refresh_location ); -} +}; /*********************** * Manage location hash ***********************/ -set_location = function(scase, cat, detail) { +set_location = function (scase, cat, detail) { console.log(`set_location(${scase}, ${cat}, ${detail})`); var parts = []; if (scase) parts[0] = scase; if (cat) parts[1] = cat; if (detail) parts[2] = detail; location.hash = parts.join("|"); -} +}; -parse_location = function(value) { +parse_location = function (value) { value = typeof value == "undefined" ? location.hash : value; console.log(`parse_location(${value})`); - parts = ( - typeof value == "undefined" ? location.hash : value - ).split("|"); + parts = (typeof value == "undefined" ? location.hash : value).split("|"); return { - scase: parts[0]?decodeURI(parts[0].substring(1)):null, - cat: parts[0] && parts[1]?decodeURI(parts[1]):null, + scase: parts[0] ? decodeURI(parts[0].substring(1)) : null, + cat: parts[0] && parts[1] ? decodeURI(parts[1]) : null, detail: parts[2] ? decodeURI(parts[2]) : null, }; -} +}; -refresh_location = function() { +refresh_location = function () { var info = parse_location(); console.log(`refresh_location(${info.scase}, ${info.cat}, ${info.detail})`); var scase = info.scase ? scases.byName(info.scase) : null; - if (!scase) - show_scases(); - else if (info.detail == 'trash') - show_scase_trash(scase, info.cat); - else - show_scase(scase, info.cat); -} + if (!scase) show_scases(); + else if (info.detail == "trash") show_scase_trash(scase, info.cat); + else show_scase(scase, info.cat); +}; /*********************** * Welcome modal ***********************/ -on_welcome_connect_click = function() { - $('#welcome_modal').modal('hide'); - $('#login_modal').modal('show'); -} +on_welcome_connect_click = function () { + $("#welcome_modal").modal("hide"); + $("#login_modal").modal("show"); +}; -on_welcome_annonymous_click = function() { - $('#welcome_modal').modal('hide'); +on_welcome_annonymous_click = function () { + $("#welcome_modal").modal("hide"); if (scases.count() == 0) propose_example_data(); -} +}; -propose_example_data = function() { +propose_example_data = function () { alertify.confirm( "Un exemple de valise ?", "Souhaitez-vous charger un exemple de valise pour commencer ?", - function() { + function () { scases.importExampleData(); scases.save(); refresh_location(); }, - null, + null ); -} +}; /********************* * Activate *********************/ -$( document ).ready( function() { - if(typeof(localStorage)==="undefined"){ +$(document).ready(function () { + if (typeof localStorage === "undefined") { alertify.notify( - 'Votre navigateur internet ne support pas le stockage de données locale. Vous ne pouvez donc malheureusment pas utiliser cette application.', "error", 5); + "Votre navigateur internet ne support pas le stockage de données locale. Vous ne pouvez donc malheureusment pas utiliser cette application.", + "error", + 5 + ); return; } pleaseWaitShow(); - $('#clear_local_data').bind('click',clear_local_data); - $('#load_example_data').bind('click',load_example_data); - $('#import_local_data').bind('click',import_local_data); - $('#export_local_data').bind('click',export_local_data); - $('#sync_local_data').bind('click',on_sync_local_data_btn_click); + $("#clear_local_data").bind("click", clear_local_data); + $("#load_example_data").bind("click", load_example_data); + $("#import_local_data").bind("click", import_local_data); + $("#export_local_data").bind("click", export_local_data); + $("#sync_local_data").bind("click", on_sync_local_data_btn_click); - $('#add_scase_btn').bind('click',on_add_scase_btn_click); - $('#add_scase_submit').bind('click',on_valid_add_scase_modal); - $("#add_scase_modal").on('shown.bs.modal',on_show_add_scase_modal); - $("#add_scase_modal").on('hidden.bs.modal',on_close_add_scase_modal); - $("#add_scase_modal form").bind('submit',on_valid_add_scase_modal); + $("#add_scase_btn").bind("click", on_add_scase_btn_click); + $("#add_scase_submit").bind("click", on_valid_add_scase_modal); + $("#add_scase_modal").on("shown.bs.modal", on_show_add_scase_modal); + $("#add_scase_modal").on("hidden.bs.modal", on_close_add_scase_modal); + $("#add_scase_modal form").bind("submit", on_valid_add_scase_modal); - $('#scases_trash_btn').bind('click',on_scases_trash_btn); + $("#scases_trash_btn").bind("click", on_scases_trash_btn); - $('#rename_scase_btn').bind('click',on_rename_scase_btn_click); - $('#rename_scase_submit').bind('click',on_valid_rename_scase_modal); - $("#rename_scase_modal").on('shown.bs.modal',on_show_rename_scase_modal); - $("#rename_scase_modal").on('hidden.bs.modal',on_close_rename_scase_modal); - $("#rename_scase_modal form").bind('submit',on_valid_rename_scase_modal); + $("#rename_scase_btn").bind("click", on_rename_scase_btn_click); + $("#rename_scase_submit").bind("click", on_valid_rename_scase_modal); + $("#rename_scase_modal").on("shown.bs.modal", on_show_rename_scase_modal); + $("#rename_scase_modal").on("hidden.bs.modal", on_close_rename_scase_modal); + $("#rename_scase_modal form").bind("submit", on_valid_rename_scase_modal); - $('#copy_scase_btn').bind('click',on_copy_scase_btn_click); - $('#copy_scase_submit').bind('click',on_valid_copy_scase_modal); - $("#copy_scase_modal").on('shown.bs.modal',on_show_copy_scase_modal); - $("#copy_scase_modal").on('hidden.bs.modal',on_close_copy_scase_modal); - $("#copy_scase_modal form").bind('submit',on_valid_copy_scase_modal); + $("#copy_scase_btn").bind("click", on_copy_scase_btn_click); + $("#copy_scase_submit").bind("click", on_valid_copy_scase_modal); + $("#copy_scase_modal").on("shown.bs.modal", on_show_copy_scase_modal); + $("#copy_scase_modal").on("hidden.bs.modal", on_close_copy_scase_modal); + $("#copy_scase_modal form").bind("submit", on_valid_copy_scase_modal); - $('#reset_scase_btn').bind('click',on_reset_scase_btn_click); - $('#delete_scase_btn').bind('click',on_delete_scase_btn_click); - $('#scase_trash_btn').bind('click',on_scase_trash_btn_click); + $("#reset_scase_btn").bind("click", on_reset_scase_btn_click); + $("#delete_scase_btn").bind("click", on_delete_scase_btn_click); + $("#scase_trash_btn").bind("click", on_scase_trash_btn_click); - $('#add_cat_btn').bind('click',on_add_cat_btn_click); - $('#add_cat_submit').bind('click',on_valid_add_cat_modal); - $("#add_cat_modal").on('shown.bs.modal',on_show_add_cat_modal); - $("#add_cat_modal").on('hidden.bs.modal',on_close_add_cat_modal); - $("#add_cat_modal form").bind('submit',on_valid_add_cat_modal); + $("#add_cat_btn").bind("click", on_add_cat_btn_click); + $("#add_cat_submit").bind("click", on_valid_add_cat_modal); + $("#add_cat_modal").on("shown.bs.modal", on_show_add_cat_modal); + $("#add_cat_modal").on("hidden.bs.modal", on_close_add_cat_modal); + $("#add_cat_modal form").bind("submit", on_valid_add_cat_modal); - $('#rename_cat_submit').bind('click',on_valid_rename_cat_modal); - $("#rename_cat_modal").on('shown.bs.modal',on_show_rename_cat_modal); - $("#rename_cat_modal").on('hidden.bs.modal',on_close_rename_cat_modal); - $("#rename_cat_modal form").bind('submit',on_valid_rename_cat_modal); + $("#rename_cat_submit").bind("click", on_valid_rename_cat_modal); + $("#rename_cat_modal").on("shown.bs.modal", on_show_rename_cat_modal); + $("#rename_cat_modal").on("hidden.bs.modal", on_close_rename_cat_modal); + $("#rename_cat_modal form").bind("submit", on_valid_rename_cat_modal); - $('#back_to_scases').bind('click',on_back_to_scases_btn_click); + $("#back_to_scases").bind("click", on_back_to_scases_btn_click); - $('input.add_thing_label').bind('focus',on_add_thing_label_focus); - $('#add_thing_submit').bind('click',on_valid_add_thing_modal); - $("#add_thing_modal").on('shown.bs.modal',on_show_add_thing_modal); - $("#add_thing_modal").on('hidden.bs.modal',on_close_add_thing_modal); - $("#add_thing_modal form").bind('submit',on_valid_add_thing_modal); + $("input.add_thing_label").bind("focus", on_add_thing_label_focus); + $("#add_thing_submit").bind("click", on_valid_add_thing_modal); + $("#add_thing_modal").on("shown.bs.modal", on_show_add_thing_modal); + $("#add_thing_modal").on("hidden.bs.modal", on_close_add_thing_modal); + $("#add_thing_modal form").bind("submit", on_valid_add_thing_modal); - $('#edit_thing_submit').bind('click',on_valid_edit_thing_modal); - $("#edit_thing_modal").on('shown.bs.modal',on_show_edit_thing_modal); - $("#edit_thing_modal").on('hidden.bs.modal',on_close_edit_thing_modal); - $("#edit_thing_modal form").bind('submit',on_valid_edit_thing_modal); + $("#edit_thing_submit").bind("click", on_valid_edit_thing_modal); + $("#edit_thing_modal").on("shown.bs.modal", on_show_edit_thing_modal); + $("#edit_thing_modal").on("hidden.bs.modal", on_close_edit_thing_modal); + $("#edit_thing_modal form").bind("submit", on_valid_edit_thing_modal); - $('#login').bind('click',on_login_button_click); - $('#logout').bind('click',on_logout_button_click); - $('#login_submit').bind('click',on_valid_login_modal); - $("#login_modal").on('shown.bs.modal',on_show_login_modal); - $("#login_modal").on('hidden.bs.modal',on_close_login_modal); - $("#login_modal form").bind('submit',on_valid_login_modal); + $("#login").bind("click", on_login_button_click); + $("#logout").bind("click", on_logout_button_click); + $("#login_submit").bind("click", on_valid_login_modal); + $("#login_modal").on("shown.bs.modal", on_show_login_modal); + $("#login_modal").on("hidden.bs.modal", on_close_login_modal); + $("#login_modal form").bind("submit", on_valid_login_modal); - $('#welcome_connect').bind('click',on_welcome_connect_click); - $('#welcome_annonymous').bind('click',on_welcome_annonymous_click); + $("#welcome_connect").bind("click", on_welcome_connect_click); + $("#welcome_annonymous").bind("click", on_welcome_annonymous_click); - $('#app-name').bind('click', show_scases); + $("#app-name").bind("click", show_scases); - user=new User(); + user = new User(); user.loadFromLocalStorage(); show_user(); - scases=new SCaseList(); - switch(scases.loadFromLocalStorage()) { + scases = new SCaseList(); + switch (scases.loadFromLocalStorage()) { case null: pleaseWaitHide(); - if (is_connected()) - propose_sync_local_data(); - else - $('#welcome_modal').modal('show'); + if (is_connected()) propose_sync_local_data(); + else $("#welcome_modal").modal("show"); break; case false: alertify.confirm( "Erreur en chargeant les données locales", - 'Une erreur est survenue en chargeant les données locales. On les purges ?', - function(data) { + "Une erreur est survenue en chargeant les données locales. On les purges ?", + function (data) { delete localStorage.scases; location.reload(); }, - function(data) { + function (data) { pleaseWaitHide(); location.reload(); - }, + } ); break; diff --git a/static/mysc_objects.js b/static/mysc_objects.js index 8b10662..2f54c30 100644 --- a/static/mysc_objects.js +++ b/static/mysc_objects.js @@ -1,61 +1,60 @@ - - function SCaseList() { - lastChange=0; + lastChange = 0; - this.importExampleData=function() { - var exampleData={ - 'Vacances': { - 'Papier': { - 'color': '#f00', - 'things': [ - {'label': 'Papier blanc', 'nb': 1 }, - {'label': 'Stylo', 'nb': 3 }, - {'label': "Carte d'identité", 'nb': 1 }, - ] + this.importExampleData = function () { + var exampleData = { + Vacances: { + Papier: { + color: "#f00", + things: [ + { label: "Papier blanc", nb: 1 }, + { label: "Stylo", nb: 3 }, + { label: "Carte d'identité", nb: 1 }, + ], }, - 'Multimédia' : { - 'color': '#0f0', - 'things': [ - {'label': 'Montre', 'nb': 1 }, - {'label': 'Chargeur montre', 'nb': 1 }, - {'label': 'PC portable', 'nb': 1 }, - ] - } - } + Multimédia: { + color: "#0f0", + things: [ + { label: "Montre", nb: 1 }, + { label: "Chargeur montre", nb: 1 }, + { label: "PC portable", nb: 1 }, + ], + }, + }, }; for (scaseName in exampleData) { - var scase=this.newSCase(scaseName); + var scase = this.newSCase(scaseName); for (catName in exampleData[scaseName]) { - var cat=scase.cats.newCat(catName); + var cat = scase.cats.newCat(catName); for (idx in exampleData[scaseName][catName].things) { - cat.newThing(exampleData[scaseName][catName].things[idx]['label'],exampleData[scaseName][catName].things[idx]['nb']); + cat.newThing( + exampleData[scaseName][catName].things[idx]["label"], + exampleData[scaseName][catName].things[idx]["nb"] + ); } } } - } + }; - this.loadFromLocalStorage=function(data) { - if (jQuery.type(localStorage.scases)!='undefined') { + this.loadFromLocalStorage = function (data) { + if (jQuery.type(localStorage.scases) != "undefined") { try { return this.loadFromJsonData(JSON.parse(localStorage.scases)); - } - catch(e) { + } catch (e) { return false; } } return null; - } + }; - this.loadFromJsonData=function(data) { + this.loadFromJsonData = function (data) { try { - this.lastChange=data.lastChange; + this.lastChange = data.lastChange; for (el in data.scases) { - this[el]=new SCase(false,false,data.scases[el]); + this[el] = new SCase(false, false, data.scases[el]); } return true; - } - catch(e) { + } catch (e) { for (el in this) { if (this.isSCase(this[el])) { delete this[el]; @@ -63,179 +62,181 @@ function SCaseList() { } } return false; - } + }; - this.export=function() { + this.export = function () { return { - 'lastChange': this.lastChange, - 'scases': this.each(function(idx,scase) { + lastChange: this.lastChange, + scases: this.each(function (idx, scase) { return scase.export(); - }) + }), }; - } + }; - this.import=function(data) { - ret={}; + this.import = function (data) { + ret = {}; for (el in this) { if (this.isSCase(this[el])) { delete ret[el]; } } - this.lastChange=data.lastChange; + this.lastChange = data.lastChange; for (el in data.scases) { - this[el]=new SCase(false,false,data.scases[el]); + this[el] = new SCase(false, false, data.scases[el]); } return true; - } + }; - this.save=function() { - localStorage.scases=JSON.stringify(this.export()); - } + this.save = function () { + localStorage.scases = JSON.stringify(this.export()); + }; - this.each=function(fct) { - var idx=0; - var ret={}; + this.each = function (fct) { + var idx = 0; + var ret = {}; for (el in this) { - if(this.isSCase(this[el])) { - ret[el]=fct(idx++,this[el]); + if (this.isSCase(this[el])) { + ret[el] = fct(idx++, this[el]); } } return ret; - } + }; - this.count=function() { - len=0; - this.each(function(idx,scase) { - len=len+1; + this.count = function () { + len = 0; + this.each(function (idx, scase) { + len = len + 1; }); return len; - } + }; - this.isSCase=function(el) { - return (jQuery.type(el)=='object' && jQuery.type(el.isSCase)=='function' && el.isSCase()); - } + this.isSCase = function (el) { + return ( + jQuery.type(el) == "object" && + jQuery.type(el.isSCase) == "function" && + el.isSCase() + ); + }; - this.byName=function(name) { + this.byName = function (name) { for (el in this) { - if(this.isSCase(this[el])) { - if (this[el].name==name) { + if (this.isSCase(this[el])) { + if (this[el].name == name) { return this[el]; } } } return false; - } + }; - this.byUUID=function(uuid) { + this.byUUID = function (uuid) { return this.isCase(this[uuid]) ? this[uuid] : null; - } + }; - this.removeSCase=function(name) { + this.removeSCase = function (name) { for (el in this) { - if (this.isSCase(this[el]) && this[el].name==name) { + if (this.isSCase(this[el]) && this[el].name == name) { this[el].remove(); return true; } } return false; - } + }; - this.newSCase=function(name) { + this.newSCase = function (name) { if (this.byName(this[name])) { - var scase=this.byName(name); + var scase = this.byName(name); if (scase.removed) { scase.restore(); return true; } - - } - else { - var uuid=uuid||generate_uuid(); - this[uuid]=new SCase(uuid,name); + } else { + var uuid = uuid || generate_uuid(); + this[uuid] = new SCase(uuid, name); return this[uuid]; } return false; - } + }; - this.renameSCase=function(name,newname) { - var scase=this.byName(name); + this.renameSCase = function (name, newname) { + var scase = this.byName(name); if (scase && !this.byName(newname)) { - scase.name=newname; - scase.lastChange=new Date().getTime(); + scase.name = newname; + scase.lastChange = new Date().getTime(); return scase; } return false; - } + }; - this.copySCase=function(name,newname) { - var orig_scase=this.byName(name); + this.copySCase = function (name, newname) { + var orig_scase = this.byName(name); if (this.isSCase(orig_scase) && !this.byName(newname)) { - var uuid=uuid||generate_uuid(); - this[uuid]=new SCase(false,false,orig_scase.export()); - this[uuid].uuid=uuid; - this[uuid].lastChange=new Date().getTime(); - this[uuid].name=newname; + var uuid = uuid || generate_uuid(); + this[uuid] = new SCase(false, false, orig_scase.export()); + this[uuid].uuid = uuid; + this[uuid].lastChange = new Date().getTime(); + this[uuid].name = newname; return this[uuid]; } return false; - } + }; - this.resetSCase=function(name) { + this.resetSCase = function (name) { for (el in this) { - if (this.isSCase(this[el]) && this[el].name==name) { + if (this.isSCase(this[el]) && this[el].name == name) { return this[el].reset(); } } return false; - } + }; } -function SCase(uuid,name,data) { - this.uuid=uuid||generate_uuid(); - this.name=name; - this.cats=new CatList(); - this.lastChange=new Date().getTime(); - this.removed=false; +function SCase(uuid, name, data) { + this.uuid = uuid || generate_uuid(); + this.name = name; + this.cats = new CatList(); + this.lastChange = new Date().getTime(); + this.removed = false; - this.isSCase=function() { + this.isSCase = function () { return true; - } + }; - this.import=function(data) { - this.uuid=data.uuid || generate_uuid(); - this.lastChange=data.lastChange || new Date().getTime(); - this.name=decodeURIComponent(data.name); - this.removed=data.removed||false; - if (jQuery.type(data.cats) == 'object') { - this.cats=new CatList(data.cats); + this.import = function (data) { + this.uuid = data.uuid || generate_uuid(); + this.lastChange = data.lastChange || new Date().getTime(); + this.name = decodeURIComponent(data.name); + this.removed = data.removed || false; + if (jQuery.type(data.cats) == "object") { + this.cats = new CatList(data.cats); } return true; - } + }; - this.export=function() { + this.export = function () { return { - 'uuid': this.uuid, - 'lastChange': this.lastChange, - 'name': encodeURIComponent(this.name), - 'removed': this.removed, - 'cats': this.cats.export() + uuid: this.uuid, + lastChange: this.lastChange, + name: encodeURIComponent(this.name), + removed: this.removed, + cats: this.cats.export(), }; - } + }; - this.byName=function(name) { + this.byName = function (name) { for (idx in this.cats) { - if (name==this.cats[idx].name) { + if (name == this.cats[idx].name) { return this.cats[idx]; } } return false; - } + }; - this.stats=function() { - var cats=0; - var things=0; - var things_done=0; - this.cats.each(function(cidx,cat) { + this.stats = function () { + var cats = 0; + var things = 0; + var things_done = 0; + this.cats.each(function (cidx, cat) { if (cat.removed) { return true; } @@ -251,399 +252,405 @@ function SCase(uuid,name,data) { } }); return { - 'cats': cats, - 'things': things, - 'done': things_done - } - } + cats: cats, + things: things, + done: things_done, + }; + }; - this.reset=function() { - this.cats.each(function(idx,cat) { + this.reset = function () { + this.cats.each(function (idx, cat) { for (idx in cat.things) { if (cat.things[idx].checked) { - cat.things[idx].checked=false; + cat.things[idx].checked = false; } } }); - this.lastChange=new Date().getTime(); + this.lastChange = new Date().getTime(); return true; - } + }; - this.remove=function() { - this.removed=true; - this.lastChange=new Date().getTime(); - } + this.remove = function () { + this.removed = true; + this.lastChange = new Date().getTime(); + }; - this.restore=function() { - this.removed=false; - this.lastChange=new Date().getTime(); - } + this.restore = function () { + this.removed = false; + this.lastChange = new Date().getTime(); + }; /* * Contructor */ - if (jQuery.type(data)=='object') { + if (jQuery.type(data) == "object") { try { this.import(data); - } - catch (e) { + } catch (e) { console.log(e); - alert('Une erreur est survenue en chargeant la valise '+this.name+' depuis le cache'); + alert( + "Une erreur est survenue en chargeant la valise " + + this.name + + " depuis le cache" + ); } } - } - function CatList(data) { - this.export=function() { - return this.each(function(idx,cat) { + this.export = function () { + return this.each(function (idx, cat) { return cat.export(); }); - } + }; - this.import=function(data) { + this.import = function (data) { for (el in this) { if (this.isCat(this[el])) { delete this[el]; } } for (el in data) { - this[el]=new Cat(el,false,false,data[el]); + this[el] = new Cat(el, false, false, data[el]); } return true; - } + }; - this.each=function(fct) { - var idx=0; - var ret={}; + this.each = function (fct) { + var idx = 0; + var ret = {}; for (el in this) { - if(this.isCat(this[el])) { - ret[el]=fct(idx++,this[el]); + if (this.isCat(this[el])) { + ret[el] = fct(idx++, this[el]); } } return ret; - } + }; - this.count=function() { - len=0; - this.each(function(idx,cat) { - len=len+1; + this.count = function () { + len = 0; + this.each(function (idx, cat) { + len = len + 1; }); return len; - } + }; - this.isCat=function(el) { - return (jQuery.type(el)=='object' && jQuery.type(el.isCat)=='function' && el.isCat()); - } + this.isCat = function (el) { + return ( + jQuery.type(el) == "object" && + jQuery.type(el.isCat) == "function" && + el.isCat() + ); + }; - this.byName=function(name) { + this.byName = function (name) { for (el in this) { - if(this.isCat(this[el])) { - if (this[el].name==name) { + if (this.isCat(this[el])) { + if (this[el].name == name) { return this[el]; } } } return false; - } + }; - this.byUUID=function(uuid) { + this.byUUID = function (uuid) { return this.isCas(this[uuid]) ? this[uuid] : null; - } + }; - this.newCat=function(name) { + this.newCat = function (name) { if (this.byName(name)) { - var cat=this.byName(name); + var cat = this.byName(name); if (cat.removed) { cat.restore(); return true; } - } - else { - var uuid=uuid||generate_uuid(); - this[uuid]=new Cat(uuid,name); + } else { + var uuid = uuid || generate_uuid(); + this[uuid] = new Cat(uuid, name); return this[uuid]; } return false; - } + }; - this.renameCat=function(name,newname) { - var cat=this.byName(name); + this.renameCat = function (name, newname) { + var cat = this.byName(name); if (cat && !this.byName(newname)) { - cat.name=newname; - cat.lastChange=new Date().getTime(); + cat.name = newname; + cat.lastChange = new Date().getTime(); return cat; } return false; - } + }; - this.removeCat=function(name) { + this.removeCat = function (name) { for (el in this) { - if (this.isCat(this[el]) && this[el].name==name) { + if (this.isCat(this[el]) && this[el].name == name) { this[el].remove(); return true; } } return false; - } + }; - this.restoreCat=function(name) { + this.restoreCat = function (name) { for (el in this) { - if (this.isCat(this[el]) && this[el].name==name && this[el].removed) { + if (this.isCat(this[el]) && this[el].name == name && this[el].removed) { this[el].restore(); return true; } } return false; - } - + }; /* * Contructor */ - if (jQuery.type(data)=='object') { + if (jQuery.type(data) == "object") { try { this.import(data); - } - catch (e) { + } catch (e) { console.log(e); - alert('Une erreur est survenue en chargeant la liste de catégorie depuis le cache'); + alert( + "Une erreur est survenue en chargeant la liste de catégorie depuis le cache" + ); } } - } -function Cat(uuid,name,color,data) { - this.uuid=generate_uuid(); - this.lastChange=new Date().getTime(); - this.name=name; - this.color=color || '#'+(0x1000000+(Math.random())*0xffffff).toString(16).substr(1,6); - this.things={}; - this.removed=false; +function Cat(uuid, name, color, data) { + this.uuid = generate_uuid(); + this.lastChange = new Date().getTime(); + this.name = name; + this.color = + color || + "#" + (0x1000000 + Math.random() * 0xffffff).toString(16).substr(1, 6); + this.things = {}; + this.removed = false; - this.isCat=function() { + this.isCat = function () { return true; - } + }; - this.import=function(data) { - this.uuid=data.uuid || generate_uuid(); - this.lastChange=data.lastChange||new Date().getTime(); - this.name=decodeURIComponent(data.name); - this.color=data.color; - this.removed=data.removed||false; - if (jQuery.type(data.things) == 'object') { + this.import = function (data) { + this.uuid = data.uuid || generate_uuid(); + this.lastChange = data.lastChange || new Date().getTime(); + this.name = decodeURIComponent(data.name); + this.color = data.color; + this.removed = data.removed || false; + if (jQuery.type(data.things) == "object") { for (tuuid in data.things) { - this.things[tuuid]=new Thing(tuuid); + this.things[tuuid] = new Thing(tuuid); this.things[tuuid].import(data.things[tuuid]); } } return true; - } + }; - this.export=function() { - var things={}; + this.export = function () { + var things = {}; for (tuuid in this.things) { - things[tuuid]=this.things[tuuid].export(); + things[tuuid] = this.things[tuuid].export(); } return { - 'uuid': this.uuid, - 'lastChange': this.lastChange, - 'name': encodeURIComponent(this.name), - 'color': this.color, - 'removed': this.removed, - 'things': things + uuid: this.uuid, + lastChange: this.lastChange, + name: encodeURIComponent(this.name), + color: this.color, + removed: this.removed, + things: things, }; - } + }; - this.byLabel=function(label) { + this.byLabel = function (label) { for (idx in this.things) { - if (label==this.things[idx].label) { + if (label == this.things[idx].label) { return this.things[idx]; } } return false; - } + }; - this.count=function() { + this.count = function () { return keys(this.things).length; - } + }; - this.stats=function() { - var count=0; - var done=0; + this.stats = function () { + var count = 0; + var done = 0; for (idx in this.things) { if (this.things[idx].removed) { continue; } if (this.things[idx].checked) { - done+=1; + done += 1; } - count+=1; + count += 1; } return { - 'things': count, - 'done': done + things: count, + done: done, }; - } + }; - this.newThing=function(label,nb) { + this.newThing = function (label, nb) { if (this.byLabel(label)) { - var thing=this.byLabel(label); + var thing = this.byLabel(label); if (thing.removed) { thing.restore(); thing.setChecked(false); thing.setNb(nb); return true; } - } - else { - var uuid=generate_uuid(); - this.things[uuid]=new Thing(uuid,label,nb); + } else { + var uuid = generate_uuid(); + this.things[uuid] = new Thing(uuid, label, nb); return true; } return false; - } + }; - this.renameThing=function(label,newlabel) { - var thing=this.byLabel(label); + this.renameThing = function (label, newlabel) { + var thing = this.byLabel(label); if (thing && !this.byLabel(newlabel)) { - thing.label=newlabel; - thing.lastChange=new Date().getTime(); + thing.label = newlabel; + thing.lastChange = new Date().getTime(); return thing; } return false; - } + }; - this.removeThing=function(label) { + this.removeThing = function (label) { for (idx in this.things) { - if (this.things[idx].label==label) { + if (this.things[idx].label == label) { this.things[idx].remove(); return true; } } return false; - } + }; - this.restoreThing=function(label) { + this.restoreThing = function (label) { for (idx in this.things) { - if (this.things[idx].label==label && this.things[idx].removed) { + if (this.things[idx].label == label && this.things[idx].removed) { this.things[idx].restore(); return true; } } return false; - } + }; - this.remove=function() { - this.removed=true; - this.lastChange=new Date().getTime(); - } - - this.restore=function() { - this.removed=false; - this.lastChange=new Date().getTime(); - } + this.remove = function () { + this.removed = true; + this.lastChange = new Date().getTime(); + }; + this.restore = function () { + this.removed = false; + this.lastChange = new Date().getTime(); + }; /* * Contructor */ - if (jQuery.type(data)=='object') { + if (jQuery.type(data) == "object") { try { this.import(data); - } - catch (e) { + } catch (e) { console.log(e); - alert('Une erreur est survenue en chargeant la catégorie catégorie '+this.name+' depuis le cache'); + alert( + "Une erreur est survenue en chargeant la catégorie catégorie " + + this.name + + " depuis le cache" + ); } } - } -function Thing(uuid,label,nb,checked) { - this.uuid=uuid||generate_uuid(); - this.lastChange=new Date().getTime(); - this.label=label; - this.nb=nb || 1; - this.checked=checked; - this.removed=false; +function Thing(uuid, label, nb, checked) { + this.uuid = uuid || generate_uuid(); + this.lastChange = new Date().getTime(); + this.label = label; + this.nb = nb || 1; + this.checked = checked; + this.removed = false; - this.import=function(data) { - this.uuid=data.uuid||generate_uuid(); - this.lastChange=data.lastChange||new Date().getTime(); - this.label=decodeURIComponent(data.label), - this.nb=data.nb||1; - this.checked=data.checked; - this.removed=data.removed||false; - } + this.import = function (data) { + this.uuid = data.uuid || generate_uuid(); + this.lastChange = data.lastChange || new Date().getTime(); + (this.label = decodeURIComponent(data.label)), (this.nb = data.nb || 1); + this.checked = data.checked; + this.removed = data.removed || false; + }; - this.export=function() { + this.export = function () { return { - 'uuid': this.uuid, - 'lastChange': this.lastChange, - 'label': encodeURIComponent(this.label), - 'nb': this.nb, - 'checked': this.checked, - 'removed': this.removed, + uuid: this.uuid, + lastChange: this.lastChange, + label: encodeURIComponent(this.label), + nb: this.nb, + checked: this.checked, + removed: this.removed, }; - } + }; - this.setNb=function(nb) { - this.nb=nb; - this.lastChange=new Date().getTime(); - } + this.setNb = function (nb) { + this.nb = nb; + this.lastChange = new Date().getTime(); + }; - this.setChecked=function(value) { - this.checked=value; - this.lastChange=new Date().getTime(); - console.log(`Thing<${this.uuid}>.setChecked(${this.checked}): ${this.lastChange}`); - } + this.setChecked = function (value) { + this.checked = value; + this.lastChange = new Date().getTime(); + console.log( + `Thing<${this.uuid}>.setChecked(${this.checked}): ${this.lastChange}` + ); + }; - this.remove=function() { - this.removed=true; - this.lastChange=new Date().getTime(); - } + this.remove = function () { + this.removed = true; + this.lastChange = new Date().getTime(); + }; - this.restore=function() { - this.removed=false; - this.lastChange=new Date().getTime(); - } + this.restore = function () { + this.removed = false; + this.lastChange = new Date().getTime(); + }; } function User() { this.username = null; this.name = null; this.token = null; - this.loadFromLocalStorage=function() { - if (jQuery.type(localStorage.user) == 'undefined') - return; + this.loadFromLocalStorage = function () { + if (jQuery.type(localStorage.user) == "undefined") return; try { - var data=JSON.parse(localStorage.user); + var data = JSON.parse(localStorage.user); this.username = data.username; this.name = data.name; this.token = data.token; - } - catch(e) { - alert('Erreur en chargeant vos informations de connexion. Merci de vous reconnecter.'); + } catch (e) { + alert( + "Erreur en chargeant vos informations de connexion. Merci de vous reconnecter." + ); } }; - this.connected=function() { + this.connected = function () { return this.username && this.token; - } + }; - this.reset=function() { + this.reset = function () { this.username = null; this.name = null; this.token = null; - } + }; - this.save=function() { + this.save = function () { localStorage.user = JSON.stringify({ username: this.username, name: this.name, token: this.token, }); - } + }; }