Dotclear

source: plugins/dcLegacyEditor/js/jsToolBar/jsToolBar.dotclear.js @ 3880:e6d1f6d9d7df

Revision 3880:e6d1f6d9d7df, 11.0 KB checked in by franck <carnet.franck.paul@…>, 7 years ago (diff)

Use let and const rather than var (ES2015/ES6), use template string where is more efficient

Line 
1/*global jsToolBar */
2'use strict';
3
4/* Change link button actions
5-------------------------------------------------------- */
6jsToolBar.prototype.elements.link.data = {};
7jsToolBar.prototype.elements.link.fncall = {};
8jsToolBar.prototype.elements.link.open_url = 'popup_link.php?plugin_id=dcLegacyEditor';
9
10jsToolBar.prototype.elements.link.popup = function(args) {
11  window.the_toolbar = this;
12  args = args || '';
13
14  this.elements.link.data = {};
15  const url = this.elements.link.open_url + args;
16
17  window.open(url, 'dc_popup',
18    'alwaysRaised=yes,dependent=yes,toolbar=yes,height=420,width=520,' +
19    'menubar=no,resizable=yes,scrollbars=yes,status=no');
20};
21
22jsToolBar.prototype.elements.link.fn.wiki = function() {
23  this.elements.link.popup.call(this, '&hreflang=' + this.elements.link.default_hreflang);
24};
25jsToolBar.prototype.elements.link.fncall.wiki = function() {
26  const data = this.elements.link.data;
27
28  if (data.href == '') {
29    return;
30  }
31
32  let etag = '|' + data.href;
33  if (data.hreflang) {
34    etag += '|' + data.hreflang;
35  }
36
37  if (data.title) {
38    if (!data.hreflang) {
39      etag += '|';
40    }
41    etag += '|' + data.title;
42  }
43
44  if (data.content) {
45    this.encloseSelection(`[${data.content}`, `${etag}]`);
46  } else {
47    this.encloseSelection('[', `${etag}]`);
48  }
49};
50
51jsToolBar.prototype.elements.link.fn.xhtml = function() {
52  this.elements.link.popup.call(this, `&hreflang=${this.elements.link.default_hreflang}`);
53};
54jsToolBar.prototype.elements.link.fncall.xhtml = function() {
55  const data = this.elements.link.data;
56
57  if (data.href == '') {
58    return;
59  }
60
61  let stag = `<a href="${data.href}"`;
62
63  if (data.hreflang) {
64    stag += ` hreflang="${data.hreflang}"`;
65  }
66  if (data.title) {
67    stag += ` title="${data.title}"`;
68  }
69  stag += '>';
70  const etag = '</a>';
71
72  if (data.content) {
73    this.encloseSelection('', '', function() {
74      return stag + data.content + etag;
75    });
76  } else {
77    this.encloseSelection(stag, etag);
78  }
79};
80
81jsToolBar.prototype.elements.link.fn.wysiwyg = function() {
82  let href;
83  let title;
84  let hreflang;
85  href = title = hreflang = '';
86  hreflang = this.elements.link.default_hreflang;
87
88  var a = this.getAncestor();
89
90  if (a.tagName == 'a') {
91    href = a.tag.href || '';
92    title = a.tag.title || '';
93    hreflang = a.tag.hreflang || '';
94  }
95
96  this.elements.link.popup.call(this, `&href=${href}&hreflang=${hreflang}&title=${title}`);
97};
98jsToolBar.prototype.elements.link.fncall.wysiwyg = function() {
99  const data = this.elements.link.data;
100
101  let a = this.getAncestor();
102
103  if (a.tagName == 'a') {
104    if (data.href == '') {
105      // Remove link
106      this.replaceNodeByContent(a.tag);
107      this.iwin.focus();
108      return;
109    } else {
110      // Update link
111      a.tag.href = data.href;
112      if (data.hreflang) {
113        a.tag.setAttribute('hreflang', data.hreflang);
114      } else {
115        a.tag.removeAttribute('hreflang');
116      }
117      if (data.title) {
118        a.tag.setAttribute('title', data.title);
119      } else {
120        a.tag.removeAttribute('title');
121      }
122      return;
123    }
124  }
125
126  // Create link
127  let n;
128  if (data.content) {
129    n = document.createTextNode(data.content);
130  } else {
131    n = this.getSelectedNode();
132  }
133  a = this.iwin.document.createElement('a');
134  a.href = data.href;
135  if (data.hreflang) a.setAttribute('hreflang', data.hreflang);
136  if (data.title) a.setAttribute('title', data.title);
137  a.appendChild(n);
138  this.insertNode(a);
139};
140jsToolBar.prototype.getAncestor = function() {
141  let res = {};
142  let range;
143  let commonAncestorContainer;
144
145  if (this.iwin.getSelection) { //gecko
146    const selection = this.iwin.getSelection();
147    range = selection.getRangeAt(0);
148    commonAncestorContainer = range.commonAncestorContainer;
149    while (commonAncestorContainer.nodeType != 1) {
150      commonAncestorContainer = commonAncestorContainer.parentNode;
151    }
152  } else { //ie
153    range = this.iwin.document.selection.createRange();
154    commonAncestorContainer = range.parentElement();
155  }
156
157  let ancestorTagName = commonAncestorContainer.tagName.toLowerCase();
158  while (ancestorTagName != 'a' && ancestorTagName != 'body') {
159    commonAncestorContainer = commonAncestorContainer.parentNode;
160    ancestorTagName = commonAncestorContainer.tagName.toLowerCase();
161  }
162
163  res.tag = commonAncestorContainer;
164  res.tagName = ancestorTagName;
165
166  return res;
167};
168
169/* Image selector
170-------------------------------------------------------- */
171jsToolBar.prototype.elements.img_select = {
172  type: 'button',
173  title: 'Image chooser',
174  accesskey: 'm',
175  fn: {},
176  fncall: {},
177  open_url: 'media.php?popup=1&plugin_id=dcLegacyEditor',
178  data: {},
179  popup: function() {
180    window.the_toolbar = this;
181    this.elements.img_select.data = {};
182
183    window.open(this.elements.img_select.open_url, 'dc_popup',
184      'alwaysRaised=yes,dependent=yes,toolbar=yes,height=500,width=760,' +
185      'menubar=no,resizable=yes,scrollbars=yes,status=no');
186  }
187};
188jsToolBar.prototype.elements.img_select.fn.wiki = function() {
189  this.elements.img_select.popup.call(this);
190};
191jsToolBar.prototype.elements.img_select.fncall.wiki = function() {
192  var d = this.elements.img_select.data;
193  if (d.src == undefined) {
194    return;
195  }
196
197  this.encloseSelection('', '', function(str) {
198    const alt = (str) ? str : d.title;
199    let res = `((${d.src}|${alt}`;
200
201    if (d.alignment == 'left') {
202      res += '|L';
203    } else if (d.alignment == 'right') {
204      res += '|R';
205    } else if (d.alignment == 'center') {
206      res += '|C';
207    } else if (d.description) {
208      res += '|';
209    }
210
211    if (d.description) {
212      res += `|${d.description}`;
213    }
214
215    res += '))';
216
217    if (d.link) {
218      res = `[${res}|${d.url}${(alt) ? `||${alt}` : ''}]`;
219    }
220
221    return res;
222  });
223};
224jsToolBar.prototype.elements.img_select.fn.xhtml = function() {
225  this.elements.img_select.popup.call(this);
226};
227jsToolBar.prototype.elements.img_select.fncall.xhtml = function() {
228  const d = this.elements.img_select.data;
229  if (d.src == undefined) {
230    return;
231  }
232
233  this.encloseSelection('', '', function(str) {
234    const alt = (str) ? str : d.title;
235    let res = `<img src="${d.src}" alt="${alt.replace('&', '&amp;').replace('>', '&gt;').replace('<', '&lt;').replace('"', '&quot;')}"`;
236
237    if (d.alignment == 'left') {
238      res += ' style="float: left; margin: 0 1em 1em 0;"';
239    } else if (d.alignment == 'right') {
240      res += ' style="float: right; margin: 0 0 1em 1em;"';
241    } else if (d.alignment == 'center') {
242      res += ' style="margin: 0 auto; display: block;"';
243    }
244
245    if (d.description) {
246      res += ` title="${d.description.replace('&', '&amp;').replace('>', '&gt;').replace('<', '&lt;').replace('"', '&quot;')}"`;
247    }
248
249    res += ' />';
250
251    if (d.link) {
252      const ltitle = (alt) ? ` title="${alt.replace('&', '&amp;').replace('>', '&gt;').replace('<', '&lt;').replace('"', '&quot;')}"` : '';
253      res = `<a href="${d.url}"${ltitle}>${res}</a>`;
254    }
255
256    return res;
257  });
258};
259
260jsToolBar.prototype.elements.img.fn.wysiwyg = function() {
261  const src = this.elements.img.prompt.call(this);
262  if (!src) {
263    return;
264  }
265
266  const img = this.iwin.document.createElement('img');
267  img.src = src;
268  img.setAttribute('alt', this.getSelectedText());
269
270  this.insertNode(img);
271};
272
273jsToolBar.prototype.elements.img_select.fn.wysiwyg = function() {
274  this.elements.img_select.popup.call(this);
275};
276jsToolBar.prototype.elements.img_select.fncall.wysiwyg = function() {
277  const d = this.elements.img_select.data;
278  const alt = (this.getSelectedText()) ? this.getSelectedText() : d.title;
279  if (d.src == undefined) {
280    return;
281  }
282
283  const img = this.iwin.document.createElement('img');
284  img.src = d.src;
285  img.setAttribute('alt', alt);
286
287  if (d.alignment == 'left') {
288    if (img.style.styleFloat != undefined) {
289      img.style.styleFloat = 'left';
290    } else {
291      img.style.cssFloat = 'left';
292    }
293    img.style.marginTop = 0;
294    img.style.marginRight = '1em';
295    img.style.marginBottom = '1em';
296    img.style.marginLeft = 0;
297  } else if (d.alignment == 'right') {
298    if (img.style.styleFloat != undefined) {
299      img.style.styleFloat = 'right';
300    } else {
301      img.style.cssFloat = 'right';
302    }
303    img.style.marginTop = 0;
304    img.style.marginRight = 0;
305    img.style.marginBottom = '1em';
306    img.style.marginLeft = '1em';
307  } else if (d.alignment == 'center') {
308    img.style.marginTop = 0;
309    img.style.marginRight = 'auto';
310    img.style.marginBottom = 0;
311    img.style.marginLeft = 'auto';
312    img.style.display = 'block';
313  }
314
315  if (d.description) {
316    img.setAttribute('title', d.description);
317  }
318
319  if (d.link) {
320    const a = this.iwin.document.createElement('a');
321    a.href = d.url;
322    if (alt) {
323      a.setAttribute('title', alt);
324    }
325    a.appendChild(img);
326    this.insertNode(a);
327  } else {
328    this.insertNode(img);
329  }
330};
331
332// MP3 helpers
333jsToolBar.prototype.elements.mp3_insert = {
334  fncall: {},
335  data: {}
336};
337jsToolBar.prototype.elements.mp3_insert.fncall.wiki = function() {
338  const d = this.elements.mp3_insert.data;
339  if (d.player == undefined) {
340    return;
341  }
342
343  this.encloseSelection('', '', function() {
344    return '\n///html\n' + d.player + '///\n';
345  });
346};
347jsToolBar.prototype.elements.mp3_insert.fncall.xhtml = function() {
348  const d = this.elements.mp3_insert.data;
349  if (d.player == undefined) {
350    return;
351  }
352
353  this.encloseSelection('', '', function() {
354    return '\n' + d.player + '\n';
355  });
356};
357jsToolBar.prototype.elements.mp3_insert.fncall.wysiwyg = function() {
358  return;
359};
360
361// FLV helpers
362jsToolBar.prototype.elements.flv_insert = {
363  fncall: {},
364  data: {}
365};
366jsToolBar.prototype.elements.flv_insert.fncall.wiki = function() {
367  const d = this.elements.flv_insert.data;
368  if (d.player == undefined) {
369    return;
370  }
371
372  this.encloseSelection('', '', function() {
373    return '\n///html\n' + d.player + '///\n';
374  });
375};
376jsToolBar.prototype.elements.flv_insert.fncall.xhtml = function() {
377  const d = this.elements.flv_insert.data;
378  if (d.player == undefined) {
379    return;
380  }
381
382  this.encloseSelection('', '', function() {
383    return '\n' + d.player + '\n';
384  });
385};
386jsToolBar.prototype.elements.flv_insert.fncall.wysiwyg = function() {
387  return;
388};
389
390/* Posts selector
391-------------------------------------------------------- */
392jsToolBar.prototype.elements.post_link = {
393  type: 'button',
394  title: 'Link to an entry',
395  fn: {},
396  open_url: 'popup_posts.php?plugin_id=dcLegacyEditor',
397  data: {},
398  popup: function() {
399    window.the_toolbar = this;
400    this.elements.img_select.data = {};
401
402    window.open(this.elements.post_link.open_url, 'dc_popup',
403      'alwaysRaised=yes,dependent=yes,toolbar=yes,height=500,width=760,' +
404      'menubar=no,resizable=yes,scrollbars=yes,status=no');
405  }
406};
407jsToolBar.prototype.elements.post_link.fn.wiki = function() {
408  this.elements.post_link.popup.call(this);
409};
410jsToolBar.prototype.elements.post_link.fn.xhtml = function() {
411  this.elements.post_link.popup.call(this);
412};
413jsToolBar.prototype.elements.post_link.fn.wysiwyg = function() {
414  this.elements.post_link.popup.call(this);
415};
416
417// Last space element
418jsToolBar.prototype.elements.space3 = {
419  type: 'space',
420  format: {
421    wysiwyg: true,
422    wiki: true,
423    xhtml: true
424  }
425};
Note: See TracBrowser for help on using the repository browser.

Sites map