Ajout d'un composant pour permettre la sélection d'un lien unique
authorThierry Florac <thierry.florac@onf.fr>
Fri, 20 Jan 2017 15:36:00 +0100
changeset 64 2986440f9797
parent 63 2e8d9336f0ac
child 65 e4e1df663072
Ajout d'un composant pour permettre la sélection d'un lien unique
src/pyams_content/component/links/zmi/templates/widget-display.pt
src/pyams_content/component/links/zmi/templates/widget-input.pt
src/pyams_content/component/links/zmi/templates/widget-list-display.pt
src/pyams_content/component/links/zmi/templates/widget-list-input.pt
src/pyams_content/component/links/zmi/widget.py
--- a/src/pyams_content/component/links/zmi/templates/widget-display.pt	Tue Dec 20 18:56:24 2016 +0100
+++ b/src/pyams_content/component/links/zmi/templates/widget-display.pt	Fri Jan 20 15:36:00 2017 +0100
@@ -1,5 +1,4 @@
 <input type="hidden" autocomplete="off" readonly
-	data-ams-select2-multiple="true"
 	tal:attributes="id view/id;
 					name view/name;
 					class string:select2 ${view/klass} ordered;
--- a/src/pyams_content/component/links/zmi/templates/widget-input.pt	Tue Dec 20 18:56:24 2016 +0100
+++ b/src/pyams_content/component/links/zmi/templates/widget-input.pt	Fri Jan 20 15:36:00 2017 +0100
@@ -3,7 +3,7 @@
 	   data-ams-plugin-pyams_content-src="/--static--/pyams_content/js/pyams_content{MyAMS.devext}.js">
 	<div class="btn-group icon-append">
 		<i class="fa fa-fw fa-bars txt-color-green opaque" data-toggle="dropdown"
-			tal:attributes="data-ams-select2-target string:${view/name}:list"></i>
+			tal:attributes="data-ams-select2-target view/name"></i>
 		<ul class="dropdown-menu pull-right">
 			<li class="small">
 				<a data-ams-url="add-internal-link.html?origin=link"
@@ -29,10 +29,12 @@
 		</ul>
 	</div>
 	<div class="select2-parent">
-		<select class="select2 ordered"
+		<input type="hidden" class="select2 ordered"
+				data-ams-events-handlers='{"select2-open": "PyAMS_content.links.init"}'
 				data-ams-select2-allow-clear="true"
+				data-ams-select2-multiple="false"
 				tal:attributes="id view/id;
-								name string:${view/name}:list;
+								name view/name;
 								class string:${view/klass} select2 ordered;
 								style view/style;
 								title view/title;
@@ -52,16 +54,7 @@
 								onfocus view/onfocus;
 								onblur view/onblur;
 								onchange view/onchange;
-								multiple view/multiple;
-								size view/size">
-			<option tal:repeat="entry view/selectedItems"
-					tal:attributes="value entry/value;
-									selected python:entry['value'] in view.value;"
-					tal:content="entry/content"></option>
-			<option tal:repeat="entry view/notselectedItems"
-					tal:attributes="value entry/value;
-									selected python:entry['value'] in view.value;"
-					tal:content="entry/content"></option>
-		</select>
+								value python:','.join(view.value);
+								data-ams-select2-data view/values_data;" />
 	</div>
 </label>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/links/zmi/templates/widget-list-display.pt	Fri Jan 20 15:36:00 2017 +0100
