Sauvegarde locale des modifications

This commit is contained in:
Evarin 2018-03-20 01:19:45 +01:00
parent 0588464bac
commit 3d659fab3c
4 changed files with 548 additions and 155 deletions

View file

@ -0,0 +1,324 @@
/**
* selectize.css (v0.12.4)
* Copyright (c) 20132015 Brian Reavis & contributors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at:
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
* ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*
* @author Brian Reavis <brian@thirdroute.com>
*/
.selectize-control.plugin-drag_drop.multi > .selectize-input > div.ui-sortable-placeholder {
visibility: visible !important;
background: #f2f2f2 !important;
background: rgba(0, 0, 0, 0.06) !important;
border: 0 none !important;
-webkit-box-shadow: inset 0 0 12px 4px #ffffff;
box-shadow: inset 0 0 12px 4px #ffffff;
}
.selectize-control.plugin-drag_drop .ui-sortable-placeholder::after {
content: '!';
visibility: hidden;
}
.selectize-control.plugin-drag_drop .ui-sortable-helper {
-webkit-box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
}
.selectize-dropdown-header {
position: relative;
padding: 5px 8px;
border-bottom: 1px solid #d0d0d0;
background: #f8f8f8;
-webkit-border-radius: 3px 3px 0 0;
-moz-border-radius: 3px 3px 0 0;
border-radius: 3px 3px 0 0;
}
.selectize-dropdown-header-close {
position: absolute;
right: 8px;
top: 50%;
color: #303030;
opacity: 0.4;
margin-top: -12px;
line-height: 20px;
font-size: 20px !important;
}
.selectize-dropdown-header-close:hover {
color: #000000;
}
.selectize-dropdown.plugin-optgroup_columns .optgroup {
border-right: 1px solid #f2f2f2;
border-top: 0 none;
float: left;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.selectize-dropdown.plugin-optgroup_columns .optgroup:last-child {
border-right: 0 none;
}
.selectize-dropdown.plugin-optgroup_columns .optgroup:before {
display: none;
}
.selectize-dropdown.plugin-optgroup_columns .optgroup-header {
border-top: 0 none;
}
.selectize-control.plugin-remove_button [data-value] {
position: relative;
padding-right: 24px !important;
}
.selectize-control.plugin-remove_button [data-value] .remove {
z-index: 1;
/* fixes ie bug (see #392) */
position: absolute;
top: 0;
right: 0;
bottom: 0;
width: 17px;
text-align: center;
font-weight: bold;
font-size: 12px;
color: inherit;
text-decoration: none;
vertical-align: middle;
display: inline-block;
padding: 2px 0 0 0;
border-left: 1px solid #d0d0d0;
-webkit-border-radius: 0 2px 2px 0;
-moz-border-radius: 0 2px 2px 0;
border-radius: 0 2px 2px 0;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
.selectize-control.plugin-remove_button [data-value] .remove:hover {
background: rgba(0, 0, 0, 0.05);
}
.selectize-control.plugin-remove_button [data-value].active .remove {
border-left-color: #cacaca;
}
.selectize-control.plugin-remove_button .disabled [data-value] .remove:hover {
background: none;
}
.selectize-control.plugin-remove_button .disabled [data-value] .remove {
border-left-color: #ffffff;
}
.selectize-control.plugin-remove_button .remove-single {
position: absolute;
right: 28px;
top: 6px;
font-size: 23px;
}
.selectize-control {
position: relative;
}
.selectize-dropdown,
.selectize-input,
.selectize-input input {
color: #303030;
font-family: inherit;
font-size: 13px;
line-height: 18px;
-webkit-font-smoothing: inherit;
}
.selectize-input,
.selectize-control.single .selectize-input.input-active {
background: #ffffff;
cursor: text;
display: inline-block;
}
.selectize-input {
border: 1px solid #d0d0d0;
padding: 8px 8px;
display: inline-block;
width: 100%;
overflow: hidden;
position: relative;
z-index: 1;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.1);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.1);
-webkit-border-radius: 3px;
-moz-border-radius: 3px;
border-radius: 3px;
}
.selectize-control.multi .selectize-input.has-items {
padding: 6px 8px 3px;
}
.selectize-input.full {
background-color: #ffffff;
}
.selectize-input.disabled,
.selectize-input.disabled * {
cursor: default !important;
}
.selectize-input.focus {
-webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15);
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.15);
}
.selectize-input.dropdown-active {
-webkit-border-radius: 3px 3px 0 0;
-moz-border-radius: 3px 3px 0 0;
border-radius: 3px 3px 0 0;
}
.selectize-input > * {
vertical-align: baseline;
display: -moz-inline-stack;
display: inline-block;
zoom: 1;
*display: inline;
}
.selectize-control.multi .selectize-input > div {
cursor: pointer;
margin: 0 3px 3px 0;
padding: 2px 6px;
background: #f2f2f2;
color: #303030;
border: 0 solid #d0d0d0;
}
.selectize-control.multi .selectize-input > div.active {
background: #e8e8e8;
color: #303030;
border: 0 solid #cacaca;
}
.selectize-control.multi .selectize-input.disabled > div,
.selectize-control.multi .selectize-input.disabled > div.active {
color: #7d7d7d;
background: #ffffff;
border: 0 solid #ffffff;
}
.selectize-input > input {
display: inline-block !important;
padding: 0 !important;
min-height: 0 !important;
max-height: none !important;
max-width: 100% !important;
margin: 0 2px 0 0 !important;
text-indent: 0 !important;
border: 0 none !important;
background: none !important;
line-height: inherit !important;
-webkit-user-select: auto !important;
-webkit-box-shadow: none !important;
box-shadow: none !important;
}
.selectize-input > input::-ms-clear {
display: none;
}
.selectize-input > input:focus {
outline: none !important;
}
.selectize-input::after {
content: ' ';
display: block;
clear: left;
}
.selectize-input.dropdown-active::before {
content: ' ';
display: block;
position: absolute;
background: #f0f0f0;
height: 1px;
bottom: 0;
left: 0;
right: 0;
}
.selectize-dropdown {
position: absolute;
z-index: 10;
border: 1px solid #d0d0d0;
background: #ffffff;
margin: -1px 0 0 0;
border-top: 0 none;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
-webkit-border-radius: 0 0 3px 3px;
-moz-border-radius: 0 0 3px 3px;
border-radius: 0 0 3px 3px;
}
.selectize-dropdown [data-selectable] {
cursor: pointer;
overflow: hidden;
}
.selectize-dropdown [data-selectable] .highlight {
background: rgba(125, 168, 208, 0.2);
-webkit-border-radius: 1px;
-moz-border-radius: 1px;
border-radius: 1px;
}
.selectize-dropdown [data-selectable],
.selectize-dropdown .optgroup-header {
padding: 5px 8px;
}
.selectize-dropdown .optgroup:first-child .optgroup-header {
border-top: 0 none;
}
.selectize-dropdown .optgroup-header {
color: #303030;
background: #ffffff;
cursor: default;
}
.selectize-dropdown .active {
background-color: #f5fafd;
color: #495c68;
}
.selectize-dropdown .active.create {
color: #495c68;
}
.selectize-dropdown .create {
color: rgba(48, 48, 48, 0.5);
}
.selectize-dropdown-content {
overflow-y: auto;
overflow-x: hidden;
max-height: 200px;
-webkit-overflow-scrolling: touch;
}
.selectize-control.single .selectize-input,
.selectize-control.single .selectize-input input {
cursor: pointer;
}
.selectize-control.single .selectize-input.input-active,
.selectize-control.single .selectize-input.input-active input {
cursor: text;
}
.selectize-control.single .selectize-input:after {
content: ' ';
display: block;
position: absolute;
top: 50%;
right: 15px;
margin-top: -3px;
width: 0;
height: 0;
border-style: solid;
border-width: 5px 5px 0 5px;
border-color: #808080 transparent transparent transparent;
}
.selectize-control.single .selectize-input.dropdown-active:after {
margin-top: -4px;
border-width: 0 5px 5px 5px;
border-color: transparent transparent #808080 transparent;
}
.selectize-control.rtl.single .selectize-input:after {
left: 15px;
right: auto;
}
.selectize-control.rtl .selectize-input > input {
margin: 0 4px 0 -2px !important;
}
.selectize-control .selectize-input.disabled {
opacity: 0.5;
background-color: #fafafa;
}

