Project

General

Profile

root / branches / sc50-v2.0 / sources2-0-011 / academic / model / Re / mediaWeb / mw.doss / _mediaWeb / mwFragment.xbl.xml @ 322

1
<?xml version="1.0" encoding="UTF-8"?>
2
<bindings xmlns="http://www.mozilla.org/xbl" xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" xmlns:xbl="http://www.mozilla.org/xbl" xmlns:html="http://www.w3.org/1999/xhtml">
3
	<binding id="generic" extends="chrome://scenarixedlib/content/libUiBase/widgets/xedUiBase.xbl.xml#boundedElement">
4
		<content orient="vertical" flex="1">
5
			<xul:toolbar class="fragToolbar">
6
				<xul:toolbarbutton anonid="btnPaste" class="btnPaste" label="Coller" oncommand="document.getBindingParent(this).pasteFrag();" tooltiptext="Coller le fragment web depuis le presse-papier"/>
7
				<xul:toolbarbutton anonid="btnCopy" class="btnCopy" label="Copier" oncommand="document.getBindingParent(this).copyFrag();" tooltiptext="Copier le fragment web dans le presse-papier" disabled="true"/>
8
				<xul:toolbarbutton anonid="btnDelete" class="btnDelete" label="Effacer" oncommand="document.getBindingParent(this).deleteFrag();" tooltiptext="Effacer le fragment web" disabled="true"/>
9
				<xul:toolbarbutton anonid="btnHelp" class="btnHelp" label="Aide" oncommand="document.getBindingParent(this).help();" tooltiptext="Aide"/>
10
			</xul:toolbar>
11
			<xul:vbox>
12
				<xul:iframe anonid="previewFrame" class="fragPreview" src="" flex="1" />
13
			</xul:vbox>
14
		</content>
15
		<implementation>
16
			<field name="fPreviewHeight">100</field>
17
			<field name="fPreviewFrame">document.getAnonymousElementByAttribute(this, "anonid", "previewFrame")</field>
18
			<field name="fBtnPaste">document.getAnonymousElementByAttribute(this, "anonid", "btnPaste")</field>
19
			<field name="fBtnCopy">document.getAnonymousElementByAttribute(this, "anonid", "btnCopy")</field>
20
			<field name="fBtnDelete">document.getAnonymousElementByAttribute(this, "anonid", "btnDelete")</field>
21
			<property name="realValue"><!-- Get and set de value in XED -->
22
				<getter>
23
					var vBind = xed.getBind(this);
24
					return vBind.fValue;
25
				</getter>
26
				<setter>
27
					var vBind = xed.getBind(this);
28
					vBind.fValue = val;
29
					return val;
30
				</setter>
31
			</property>
32
			<method name="help"><!-- help -->
33
				<body><![CDATA[
34
					system.openExternalUrl("http://scenari-platform.org/trac/office/wiki/modulewebdistant/");
35
				]]></body>
36
			</method>
37
			<method name="onxedload"><!-- Init - called on xed load -->
38
				<body><![CDATA[
39
					//xed.debug("mwFragment.onxedload");
40
					try{
41
						var vXbl = this;
42
						window.setTimeout(function(){vXbl.refresh()}, 10);
43
						this.fPreviewFrame.autoSize = this.autoSize.bind(this);
44
					} catch(e){xed.debug("ERROR - mwFragment.onxedload: "+e)}
45
				]]></body>
46
			</method>
47
			<method name="refresh"><!-- Refresh the preview frame with de current fragment -->
48
				<body><![CDATA[
49
					//xed.debug("mwFragment.refresh");
50
					try{
51
						var vXbl = this;
52
						var vVal = this.realValue;
53
						var vStr = '<html><head><title></title></head><body style="font:caption;" onload="frameElement.autoSize()">';
54
						if (!vVal || vVal == "") {
55
							vStr += '<div style="margin-top: 40px; text-align:center;color:silver;font-style:italic;">Veuillez coller un fragment HTML.</div>';
56
						} else {
57
							vStr += this.xDomString2HtmlString(vVal);
58
						}
59
						vStr += '</body></html>'
60
						//xed.debug("preview html = "+vStr);
61
						var vDataUri = "data:text/html;charset=utf-8,"+encodeURI(vStr);
62
						this.fPreviewFrame.docShell.allowJavascript = true;
63
						//xed.debug(vDataUri);
64
						this.fPreviewFrame.parentNode.height = 100;
65
						this.fPreviewFrame.setAttribute("src", vDataUri);
66
						this.fBtnCopy.disabled = !(vVal && vVal.length > 0);
67
						this.fBtnDelete.disabled = !(vVal && vVal.length > 0);
68
					} catch(e){xed.debug("ERROR - mwFragment.refresh: "+e)}
69
				]]></body>
