src/pyams_content/skin/resources/js/pyams_content.js
changeset 141 643417150ee3
parent 70 ba26f4023bc2
child 171 13db231ca918
--- a/src/pyams_content/skin/resources/js/pyams_content.js	Mon Sep 11 14:54:30 2017 +0200
+++ b/src/pyams_content/skin/resources/js/pyams_content.js	Mon Sep 11 15:10:50 2017 +0200
@@ -18,11 +18,21 @@
 			},
 
 			getImagesList: function(callback) {
-				return MyAMS.ajax.post('get-images-list.json', {}, callback);
+				var form = $(document.activeElement).parents('form');
+				if (form.exists()) {
+					var base = form.attr('data-ams-form-handler') || form.attr('action');
+					var target = base.substr(0, base.lastIndexOf('/') + 1);
+					return MyAMS.ajax.post(target + 'get-images-list.json', {}, callback);
+				}
 			},
 
 			getLinksList: function(callback) {
-				return MyAMS.ajax.post('get-links-list.json', {}, callback);
+				var form = $(document.activeElement).parents('form');
+				if (form.exists()) {
+					var base = form.attr('data-ams-form-handler') || form.attr('action');
+					var target = base.substr(0, base.lastIndexOf('/')+1);
+					return MyAMS.ajax.post(target + 'get-links-list.json', {}, callback);
+				}
 			}
 		},
 
@@ -49,103 +59,27 @@
 		},
 
 		/**
-		 * External files management
-		 */
-		extfiles: {
-
-			refresh: function(options) {
-				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)
-									  .attr('selected', 'selected')
-									  .text(options.new_file.text)
-									  .appendTo(select);
-				var data = select.select2('data');
-				data.push(options.new_file);
-				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', $('div.action.extfiles', para));
-				if (changes.nb_files > 0) {
-					counter.text('(' + changes.nb_files + ')');
-				} else {
-					counter.text('');
-				}
-			}
-		},
-
-
-		/**
-		 * Links management
-		 */
-		links: {
-
-			init: function() {
-
-				function initElements(data, status) {
-					var plugin = select.data('select2');
-					plugin.results.empty();
-					plugin.opts.populateResults.call(plugin, plugin.results, data, {term: ''});
-				}
-
-				var select = $(this);
-				MyAMS.ajax.post('get-links.json', {
-					keyFieldName: 'id',
-					valueFieldName: 'text'
-				}, initElements);
-			},
-
-			refresh: function(options) {
-				if (typeof(options) === 'string') {
-					options = JSON.parse(options);
-				}
-				var select = $('input[name="form.widgets.link"], select[name="form.widgets.links:list"]');
-				var plugin = select.data('select2');
-				if (select.attr('type') === 'select') {
-					$('<option></option>').attr('value', options.new_link.id)
-										  .attr('selected', 'selected')
-										  .text(options.new_link.text)
-										  .appendTo(select);
-				}
-				var data;
-				if (select.prop('multiple')) {
-					data = select.select2('data');
-					data.push(options.new_link);
-					select.select2('data', data);
-					plugin.results.empty();
-					plugin.opts.populateResults.call(plugin, plugin.results, options.links, {term: ''});
-				} else {
-					select.select2('data', [options.new_link]);
-					plugin.results.empty();
-					plugin.opts.populateResults.call(plugin, plugin.results, options.links, {term: ''});
-					select.val(options.new_link.id);
-				}
-			},
-
-			refreshContainer: function(changes) {
-				var para = $('tr[data-ams-element-name="' + changes.object_name + '"]');
-				var counter = $('span.count', $('div.action.links', para));
-				if (changes.nb_links > 0) {
-					counter.text('(' + changes.nb_links + ')');
-				} else {
-					counter.text('');
-				}
-			}
-		},
-
-
-		/**
 		 * Galleries management
 		 */
 		galleries: {
 
+			switchImageVisibility: function(element) {
+				return function() {
+					var source = $(this);
+					var image = source.parents('.image');
+					var gallery = image.parents('.gallery');
+					MyAMS.ajax.post(gallery.data('ams-location') + '/set-image-visibility.json',
+									{object_name: image.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');
+										}
+									});
+				};
+			},
+
 			refresh: function(options) {
 				if (typeof(options) === 'string') {
 					options = JSON.parse(options);
@@ -264,14 +198,7 @@
 			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 || '--');
+				$('span.title', para).html(changes.title || ' - - - - - - - -');
 			},
 
 			switchEditor: function(element) {
@@ -346,6 +273,57 @@
 
 
 		/**
+		 * Associations management
+		 */
+		associations: {
+
+			afterUpdateCallback: function(options) {
+				// Reload widget
+				var widget = $('#' + options.parent).parent('.ams-widget');
+				widget.replaceWith($(options.table));
+				widget = $('#' + options.parent).parent('.ams-widget');
+				MyAMS.initContent(widget);
+				// Check fieldset state
+				var legend = widget.siblings('legend');
+				if (legend.parents('fieldset:first').hasClass('switched')) {
+					legend.click();
+				}
+			},
+
+			switchVisibility: function (element) {
+				return function () {
+					var source = $(this);
+					var association = source.parents('tr');
+					var container = association.parents('table');
+					MyAMS.ajax.post(container.data('ams-location') + '/set-association-visibility.json',
+						{object_name: association.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');
+							}
+						});
+				};
+			}
+		},
+
+
+		/**
+		 * Illustration management
+		 */
+		illustration: {
+
+			afterUpdateCallback: function(options) {
+				var parent = $('#' + options.parent);
+				parent.replaceWith($(options.form));
+				parent = $('#' + options.parent);
+				MyAMS.initContent(parent);
+			}
+		},
+
+
+		/**
 		 * Themes management
 		 */
 		themes: {
@@ -475,15 +453,6 @@
 			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;
 			case 'review_comments':
 				PyAMS_content.review.updateComments();
 				break;