src/pyams_content/skin/resources/js/pyams_content.js
changeset 281 e5bfd8db9f3f
parent 276 78422a1c4228
child 302 6d86ba0d1ec3
--- a/src/pyams_content/skin/resources/js/pyams_content.js	Wed Nov 15 17:29:30 2017 +0100
+++ b/src/pyams_content/skin/resources/js/pyams_content.js	Sun Nov 26 09:49:57 2017 +0100
@@ -7,25 +7,25 @@
 	var PyAMS_content = {
 
 		/**
-		 * Replace given form with new content
+		 * Custom widgets management
 		 */
-		refreshForm: function(changes) {
-			var parent = $('#' + changes.object_name);
-			parent.replaceWith($(changes.form));
-			parent = $('#' + changes.object_name);
-			MyAMS.initContent(parent);
-			return parent;
+		widget: {
+
+			/** Treeview widget */
+			treeview: {
+
+				selectFolder: function(event, node) {
+					var target = $(event.target);
+					target.siblings('input[type="hidden"]').val(node.id);
+				},
+
+				unselectFolder: function(event, node) {
+					var target = $(event.target);
+					target.siblings('input[type="hidden"]').val(null);
+				}
+			}
 		},
 
-		refreshTable: function(changes) {
-			var widget = $('#' + changes.object_name).parent('.ams-widget');
-			widget.replaceWith($(changes.table));
-			widget = $('#' + changes.object_name).parent('.ams-widget');
-			MyAMS.initContent(widget);
-			return widget;
-		},
-
-
 		/**
 		 * TinyMCE editor configuration
 		 */
@@ -57,28 +57,6 @@
 		},
 
 		/**
-		 * 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');
-									}
-								});
-			}
-		},
-
-		/**
 		 * Galleries management
 		 */
 		galleries: {
@@ -147,38 +125,20 @@
 			}
 		},
 
-
 		/**
-		 * Image map management
+		 * Illustrations management
 		 */
-		imgmap: {
+		illustration: {
 
-			init: function() {
-				var image = $(this);
-				MyAMS.ajax.check($.fn.canvasAreaDraw,
-								 '/--static--/pyams_content/js/jquery-canvasAreaDraw' + MyAMS.devext + '.js',
-								 function() {
-									image.canvasAreaDraw({imageUrl: image.data('ams-image-url')});
-								 });
-			},
-
-			initSummary: function() {
-				var image = $(this);
-				MyAMS.ajax.check($.fn.mapster,
-								 '/--static--/pyams_content/js/jquery-imagemapster-1.2.10' + MyAMS.devext + '.js',
-								 function() {
-									image.mapster({
-										fillColor: 'ff0000',
-										fillOpacity: 0.35,
-										selected: true,
-										highlight: true,
-										staticState: true
-									});
-								 });
+			addIllustration: function() {
+				var source = $(this);
+				var group = source.parents('.btn-group');
+				var legend = group.siblings('legend.switcher');
+				$('i.fa-plus', legend).click();
+				source.hide();
 			}
 		},
 
-
 		/**
 		 * Associations management
 		 */
@@ -186,7 +146,7 @@
 
 			refreshAssociations: function(options) {
 				// Reload widget
-				var widget = PyAMS_content.refreshTable(options);
+				var widget = MyAMS.skin.refreshTable(options);
 				// Check fieldset state
 				var legend = widget.siblings('legend');
 				if (legend.parents('fieldset:first').hasClass('switched')) {
@@ -212,46 +172,6 @@
 			}
 		},
 
-
-		/**
-		 * Types management
-		 */
-		types: {
-
-			switchSubtypes: function(element) {
-				var source = $(this);
-				var switcher = $('i.switch', source);
-				var td = source.parents('td');
-				var subtypes = $('.subtypes', td);
-				var datatype = source.parents('tr');
-				if (switcher.hasClass('fa-plus-square-o')) {
-					var container = datatype.parents('table');
-					subtypes.html('<h1 class="loading"><i class="fa fa-2x fa-gear fa-spin"></i></h1>');
-					MyAMS.ajax.post(container.data('ams-location') + '/get-subtypes-table.json',
-									{object_name: datatype.data('ams-element-name')},
-									function(result) {
-										subtypes.html(result);
-										if (result) {
-											MyAMS.initContent(subtypes);
-											switcher.removeClass('fa-plus-square-o')
-													.addClass('fa-minus-square-o');
-										}
-									});
-				} else {
-					MyAMS.skin.cleanContainer(subtypes);
-					subtypes.empty();
-					switcher.removeClass('fa-minus-square-o')
-							.addClass('fa-plus-square-o');
-				}
-			},
-
-			refreshSubtypes: function(options) {
-				// Reload widget
-				PyAMS_content.refreshTable(options);
-			}
-		},
-
-
 		/**
 		 * Paragraphs management
 		 */
@@ -264,7 +184,7 @@
 
 			postReload: function() {
 				$(PyAMS_content.paragraphs.switched).each(function() {
-					$('i.switch.fa-plus-square-o', '[id="' + this + '"]').parents('div:first').click();
+					$('i.switch.fa-plus-square-o', '[id="' + this + '"]').parents('div').first().click();
 				});
 				delete PyAMS_content.paragraphs.switched;
 			},
@@ -288,7 +208,7 @@
 
 			refreshParagraphs: function(options) {
 				// Reload widget
-				var widget = PyAMS_content.refreshTable(options);
+				var widget = MyAMS.skin.refreshTable(options);
 				// Check fieldset state
 				var legend = widget.siblings('legend');
 				if (legend.parents('fieldset:first').hasClass('switched')) {
@@ -398,52 +318,6 @@
 			}
 		},
 
-
-		/**
-		 * Illustrations management
-		 */
-		illustration: {
-
-			addIllustration: function() {
-				var source = $(this);
-				var group = source.parents('.btn-group');
-				var legend = group.siblings('legend.switcher');
-				$('i.fa-plus', legend).click();
-				source.hide();
-			}
-		},
-
-
-		/**
-		 * Form fields management
-		 */
-		fields: {
-
-			refreshField: function(changes) {
-				var container = $('table[id="form_fields_list"]');
-				var para = $('tr[data-ams-element-name="' + changes.object_name + '"]', container);
-				$('td:nth-child(4)', para).html(changes.title);
-			},
-
-			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-form-field-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');
-							}
-						});
-				};
-			}
-		},
-
-
 		/**
 		 * Themes management
 		 */