70
			</method>
71
			<method name="autoSize"><!-- Update de height of the preview frame -->
72
				<body><![CDATA[
73
					try{
74
						var vHeight = this.fPreviewFrame.contentDocument.body.scrollHeight;
75
						this.fPreviewFrame.parentNode.height = vHeight + 2;
76
					} catch(e){xed.debug("ERROR - mwFragment.autoSize: "+e)}
77
				]]></body>
78
			</method>
79
			<method name="pasteFrag"><!-- Paste from clipboard - resets the preview frame before pasing -->
80
				<body><![CDATA[
81
					//xed.debug("mwFragment.pasteFrag");
82
					try{
83
						var vDataUri = "data:text/html;charset=utf-8,"+encodeURI('<html><head><title></title></head><body></body></html>');
84
						this.fPreviewFrame.setAttribute("src", vDataUri); // Charger une page HTML vide - on reset la page XHTML de preview
85
						this.fPreviewHeight = 100;
86
						var vXbl = this;
87
						window.setTimeout(function(){vXbl.xPasteFrag()}, 10); // Attendre que la page html vide soit chargée
88
					} catch(e){xed.debug("ERROR - mwFragment.pasteFrag: "+e)}
89
				]]></body>
90
			</method>
91
			<method name="copyFrag"><!-- Copy fragment to clipboard -->
92
				<body><![CDATA[
93
					//xed.debug("mwFragment.copyFrag");
94
					try{
95
						Components.classes['@mozilla.org/widget/clipboardhelper;1'].getService(Components.interfaces.nsIClipboardHelper).copyString(this.xDomString2HtmlString(this.realValue));
96
					} catch(e){xed.debug("ERROR - mwFragment.copyFrag: "+e)}
97
				]]></body>
98
			</method>
99
			<method name="deleteFrag"><!-- Delete fragment -->
100
				<body><![CDATA[
101
					//xed.debug("mwFragment.deleteFrag");
102
					try{
103
						this.updateVal("");
104
						this.refresh();
105
					} catch(e){xed.debug("ERROR - mwFragment.deleteFrag: "+e)}
106
				]]></body>
107
			</method>
108
			<method name="updateVal"><!-- Update undo history -->
109
				<parameter name="pNewVal"/>
110
				<parameter name="pNotHisto"/>
111
				<body><![CDATA[
112
					//xed.debug("mwFragment.updateVal");
113
					try{
114
						var vOldVal = this.realValue;
115
						if(vOldVal) {
116
							if(pNewVal == vOldVal) {
117
								//Valeurs identiques
118
								return false;
119
							}
120
						} else if(pNewVal==null || pNewVal=="") {
121
							//Valeurs nulles avant et après.
122
							return false;
123
						}
124
						this.realValue = pNewVal;
125
						if( ! pNotHisto) {
126
							xed.getEditor(this).addHisto(new XEDUpdateValHisto(this, vOldVal, pNewVal), true);
127
						} else {
128
							this.refresh();
129
						}
130
						var vEvent = document.createEvent("Events");
131
						vEvent.initEvent(XEDDataChangeEvent, true, false);
132
						this.dispatchEvent(vEvent);
133
						return true;
134
					} catch(e){xed.debug("ERROR - mwFragment.updateVal: "+e);}
135
				]]></body>
136
			</method>
137
			<method name="xPasteFrag"><!-- Paste the contents of the clipboard -->
138
				<body><![CDATA[
139
					//xed.debug("mwFragment.xPasteFrag");
140
					try{
141
						this.fPreviewFrame.docShell.allowJavascript = false;
142
						var vTrans = xed_transf.createTransferable();
143
						vTrans.addDataFlavor(xed_transf.FLAVOUR_TEXT_UNICODE);
144
						xed_transf.fClipboardService.getData(vTrans, 1);
145
						var vText = xed_transf.getStringDataFromTransferable(vTrans, xed_transf.FLAVOUR_TEXT_UNICODE);
146
						var vHtmlDoc = this.fPreviewFrame.contentDocument;
147
						vHtmlDoc.body.innerHTML = vText;
148
						var vXhtmlRoot = document.createDocumentFragment();
149
						this.xCloneChildren2Xhtml(vHtmlDoc.body, vXhtmlRoot);
150
						var vEncoder = Components.classes["@mozilla.org/layout/documentEncoder;1?type=application/xhtml+xml"].createInstance(Components.interfaces.nsIDocumentEncoder);
151
						vEncoder.init(vXhtmlRoot.ownerDocument, "application/xhtml+xml", 0);
152
						vEncoder.setContainerNode(vXhtmlRoot);
153
						var vEncFrag = vEncoder.encodeToString();
154
						//xed.debug("RESULT = "+vEncFrag);
155
						this.updateVal(vEncFrag);
156
						this.refresh();
157
					} catch(e){xed.debug("ERROR - mwFragment.xPasteFrag: "+e)}
158
				]]></body>