View file

@ -0,0 +1,213 @@
function initEditStage(STATIC_URL, API_URL) {
var has_changes = true;
var stage_object_id = $("#stage_object_id").val();
$(window).on('beforeunload',
function () {
localSave();
/*if (has_changes)
return 'Vous n\'avez pas enregistré vos changements !';
else return undefined;*/
});
// DATE FIELDS
$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });
// RICH TEXT FIELDS
var rich_texts = [];
function initRichTexts(selector) {
var txtr = $(selector+" textarea.tinymce");
$.each(txtr, function(i, item) {
var newitem = $("<div>", {"class":"tinymce"}).html(item.value)
item.fakeinput = newitem;
item.style.display = "none";
$(item).parent().append(newitem);
rich_texts.push(item);
});
tinyMCE.init({
selector: selector+" div.tinymce",
menubar: false,
plugins: "paste,searchreplace,lists,autolink,link",
toolbar: 'undo redo | bold italic underline strikethrough | bullist numlist outdent indent | link',
inline: true,
language: "fr_FR",
browser_spellcheck: true
});
};
// SELECT MULTIPLE FIELDS
var slts = $("select[multiple]").selectize();
// CHOIX DU LIEU
var lieu_select = new SelectLieuWidget(STATIC_URL, API_URL,
$("#lieu_widget"), lieuChoisi);
var avis_lieu_template = $("#avis_lieu_vide").remove().html();
var lieux_liste = $("#lieux-selector");
var lieu_focus;
lieux_liste.find(".lieu-change")
.on("click", clickLieu);
// Modification/ajout d'un lieu
function clickLieu() {
var form_id = undefined;
var lieu_id = undefined;
if(this.id != "lieu-ajout") {
form_id = this.id.split("-");
form_id = form_id[form_id.length-1];
var dest_form = $("#avis-lieux-"+form_id);
lieu_id = dest_form.find("input.lieu-hidden").val();
}
lieu_focus = form_id;
lieu_select.showWidget(lieu_id);
}
// Callback choix du lieu terminé
function lieuChoisi(lieu) {
var dest_form, dest_btn;
console.log(lieu);
console.log(lieu_focus);
if(lieu_focus === undefined) {
// Élimination des doublons
var candidates = $("input.lieu-hidden");
for (var i=0; i<candidates.length; i++) {
if($(candidates[i]).val() == lieu.id) {
lieu_select.closeWidget();
return;
}
}
// Créer un nouveau lieu
var cnt = $("#id_lieux-TOTAL_FORMS");
var i_form = cnt.val()*1;
cnt.val(i_form+1);
var dest_tmp = addAvisLieuForm(i_form);
dest_form = dest_tmp[0];
dest_btn = dest_tmp[1];
} else {
// Changer un lieu existant
dest_form = $("#avis-lieux-"+lieu_focus);
dest_btn = lieux_liste.find("#change-lieux-"+lieu_focus);
}
dest_form.find(".lieu-nom")
.text(lieu.nom+" ("+lieu.ville+")");
dest_btn.text(lieu.nom+" ("+lieu.ville+")");
dest_form.find("input.lieu-hidden")
.val(lieu.id);
lieu_select.closeWidget();
}
// Formulaire vide d'avis d'un lieu
function addAvisLieuForm(i_form) {
var idform = "avis-lieux-"+i_form;
// Formulaire vide
var dest_form = $("<div>", {id: idform, class:"avis_lieu"})
.html(avis_lieu_template.replace(/__prefix__/g, i_form));
// Bouton de modification du lieu
var dest_btn = $("<a>", {href:"javascript:void(0);",
class:"lieu-change",
id:"change-lieux-"+i_form})
.on("click", clickLieu);
// Ajout au DOM et listeners
lieux_liste.children().last().before(dest_btn);
$("#avis_lieu_container").append(dest_form);
initRichTexts("#"+idform);
return [dest_form, dest_btn];
}
// SAUVEGARDE LOCALE EN CAS DE PÉPIN
function localSave () {
cleanForm();
var formvals = {};
var els = $("#stageform").get(0).elements;
$.each(els, function(i, el) {
if (!el.name) return;
var elprefix = el.name.slice(0,5);
if (elprefix !== "stage" && elprefix !== "lieux" && elprefix !== "avis-") return;
if (formvals[el.name] === undefined) formvals[el.name] = [];
formvals[el.name].push(el.value);
});
var extvals = {};
for(var i=0; i<formvals["lieux-TOTAL_FORMS"]; i++) {
extvals["lieux-n"+i] = $("#change-lieux-"+i).text();
}
extvals.date = new Date().getTime();
var locsave = {"form": formvals, "misc": extvals};
window.localStorage.setItem("stage_save_"+stage_object_id, JSON.stringify(locsave));
console.log(formvals);
}
function restoreSave () {
var locsave = window.localStorage.getItem("stage_save_"+stage_object_id);
if (locsave === null) return;
locsave = JSON.parse(locsave);
var formvals = locsave.form;
var misc = locsave.misc;
var nlieux = $(".avis_lieu").length;
for (var i=0; i<formvals["lieux-TOTAL_FORMS"]; i++) {
if (i>=nlieux) addAvisLieuForm(i);
var lieunom = misc["lieux-n"+i];
$("#avis-lieux-"+i+" .lieu-nom").text(lieunom);
$("#change-lieux-"+i).text(lieunom);
}
var els = $("#stageform").get(0).elements;
$.each(els, function(i, el) {
if(formvals[el.name] === undefined) return;
var vals = formvals[el.name];
if(el.name === "stage-matieres") {
el.selectize.setValue(vals, true);
} else {
el.value = vals[0];
}
});
// Mise à jour des tinyMCE en asynchrone (sinon erreur)
setTimeout(function(){
$.each(rich_texts, function(i, item) {
tinyMCE.get(item.fakeinput.attr("id")).setContent(item.value);
});
}, 1000);
}
function initSave() {
var locsave = window.localStorage.getItem("stage_save_"+stage_object_id);
if (locsave === null) return;
locsave = JSON.parse(locsave);
var last_maj = $("#date_maj").val();
console.log(last_maj, locsave.misc.date/1000);
if (locsave.misc.date/1000 > last_maj+60) {
var texte = "";
if(stage_object_id == "new")
texte = "Une sauvegarde automatique existe pour un brouillon de fiche de stage. Voulez-vous la récupérer (si l'enregistrement s'est mal passé ?)";
else
texte = "Une sauvegarde automatique plus récente que la dernière modification de cette fiche de stage existe, voulez-vous la récupérer ?";
if(confirm(texte)) {
restoreSave();
}
}
}
function cleanForm() {
$.each(rich_texts, function(i, item) {
item.value = tinyMCE.get(item.fakeinput.attr("id")).getContent();
});
}
initRichTexts("");
// CLEANUP ON SENDING
$("#stageform").submit(function() {
cleanForm();
localSave();
has_changes = false;
});
initSave();
setInterval(localSave, 20000);
}