@@ -0,0 +1,22 @@
+<input type="hidden" autocomplete="off" readonly
+	data-ams-select2-multiple="true"
+	tal:attributes="id view/id;
+					name view/name;
+					class string:select2 ${view/klass} ordered;
+					style view/style;
+					title view/title;
+					value python:','.join(view.value);
+					lang view/lang;
+					onclick view/onclick;
+					ondblclick view/ondblclick;
+					onmousedown view/onmousedown;
+					onmouseup view/onmouseup;
+					onmouseover view/onmouseover;
+					onmousemove view/onmousemove;
+					onmouseout view/onmouseout;
+					onkeypress view/onkeypress;
+					onkeydown view/onkeydown;
+					onkeyup view/onkeyup;
+					disabled view/disabled;
+					tabindex view/tabindex;
+					data-ams-select2-values view/values_map;" />
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/links/zmi/templates/widget-list-input.pt	Fri Jan 20 15:36:00 2017 +0100
@@ -0,0 +1,67 @@
+<label class="input bordered with-icon" i18n:domain="pyams_content"
+	   data-ams-plugins="pyams_content"
+	   data-ams-plugin-pyams_content-src="/--static--/pyams_content/js/pyams_content{MyAMS.devext}.js">
+	<div class="btn-group icon-append">
+		<i class="fa fa-fw fa-bars txt-color-green opaque" data-toggle="dropdown"
+			tal:attributes="data-ams-select2-target string:${view/name}:list"></i>
+		<ul class="dropdown-menu pull-right">
+			<li class="small">
+				<a data-ams-url="add-internal-link.html?origin=link"
+				   data-ams-stop-propagation="true" data-toggle="modal">
+					<i class="fa fa-fw fa-link"></i>
+					<span i18n:translate="">Add internal link...</span>
+				</a>
+			</li>
+			<li class="small">
+				<a data-ams-url="add-external-link.html?origin=link"
+				   data-ams-stop-propagation="true" data-toggle="modal">
+					<i class="fa fa-fw fa-external-link"></i>
+					<span i18n:translate="">Add external link...</span>
+				</a>
+			</li>
+			<li class="small">
+				<a data-ams-url="add-mailto-link.html?origin=link"
+				   data-ams-stop-propagation="true" data-toggle="modal">
+					<i class="fa fa-fw fa-envelope-o"></i>
+					<span i18n:translate="">Add mailto link...</span>
+				</a>
+			</li>
+		</ul>
+	</div>
+	<div class="select2-parent">
+		<select class="select2 ordered"
+				data-ams-select2-allow-clear="true"
+				tal:attributes="id view/id;
+								name string:${view/name}:list;
+								class string:${view/klass} select2 ordered;
+								style view/style;
+								title view/title;
+								lang view/lang;
+								onclick view/onclick;
+								ondblclick view/ondblclick;
+								onmousedown view/onmousedown;
+								onmouseup view/onmouseup;
+								onmouseover view/onmouseover;
+								onmousemove view/onmousemove;
+								onmouseout view/onmouseout;
+								onkeypress view/onkeypress;
+								onkeydown view/onkeydown;
+								onkeyup view/onkeyup;
+								disabled view/disabled;
+								tabindex view/tabindex;
+								onfocus view/onfocus;
+								onblur view/onblur;
+								onchange view/onchange;
+								multiple view/multiple;
+								size view/size">
+			<option tal:repeat="entry view/selectedItems"
+					tal:attributes="value entry/value;
+									selected python:entry['value'] in view.value;"
+					tal:content="entry/content"></option>
+			<option tal:repeat="entry view/notselectedItems"
+					tal:attributes="value entry/value;
+									selected python:entry['value'] in view.value;"
+					tal:content="entry/content"></option>
+		</select>
+	</div>
+</label>
--- a/src/pyams_content/component/links/zmi/widget.py	Tue Dec 20 18:56:24 2016 +0100
+++ b/src/pyams_content/component/links/zmi/widget.py	Fri Jan 20 15:36:00 2017 +0100
@@ -27,8 +27,14 @@
 
 @widgettemplate_config(mode='input', template='templates/widget-input.pt', layer=IPyAMSLayer)
 @widgettemplate_config(mode='display', template='templates/widget-display.pt', layer=IPyAMSLayer)
-class LinkLinksSelectWidget(OrderedSelectWidget):
-    """Links links select widget"""
+class SingleLinkLinkSelectWidget(OrderedSelectWidget):
+    """Single Link link select widget"""
+
+    @property
+    def values_data(self):
+        result = sorted([{'id': entry['value'], 'text': entry['content']} for entry in self.items],
+                        key=lambda x: x['text'])
+        return json.dumps(result)
 
     @property
     def values_map(self):
@@ -37,6 +43,23 @@
         return json.dumps(result)
 
 
-def LinkLinkSelectFieldWidget(field, request):
+def SingleLinkLinkSelectFieldWidget(field, request):
+    """Single link link select widget factory"""
+    return FieldWidget(field, SingleLinkLinkSelectWidget(request))
+
+
+@widgettemplate_config(mode='input', template='templates/widget-list-input.pt', layer=IPyAMSLayer)
+@widgettemplate_config(mode='display', template='templates/widget-list-display.pt', layer=IPyAMSLayer)
+class LinkLinksSelectWidget(OrderedSelectWidget):
+    """Multiple inks links select widget"""
+
+    @property
+    def values_map(self):
+        result = {}
+        [result.update({entry['value']: entry['content']}) for entry in self.selectedItems]
+        return json.dumps(result)
+
+
+def LinkLinksSelectFieldWidget(field, request):
     """Links links select widget factory"""
     return FieldWidget(field, LinkLinksSelectWidget(request))