159
			</method>
160
			<method name="xDomString2HtmlString"><!-- transform a string representation of a DOM to a HTML string -->
161
				<parameter name="pVal"/>
162
				<body><![CDATA[
163
					//xed.debug("mwFragment.xDomString2HtmlString");
164
					try{
165
						var vHtmlRoot = document.createDocumentFragment();
166
						var vDoc = new DOMParser().parseFromString("<root>"+pVal+"</root>", "text/xml");
167
						this.xCloneChildren2Html(vDoc.firstChild, vHtmlRoot);
168
						var vEncoder = Components.classes["@mozilla.org/layout/documentEncoder;1?type=text/html"].createInstance(Components.interfaces.nsIDocumentEncoder);
169
						vEncoder.init(vHtmlRoot.ownerDocument, "text/html", 0);
170
						vEncoder.setContainerNode(vHtmlRoot);
171
						var vEncFrag = vEncoder.encodeToString();
172
						//xed.debug("RESULT = "+vEncFrag);
173
						return vEncFrag;
174
					} catch(e){xed.debug("ERROR - mwFragment.xDom2HtmlString: "+e)}
175
				]]></body>
176
			</method>
177
			<method name="xCloneChildren2Xhtml"><!-- parse a DOM cloning to an XHTML DOM -->
178
				<parameter name="pHtmlNode"/>
179
				<parameter name="pXhtml"/>
180
				<body><![CDATA[
181
					var vNode = pHtmlNode.firstChild;
182
					while(vNode) {
183
						switch(vNode.nodeType) {
184
						case 1 :
185
							var vClone = pXhtml.ownerDocument.createElementNS("http://www.w3.org/1999/xhtml", "xhtml:"+vNode.nodeName.toLowerCase());
186
							var vAtts = vNode.attributes;
187
							for(var i = 0; i < vAtts.length; i++) {
188
								var vAtt = vAtts.item(i);
189
								vClone.setAttribute(vAtt.nodeName, vAtt.nodeValue);
190
							}
191
							pXhtml.appendChild(vClone);
192
							if (vClone.localName.match(/noscript$/)) {
193
								var vDoc = new DOMParser().parseFromString("<root>"+vNode.firstChild.nodeValue+"</root>", "text/xml");
194
								this.xCloneChildren2Xhtml(vDoc.firstChild, vClone);
195
							} else this.xCloneChildren2Xhtml(vNode, vClone);
196
							break;
197
						case 3 :
198
						case 4 :
199
							var vClone = pXhtml.ownerDocument.createTextNode(vNode.nodeValue);
200
							pXhtml.appendChild(vClone);
201
							break;
202
						}
203
						vNode = vNode.nextSibling;
204
					}
205
				]]></body>
206
			</method>
207
			<method name="xCloneChildren2Html"><!-- parse a DOM cloning to an HTML DOM -->
208
				<parameter name="pHtmlNode"/>
209
				<parameter name="pHtml"/>
210
				<body><![CDATA[
211
					var vNode = pHtmlNode.firstChild;
212
					while(vNode) {
213
						switch(vNode.nodeType) {
214
						case 1 :
215
							var vClone = pHtml.ownerDocument.createElement(vNode.localName);
216
							var vAtts = vNode.attributes;
217
							for(var i = 0; i < vAtts.length; i++) {
218
								var vAtt = vAtts.item(i);
219
								if (! vAtt.nodeName.match(/^xmlns/)) vClone.setAttribute(vAtt.localName, vAtt.nodeValue);
220
							}
221
							pHtml.appendChild(vClone);
222
							this.xCloneChildren2Html(vNode, vClone);
223
							break;
224
						case 3 :
225
						case 4 :
226
							var vClone = pHtml.ownerDocument.createTextNode(vNode.nodeValue);
227
							pHtml.appendChild(vClone);
228
							break;
229
						}
230
						vNode = vNode.nextSibling;
231
					}
232
				]]></body>
233
			</method>
234
		</implementation>
235
	</binding>
236
</bindings>
237

    
238

    
    (1-1/1)