@@ -490,6 +364,97 @@
 			}
 		},
 
+		/**
+		 * Form fields management
+		 */
+		fields: {
+
+			refreshField: function(changes) {
+				var container = $('table[id="form_fields_list"]');
+				var para = $('tr[data-ams-element-name="' + changes.object_name + '"]', container);
+				$('td:nth-child(4)', para).html(changes.title);
+			},
+
+			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-form-field-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');
+							}
+						});
+				};
+			}
+		},
+
+		/**
+		 * Image map management
+		 */
+		imgmap: {
+
+			init: function() {
+				var image = $(this);
+				MyAMS.ajax.check($.fn.canvasAreaDraw,
+								 '/--static--/pyams_content/js/jquery-canvasAreaDraw' + MyAMS.devext + '.js',
+								 function() {
+									image.canvasAreaDraw({imageUrl: image.data('ams-image-url')});
+								 });
+			},
+
+			initSummary: function() {
+				var image = $(this);
+				MyAMS.ajax.check($.fn.mapster,
+								 '/--static--/pyams_content/js/jquery-imagemapster-1.2.10' + MyAMS.devext + '.js',
+								 function() {
+									image.mapster({
+										fillColor: 'ff0000',
+										fillOpacity: 0.35,
+										selected: true,
+										highlight: true,
+										staticState: true
+									});
+								 });
+			}
+		},
+
+		/**
+		 * Types management
+		 */
+		types: {
+
+			switchSubtypes: function(element) {
+				var source = $(this);
+				var switcher = $('i.switch', source);
+				var td = source.parents('td');
+				var subtypes = $('.subtypes', td);
+				var datatype = source.parents('tr');
+				if (switcher.hasClass('fa-plus-square-o')) {
+					var container = datatype.parents('table');
+					subtypes.html('<h1 class="loading"><i class="fa fa-2x fa-gear fa-spin"></i></h1>');
+					MyAMS.ajax.post(container.data('ams-location') + '/get-subtypes-table.json',
+									{object_name: datatype.data('ams-element-name')},
+									function(result) {
+										subtypes.html(result);
+										if (result) {
+											MyAMS.initContent(subtypes);
+											switcher.removeClass('fa-plus-square-o')
+													.addClass('fa-minus-square-o');
+										}
+									});
+				} else {
+					MyAMS.skin.cleanContainer(subtypes);
+					subtypes.empty();
+					switcher.removeClass('fa-minus-square-o')
+							.addClass('fa-plus-square-o');
+				}
+			}
+		},
 
 		/**
 		 * Review comments management
@@ -570,19 +535,37 @@
 				chat.animate({scrollTop: chat[0].scrollHeight}, 1000);
 				$('.badge', 'nav a[href="#review-comments.html"]').text(options.count).removeClass('hidden');
 			}
+		},
+
+		/**
+		 * 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');
+									}
+								});
+			}
 		}
 	};
 
-	globals.PyAMS_content = PyAMS_content;
-
-	$(globals.document).on('PyAMS_content.changed_item', function(event, settings) {
-		MyAMS.executeFunctionByName(settings.handler, document, settings);
-	});
-
 	var badge = $('.badge', 'nav a[href="#review-comments.html"]');
 	if (badge.exists()) {
 		PyAMS_content.review.timer = setInterval(PyAMS_content.review.updateComments,
 												 PyAMS_content.review.timer_duration.general);
 	}
 
+	globals.PyAMS_content = PyAMS_content;
+
 })(jQuery, this);