src/pyams_content/skin/resources/js/pyams_content.js
changeset 19 25e455568b00
parent 0 7c0001cacf8e
child 26 7f025cebf19d
--- a/src/pyams_content/skin/resources/js/pyams_content.js	Mon Jan 18 17:44:37 2016 +0100
+++ b/src/pyams_content/skin/resources/js/pyams_content.js	Mon Jan 18 17:45:47 2016 +0100
@@ -1,6 +1,10 @@
-(function($) {
+(function($, globals) {
+
+	"use strict";
 
-	PyAMS_content = {
+	var MyAMS = globals.MyAMS;
+
+	var PyAMS_content = {
 
 		/**
 		 * TinyMCE editor configuration
@@ -23,13 +27,36 @@
 		},
 
 		/**
+		 * User profile management
+		 */
+		profile: {
+
+			switchFavorite: function() {
+				var star = $(this);
+				var oid = star.data('sequence-oid');
+				MyAMS.ajax.post('switch-user-favorite.json',
+								{oid: oid},
+								function(result, status) {
+									if (result.favorite) {
+										star.removeClass('fa-star-o')
+											.addClass('fa-star');
+									} else {
+										star.removeClass('fa-star')
+											.addClass('fa-star-o');
+									}
+								});
+			}
+		},
+
+		/**
 		 * External files management
 		 */
 		extfiles: {
 
 			refresh: function(options) {
-				if (typeof(options) == 'string')
+				if (typeof(options) === 'string') {
 					options = JSON.parse(options);
+				}
 				var select = $('select[name="form.widgets.files:list"]');
 				var plugin = select.data('select2');
 				$('<option></option>').attr('value', options.new_file.id)
@@ -41,6 +68,16 @@
 				select.select2('data', data);
 				plugin.results.empty();
 				plugin.opts.populateResults.call(plugin, plugin.results, options.files, {term: ''});
+			},
+
+			refreshContainer: function(changes) {
+				var para = $('tr[data-ams-element-name="' + changes.object_name + '"]');
+				var counter = $('span.count', $('td.extfiles', para));
+				if (changes.nb_files > 0) {
+					counter.text('(' + changes.nb_files + ')');
+				} else {
+					counter.text('');
+				}
 			}
 		},
 
@@ -51,8 +88,9 @@
 		links: {
 
 			refresh: function(options) {
-				if (typeof(options) == 'string')
+				if (typeof(options) === 'string') {
 					options = JSON.parse(options);
+				}
 				var select = $('select[name="form.widgets.links:list"]');
 				var plugin = select.data('select2');
 				$('<option></option>').attr('value', options.new_link.id)
@@ -64,6 +102,16 @@
 				select.select2('data', data);
 				plugin.results.empty();
 				plugin.opts.populateResults.call(plugin, plugin.results, options.links, {term: ''});
+			},
+
+			refreshContainer: function(changes) {
+				var para = $('tr[data-ams-element-name="' + changes.object_name + '"]');
+				var counter = $('span.count', $('td.links', para));
+				if (changes.nb_links > 0) {
+					counter.text('(' + changes.nb_links + ')');
+				} else {
+					counter.text('');
+				}
 			}
 		},
 
@@ -74,8 +122,9 @@
 		galleries: {
 
 			refresh: function(options) {
-				if (typeof(options) == 'string')
+				if (typeof(options) === 'string') {
 					options = JSON.parse(options);
+				}
 				var select = $('select[name="form.widgets.galleries:list"]');
 				var plugin = select.data('select2');
 				$('<option></option>').attr('value', options.new_gallery.id)
@@ -90,8 +139,9 @@
 			},
 
 			setOrder: function(event, ui) {
-				if (ui && ui.item.hasClass('already-dropped'))
+				if (ui && ui.item.hasClass('already-dropped')) {
 					return;
+				}
 				var gallery = ui.item.parents('.gallery');
 				var ids = $('.image', gallery).listattr('data-ams-element-name');
 				MyAMS.ajax.post(gallery.data('ams-location') + '/set-images-order.json',
@@ -106,16 +156,135 @@
 						content: '<i class="text-danger fa fa-2x fa-bell shake animated"></i>&nbsp; ' + MyAMS.i18n.DELETE_WARNING,
 						buttons: MyAMS.i18n.BTN_OK_CANCEL
 					}, function(button) {
-						if (button == MyAMS.i18n.BTN_OK) {
+						if (button === MyAMS.i18n.BTN_OK) {
 							var gallery = link.parents('.gallery');
 							var location = gallery.data('ams-location');
 							var image = link.parents('.image');
 							var object_name = image.data('ams-element-name');
-							MyAMS.ajax.post(location + '/delete-element.json', {'object_name': object_name}, function(result, status) {
-								image.remove();
-							});
+							MyAMS.ajax.post(location + '/delete-element.json',
+											{object_name: object_name},
+											function(result, status) {
+												image.remove();
+											});
 						}
 					});
+				};
+			},
+
+			refreshContainer: function(changes) {
+				var para = $('tr[data-ams-element-name="' + changes.object_name + '"]');
+				var counter = $('span.count', $('td.galleries', para));
+				if (changes.nb_galleries > 0) {
+					counter.text('(' + changes.nb_galleries + ')');
+				} else {
+					counter.text('');
+				}
+			}
+		},
+
+
+		/**
+		 * Paragraphs management
+		 */
+		paragraphs: {
+
+			switchVisibility: function(element) {
+				return function() {
+					var source = $(this);
+					var para = source.parents('tr');
+					var container = para.parents('table');
+					MyAMS.ajax.post(container.data('ams-location') + '/set-paragraph-visibility.json',
+									{object_name: para.data('ams-element-name')},
+									function(result, status) {
+										if (result.visible) {
+											$('i', source).attr('class', 'fa fa-fw fa-eye');
+										} else {
+											$('i', source).attr('class', 'fa fa-fw fa-eye-slash text-danger');
+										}
+									});
+				};
+			},
+
+			refreshParagraph: function(changes) {
+				var container = $('table[id="paragraphs_list"]');
+				var para = $('tr[data-ams-element-name="' + changes.object_name + '"]', container);
+				if (changes.visible) {
+					$('i', $('td.switcher', para)).removeClass('fa-eye-slash text-danger')
+												  .addClass('fa-eye');
+				} else {
+					$('i', $('td.switcher', para)).removeClass('fa-eye')
+												  .addClass('fa-eye-slash text-danger');
+				}
+				$('span.title', para).text(changes.title || '--');
+			},
+
+			switchEditor: function(element) {
+				var source = $(this);
+				var switcher = $('i', source);
+				var td = source.parents('td');
+				var editor = $('.editor', td);
+				var para = source.parents('tr');
+				if (switcher.hasClass('fa-plus-square-o')) {
+					var container = para.parents('table');
+					editor.html('<h1 class="loading"><i class="fa fa-2x fa-gear fa-spin"></i></h1>');
+					MyAMS.ajax.post(container.data('ams-location') + '/get-paragraph-editor.json',
+									{object_name: para.data('ams-element-name')},
+									function(result) {
+										editor.html(result);
+										if (result) {
+											MyAMS.initContent(editor);
+											switcher.removeClass('fa-plus-square-o')
+													.addClass('fa-minus-square-o');
+											para.data('ams-disabled-handlers', true);
+										}
+									});
+				} else {
+					MyAMS.skin.cleanContainer(editor);
+					editor.empty();
+					switcher.removeClass('fa-minus-square-o')
+							.addClass('fa-plus-square-o');
+					para.removeData('ams-disabled-handlers');
+				}
+			},
+
+			switchAllEditors: function(element) {
+				var source = $(this);
+				var switcher = $('i', source);
+				var container = source.parents('table');
+				// TODO: afficher le curseur de la roue qui tourne !!
+				if (switcher.hasClass('fa-plus-square-o')) {
+					switcher.removeClass('fa-plus-square-o')
+							.addClass('fa-cog fa-spin');
+					MyAMS.ajax.post(container.data('ams-location') + '/get-paragraphs-editors.json',
+									{},
+									function(result) {
+										for (var para_id in result) {
+											if (!result.hasOwnProperty(para_id)) {
+												continue;
+											}
+											var para = $('tr[data-ams-element-name="' + para_id + '"]', container);
+											var editor = $('.editor', para);
+											if (editor.is(':empty')) {
+												editor.html(result[para_id]);
+												MyAMS.initContent(editor);
+											}
+											$('.fa-plus-square-o', para).removeClass('fa-plus-square-o')
+																		.addClass('fa-minus-square-o');
+											para.data('ams-disabled-handlers', true);
+										}
+										if (!$('i.fa-plus-square-o', $('tbody', container)).exists()) {
+											switcher.removeClass('fa-cog fa-spin')
+													.addClass('fa-minus-square-o');
+										}
+									});
+				} else {
+					$('.editor', container).each(function() {
+						MyAMS.skin.cleanContainer($(this));
+						$(this).empty();
+					});
+					$('.fa-minus-square-o', container).removeClass('fa-minus-square-o')
+													  .addClass('fa-plus-square-o');
+					$('tr', container).removeData('ams-disabled-handlers');
 				}
 			}
 		},
@@ -132,15 +301,18 @@
 				var extract = $('select[name="form.widgets.extract_name:list"]', element);
 				var extract_name = extract.val();
 				if (thesaurus_name) {
-					MyAMS.jsonrpc.post('getExtracts', {thesaurus_name: thesaurus_name}, {url: '/api/thesaurus/json'}, function(data) {
-						extract.empty();
-						$(data.result).each(function() {
-							$('<option></option>').attr('value', this.id)
-												  .attr('selected', this.id == extract_name)
-												  .text(this.text)
-												  .appendTo(extract);
-						});
-					});
+					MyAMS.jsonrpc.post('getExtracts',
+									   {thesaurus_name: thesaurus_name},
+									   {url: '/api/thesaurus/json'},
+									   function(data) {
+											extract.empty();
+											$(data.result).each(function() {
+												$('<option></option>').attr('value', this.id)
+																	  .attr('selected', this.id === extract_name)
+																	  .text(this.text)
+																	  .appendTo(extract);
+											});
+									   });
 				}
 				extract.attr('data-ams-events-handlers', '{"select2-open": "PyAMS_content.themes.getExtracts"}');
 			},
@@ -150,15 +322,37 @@
 				var form = select.parents('form');
 				var thesaurus_name = $('select[name="form.widgets.thesaurus_name:list"]', form).val();
 				if (thesaurus_name) {
-					MyAMS.jsonrpc.post('getExtracts', {thesaurus_name: thesaurus_name}, {url: '/api/thesaurus/json'}, function(data) {
-						var extract = $('select[name="form.widgets.extract_name:list"]', form);
-						var plugin = extract.data('select2');
-						plugin.results.empty();
-						plugin.opts.populateResults.call(plugin, plugin.results, data.result, {term: ''});
-					});
+					MyAMS.jsonrpc.post('getExtracts',
+									   {thesaurus_name: thesaurus_name},
+									   {url: '/api/thesaurus/json'},
+									   function(data) {
+											var extract = $('select[name="form.widgets.extract_name:list"]', form);
+											var plugin = extract.data('select2');
+											plugin.results.empty();
+											plugin.opts.populateResults.call(plugin, plugin.results, data.result,
+																			 {term: ''});
+									   });
 				}
 			}
 		}
-	}
+	};
+
+	globals.PyAMS_content = PyAMS_content;
 
-})(jQuery);
+	$(globals.document).on('PyAMS_content.changed_item', function(event, settings) {
+		switch (settings.object_type) {
+			case 'paragraph':
+				PyAMS_content.paragraphs.refreshParagraph(settings);
+				break;
+			case 'extfiles_container':
+				PyAMS_content.extfiles.refreshContainer(settings);
+				break;
+			case 'links_container':
+				PyAMS_content.links.refreshContainer(settings);
+				break;
+			case 'galleries_container':
+				PyAMS_content.galleries.refreshContainer(settings);
+				break;		}
+	});
+
+})(jQuery, this);