3
avisstage/static/js/selectize.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View file

@ -14,169 +14,22 @@
<script type="text/javascript" src="{% static "js/select_lieu.js" %}"></script> <script type="text/javascript" src="{% static "js/select_lieu.js" %}"></script>
<link rel="stylesheet" type="text/css" href="{% static "css/MarkerCluster.css" %}" /> <link rel="stylesheet" type="text/css" href="{% static "css/MarkerCluster.css" %}" />
<link rel="stylesheet" type="text/css" href="{% static "css/MarkerCluster.Default.css" %}" /> <link rel="stylesheet" type="text/css" href="{% static "css/MarkerCluster.Default.css" %}" />
<script type="text/javascript" src="{% static "js/selectize.min.js" %}"></script>
<link rel="stylesheet" type="text/css" href="{% static "css/selectize.css" %}" />
<script type="text/javascript" src="{% static "js/editstage.js" %}"></script>
<script type="text/javascript"> <script type="text/javascript">
$(function(){ $(function(){
var has_changes = true; initEditStage(
$(window).on('beforeunload',
function () {
if (has_changes)
return 'Vous n\'avez pas enregistré vos changements !';
else return undefined;
});
// DATE FIELDS
$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });
// RICH TEXT FIELDS
var rich_texts = [];
function initRichTexts(selector) {
var txtr = $(selector+" textarea.tinymce");
$.each(txtr, function(i, item) {
var newitem = $("<div>", {"class":"tinymce"}).html(item.value)
item.fakeinput = newitem;
item.style.display = "none";
$(item).parent().append(newitem);
rich_texts.push(item);
});
tinyMCE.init({
selector: selector+" div.tinymce",
menubar: false,
plugins: "paste,searchreplace,lists,autolink,link",
toolbar: 'undo redo | bold italic underline strikethrough | bullist numlist outdent indent | link',
inline: true,
language: "fr_FR",
browser_spellcheck: true
});
};
// SELECT MULTIPLE FIELDS
var slts = $("select[multiple]");
var NULL_VAL = " ";
$.each(slts, function(i, item) {
var $item = $(item);
var $parent = $item.parent();
var $liste = $("<div>", {class: "msel-liste"});
item.msel_liste = $liste;
var template = $item.clone(false);
template.prepend($("<option>", {"value": NULL_VAL})
.text("Choisir..."));
template.removeAttr("multiple")
.removeAttr("required")
.attr("id", "")
.addClass("msel-choix");
var vl = $item.val();
if(vl.length == 0) vl = [NULL_VAL];
for(var i=0; i<vl.length; i++) {
$liste.append(template.clone(false).val(vl[i]));
}
$ajouter = $("<a>", {"href": "javascript:void(0);", "class":"msel-add"}).text("Ajouter");
function ajout() {
var prevv = $liste.find("select");
if($(prevv[prevv.length-1]).val() == NULL_VAL) return;
$ajouter.before(template.clone(false).val(NULL_VAL));
}
$ajouter.on("click", ajout);
$liste.append($ajouter);
$parent.append($liste);
$item.remove();
});
// CHOIX DU LIEU
var lieu_select = new SelectLieuWidget(
"{{ STATIC_URL|escapejs }}", "{{ STATIC_URL|escapejs }}",
"{% url 'avisstage:api_dispatch_list' resource_name="lieu" api_name="v1" %}", "{% url 'avisstage:api_dispatch_list' resource_name="lieu" api_name="v1" %}");
$("#lieu_widget"), lieuChoisi);
var avis_lieu_template = $("#avis_lieu_vide").remove().html();
var lieux_liste = $("#lieux-selector");
lieux_liste.find(".lieu-change")
.on("click", clickLieu);
var lieu_focus;
function clickLieu() {
var form_id = undefined;
var lieu_id = undefined;
if(this.id != "lieu-ajout") {
form_id = this.id.split("-");
form_id = form_id[form_id.length-1];
var dest_form = $("#avis-lieux-"+form_id);
lieu_id = dest_form.find("input.lieu-hidden").val();
}
lieu_focus = form_id;
lieu_select.showWidget(lieu_id);
}
function lieuChoisi(lieu) {
var dest_form, dest_btn;
console.log(lieu);
console.log(lieu_focus);
if(lieu_focus === undefined) {
// Élimination des doublons
var candidates = $("input.lieu-hidden");
for (var i=0; i<candidates.length; i++) {
if($(candidates[i]).val() == lieu.id) {
lieu_select.closeWidget();
return;
}
}
// Créer un nouveau lieu
var cnt = $("#id_lieux-TOTAL_FORMS");
var i_form = cnt.val()*1;
var idform = "avis-lieux-"+i_form;
dest_form = $("<div>", {id: idform, class:"avis_lieu"})
.html(avis_lieu_template.replace(/__prefix__/g, i_form));
dest_btn = $("<a>", {href:"javascript:void(0);",
class:"lieu-change",
id:"change-lieux-"+i_form})
.on("click", clickLieu);
lieux_liste.children().last().before(dest_btn);
cnt.val(i_form+1);
$("#avis_lieu_container").append(dest_form);
initRichTexts("#"+idform);
} else {
// Changer un lieu existant
dest_form = $("#avis-lieux-"+lieu_focus);
dest_btn = lieux_liste.find("#change-lieux-"+lieu_focus);
}
dest_form.find(".lieu-nom")
.text(lieu.nom+" ("+lieu.ville+")");
dest_btn.text(lieu.nom+" ("+lieu.ville+")");
dest_form.find("input.lieu-hidden")
.val(lieu.id);
lieu_select.closeWidget();
}
// TODO gérer le cas de l'actualisation du formulaire où le lieu affiché n'est plus le vrai lieu
initRichTexts("");
// CLEANUP ON SENDING
$("#stageform").submit(function() {
$.each(rich_texts, function(i, item) {
item.value = tinyMCE.get(item.fakeinput.attr("id")).getContent();
});
$.each(slts, function(i, item) {
$.each(item.msel_liste.find("select"), function(j, sel) {
var v = $(sel).val();
if(v == NULL_VAL)
$(sel).val(undefined);
});
});
has_changes = false;
});
}); });
</script> </script>
{% endblock %} {% endblock %}
{% block content %} {% block content %}
<h1>{% if creation %}Nouvelle expérience{% else %}Modification d'une expérience{% endif %}</h1> <h1>{% if creation %}Nouvelle expérience{% else %}Modification d'une expérience{% endif %}</h1>
<input type="hidden" id="stage_object_id" value="{% if creation %}new{% else %}{{ form.instance.id }}{% endif %}"/>
<input type="hidden" id="date_maj" value="{% if creation %}{% else %}{{ form.instance.date_maj|date:"U" }}{% endif %}"/>
<form action="" method="post" id="stageform"> <form action="" method="post" id="stageform">
{% csrf_token %} {% csrf_token %}
{# Général #} {# Général #}