[SPIP][PLUGINS] v3.0-->v3.2
[lhc/web/www.git] / www / plugins-dist / medias / javascript / gestion_listes_documents.js.html
1 #HTTP_HEADER{Content-Type: text/javascript; charset=#CHARSET}
2 [(#REM)<script>/*
3
4 Gestion des listes de documents :
5 - Choix des modes d'affichage (grand, case, liste courte)
6 - Gestion du tri par glisser-déposer
7 - Rechargement du conteneur des listes quand l'une d'elle est vidée
8
9 Markup :
10 - Conteneur principal : .portfolios
11 - Listes : .liste_items.documents
12 - Listes ordonnables : .liste_items.documents.ordonner_rang_lien\[data-lien\]
13
14 */]
15
16 /* Choix du mode affichage des documents (grand, en case, en liste courte) */
17 function choix_affichages_documents() {
18 $('.portfolios h3:not(:has(.affichages))').each(function () {
19 var titre = $(this);
20 var liste = titre.next('.liste_items.documents');
21 var identifiant = liste.data('cookie-affichage');
22
23 titre.append(
24 "<div class='affichages'>"
25 + "<span class='icone grand on' title='<:medias:affichage_documents_en_grand|attribut_html:>'></span>"
26 + "<span class='icone cases' title='<:medias:affichage_documents_en_cases|attribut_html:>'></span>"
27 + "<span class='icone liste' title='<:medias:affichage_documents_en_liste_compacte|attribut_html:>'></span>"
28 + "</div>"
29 );
30
31 var changer_affichage_documents = function (me, bouton, classe) {
32 $(me).parent().find('.icone').removeClass('on').end().end().addClass('on');
33 var liste = $(me).parents('h3').next('.liste_items.documents');
34 liste.removeClass('documents_cases').removeClass('documents_liste');
35 if (classe) {
36 liste.addClass(classe);
37 }
38 if (identifiant) {
39 Cookies.set('affichage-' + identifiant, bouton);
40 }
41
42 liste.trigger('affichage.documents.change', {
43 'liste': liste,
44 'icone': me,
45 'bouton': bouton,
46 'classe': classe,
47 'identifiant': identifiant
48 });
49
50 };
51
52 titre.find('.affichages > .grand').click(function () {
53 changer_affichage_documents(this, 'grand', null);
54 });
55
56 titre.find('.affichages > .cases').click(function () {
57 changer_affichage_documents(this, 'cases', 'documents_cases');
58 });
59
60 titre.find('.affichages > .liste').click(function () {
61 changer_affichage_documents(this, 'liste', 'documents_liste');
62 });
63
64 if (identifiant) {
65 var defaut = Cookies.get('affichage-' + identifiant);
66 if (defaut) {
67 titre.find('.affichages > .' + defaut).trigger('click');
68 }
69 liste.trigger('affichage.documents.charge', {
70 'liste': liste,
71 'identifiant': identifiant,
72 'defaut': defaut
73 });
74 }
75 });
76 }
77
78 /* Gestion du tri des listes de documents et de leur enregistrement */
79 function ordonner_listes_documents() {
80 if ($.fn.sortable) {
81 $(".liste_items.documents.ordonner_rang_lien[data-lien]").find('> .sortable').each(function () {
82 // détruire / recréer le sortable à chaque appel ajax
83 if ($(this).has('.ui-sortable').length) {
84 $(this).sortable('destroy');
85 }
86 // pas de tri possible s'il n'y a qu'un seul élément.
87 if ($(this).find('> .item').length < 2) {
88 $(this).find('.deplacer-document').hide();
89 return true; // continue
90 } else {
91 $(this).find('.deplacer-document').show();
92 }
93 $(this).sortable({
94 /*axis: "y",*/ /* minidoc a un affichage en case */
95 handle: "",
96 placeholder: "ui-state-highlight deplacer-document-placeholder",
97 cancel: 'img.croix_centre_image',
98 start: function(event, ui) {
99 ui.item.addClass('document-en-mouvement');
100 },
101 stop: function(event, ui) {
102 ui.item.removeClass('document-en-mouvement');
103 },
104 update: function (event, ui) {
105 var items = $(this);
106 var item = ui.item;
107 var liste = items.sortable('toArray');
108 var ordre = [];
109
110 $.each(liste, function(i, id) {
111 if (id) {
112 ordre.push( id.substring(3) ); // doc123 => 123
113 }
114 });
115
116 // l'objet lié est indiqué dans l'attribut data-lien sur la liste
117 var lien = items.parents(".liste_items.documents").data("lien").split("/");
118 var objet_lie = lien[0];
119 var id_objet_lie = lien[1];
120 var action = '[(#VAL{ordonner_liens_documents}|generer_url_action{"", 1})]';
121 var params = {
122 objet_source: 'document',
123 objet_lie: objet_lie,
124 id_objet_lie: id_objet_lie,
125 ordre: ordre,
126 };
127
128 item.animateLoading();
129
130 $.ajax({
131 url: action,
132 data: params,
133 dataType: 'json',
134 cache: false,
135 }).done(function(data) {
136
137 var couleur_origine = item.css('background-color');
138 var couleur_erreur = $("<div class='remove'></div>").css('background-color');
139 var couleur_succes = $("<div class='append'></div>").css('background-color');
140 item.endLoading(true);
141
142 if (data.errors.length) {
143 items.sortable('cancel');
144 item.css({backgroundColor: couleur_erreur}).animate({backgroundColor: couleur_origine}, 'normal', function(){
145 item.css({backgroundColor: ''});
146 });
147 } else {
148 item.css({backgroundColor: couleur_succes}).animate({backgroundColor: couleur_origine}, 'normal', function(){
149 item.css({backgroundColor: ''});
150 });
151 items.parent().find('.tout_desordonner').show();
152 }
153 });
154 }
155 });
156 // bouton "désordonner"
157 if ($(this).parent().find('.deplacer-document[data-rang!=0]').length) {
158 $(this).parent().find('.tout_desordonner').show();
159 } else {
160 $(this).parent().find('.tout_desordonner').hide();
161 }
162 });
163 }
164 }
165
166 /* Recharger le conteneur des listes de documents si l'une d'elle est vide */
167 function check_reload_page(){
168 var reload = false;
169 $('.portfolios').each(function(){
170 $(this).find('.liste-items.documents').each(function() {
171 if ($(this).length && !$(this).find('.item').length) {
172 $(this).parents('.portfolios').ajaxReload();
173 reload = true;
174 return false; // break each
175 }
176 });
177 });
178 if (reload) {
179 jQuery('#navigation .box.info').ajaxReload();
180 }
181 }
182
183 /* Initialisation et relance en cas de chargement ajax */
184 if (window.jQuery) {
185 jQuery(function($){
186 if (!$.js_portfolio_documents_charge) {
187 $.js_portfolio_documents_charge = true;
188 onAjaxLoad(check_reload_page);
189 choix_affichages_documents();
190 onAjaxLoad(choix_affichages_documents);
191 ordonner_listes_documents();
192 onAjaxLoad(ordonner_listes_documents);
193 }
194 });
195 }