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

RevLine 
[3735]1/*global jsToolBar */
2'use strict';
3
[2614]4/* Change link button actions
5-------------------------------------------------------- */
6jsToolBar.prototype.elements.link.data = {};
7jsToolBar.prototype.elements.link.fncall = {};
[2834]8jsToolBar.prototype.elements.link.open_url = 'popup_link.php?plugin_id=dcLegacyEditor';
[2614]9
[2751]10jsToolBar.prototype.elements.link.popup = function(args) {
[3735]11  window.the_toolbar = this;
12  args = args || '';
[2614]13
[3735]14  this.elements.link.data = {};
[3880]15  const url = this.elements.link.open_url + args;
[2614]16
[3735]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');
[2614]20};
21
22jsToolBar.prototype.elements.link.fn.wiki = function() {
[3735]23  this.elements.link.popup.call(this, '&hreflang=' + this.elements.link.default_hreflang);
[2614]24};
25jsToolBar.prototype.elements.link.fncall.wiki = function() {
[3880]26  const data = this.elements.link.data;
[2614]27
[3735]28  if (data.href == '') {
29    return;
30  }
[2614]31
[3880]32  let etag = '|' + data.href;
[3735]33  if (data.hreflang) {
34    etag += '|' + data.hreflang;
35  }
[2614]36
[3735]37  if (data.title) {
38    if (!data.hreflang) {
39      etag += '|';
40    }
41    etag += '|' + data.title;
42  }
[2614]43
[3735]44  if (data.content) {
[3880]45    this.encloseSelection(`[${data.content}`, `${etag}]`);
[3735]46  } else {
[3880]47    this.encloseSelection('[', `${etag}]`);
[3735]48  }
[2614]49};
50
51jsToolBar.prototype.elements.link.fn.xhtml = function() {
[3880]52  this.elements.link.popup.call(this, `&hreflang=${this.elements.link.default_hreflang}`);
[2614]53};
54jsToolBar.prototype.elements.link.fncall.xhtml = function() {
[3880]55  const data = this.elements.link.data;
[2614]56
[3735]57  if (data.href == '') {
58    return;
59  }
[2614]60
[3880]61  let stag = `<a href="${data.href}"`;
[2614]62
[3735]63  if (data.hreflang) {
[3880]64    stag += ` hreflang="${data.hreflang}"`;
[3735]65  }
66  if (data.title) {
[3880]67    stag += ` title="${data.title}"`;
[3735]68  }
69  stag += '>';
[3880]70  const etag = '</a>';
[2614]71
[3735]72  if (data.content) {
73    this.encloseSelection('', '', function() {
74      return stag + data.content + etag;
75    });
76  } else {
77    this.encloseSelection(stag, etag);
78  }
[2614]79};
80
81jsToolBar.prototype.elements.link.fn.wysiwyg = function() {
[3880]82  let href;
83  let title;
84  let hreflang;
[3735]85  href = title = hreflang = '';
86  hreflang = this.elements.link.default_hreflang;
[2614]87
[3735]88  var a = this.getAncestor();
[2614]89
[3735]90  if (a.tagName == 'a') {
91    href = a.tag.href || '';
92    title = a.tag.title || '';
93    hreflang = a.tag.hreflang || '';
94  }
[2614]95
[3880]96  this.elements.link.popup.call(this, `&href=${href}&hreflang=${hreflang}&title=${title}`);
[2614]97};
98jsToolBar.prototype.elements.link.fncall.wysiwyg = function() {
[3880]99  const data = this.elements.link.data;
[2614]100
[3880]101  let a = this.getAncestor();
[2614]102
[3735]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  }
[2614]125
[3735]126  // Create link
[3880]127  let n;
[3735]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);
[2614]139};
140jsToolBar.prototype.getAncestor = function() {
[3880]141  let res = {};
142  let range;
143  let commonAncestorContainer;
[2614]144
[3735]145  if (this.iwin.getSelection) { //gecko
[3880]146    const selection = this.iwin.getSelection();
[3735]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  }
[2614]156
[3880]157  let ancestorTagName = commonAncestorContainer.tagName.toLowerCase();
[3735]158  while (ancestorTagName != 'a' && ancestorTagName != 'body') {
159    commonAncestorContainer = commonAncestorContainer.parentNode;
160    ancestorTagName = commonAncestorContainer.tagName.toLowerCase();
161  }
[2614]162
[3735]163  res.tag = commonAncestorContainer;
164  res.tagName = ancestorTagName;
[2614]165
[3735]166  return res;
[2614]167};
168
169/* Image selector
170-------------------------------------------------------- */
171jsToolBar.prototype.elements.img_select = {
[3735]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 = {};
[2614]182
[3735]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  }
[2614]187};
188jsToolBar.prototype.elements.img_select.fn.wiki = function() {
[3735]189  this.elements.img_select.popup.call(this);
[2614]190};
191jsToolBar.prototype.elements.img_select.fncall.wiki = function() {
[3735]192  var d = this.elements.img_select.data;
193  if (d.src == undefined) {
194    return;
195  }
[2614]196
[3735]197  this.encloseSelection('', '', function(str) {
[3880]198    const alt = (str) ? str : d.title;
199    let res = `((${d.src}|${alt}`;
[2614]200
[3735]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    }
[2614]210
[3735]211    if (d.description) {
[3880]212      res += `|${d.description}`;
[3735]213    }
[2614]214
[3735]215    res += '))';
[2614]216
[3735]217    if (d.link) {
[3880]218      res = `[${res}|${d.url}${(alt) ? `||${alt}` : ''}]`;
[3735]219    }
[2614]220
[3735]221    return res;
222  });
[2614]223};
224jsToolBar.prototype.elements.img_select.fn.xhtml = function() {
[3735]225  this.elements.img_select.popup.call(this);
[2614]226};
227jsToolBar.prototype.elements.img_select.fncall.xhtml = function() {
[3880]228  const d = this.elements.img_select.data;
[3735]229  if (d.src == undefined) {
230    return;
231  }
[2614]232
[3735]233  this.encloseSelection('', '', function(str) {
[3880]234    const alt = (str) ? str : d.title;
235    let res = `<img src="${d.src}" alt="${alt.replace('&', '&amp;').replace('>', '&gt;').replace('<', '&lt;').replace('"', '&quot;')}"`;
[2614]236
[3735]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    }
[2614]244
[3735]245    if (d.description) {
[3880]246      res += ` title="${d.description.replace('&', '&amp;').replace('>', '&gt;').replace('<', '&lt;').replace('"', '&quot;')}"`;
[3735]247    }
[2614]248
[3735]249    res += ' />';
[2614]250
[3735]251    if (d.link) {
[3880]252      const ltitle = (alt) ? ` title="${alt.replace('&', '&amp;').replace('>', '&gt;').replace('<', '&lt;').replace('"', '&quot;')}"` : '';
253      res = `<a href="${d.url}"${ltitle}>${res}</a>`;
[3735]254    }
[2614]255
[3735]256    return res;
257  });
[2614]258};
259
260jsToolBar.prototype.elements.img.fn.wysiwyg = function() {
[3880]261  const src = this.elements.img.prompt.call(this);
[3735]262  if (!src) {
263    return;
264  }
[2614]265
[3880]266  const img = this.iwin.document.createElement('img');
[3735]267  img.src = src;
268  img.setAttribute('alt', this.getSelectedText());
[2614]269
[3735]270  this.insertNode(img);
[2614]271};
272
273jsToolBar.prototype.elements.img_select.fn.wysiwyg = function() {
[3735]274  this.elements.img_select.popup.call(this);
[2614]275};
276jsToolBar.prototype.elements.img_select.fncall.wysiwyg = function() {
[3880]277  const d = this.elements.img_select.data;
278  const alt = (this.getSelectedText()) ? this.getSelectedText() : d.title;
[3735]279  if (d.src == undefined) {
280    return;
281  }
[2614]282
[3880]283  const img = this.iwin.document.createElement('img');
[3735]284  img.src = d.src;
285  img.setAttribute('alt', alt);
[2614]286
[3735]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  }
[2614]314
[3735]315  if (d.description) {
316    img.setAttribute('title', d.description);
317  }
[2614]318
[3735]319  if (d.link) {
[3880]320    const a = this.iwin.document.createElement('a');
[3735]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  }
[2614]330};
331
332// MP3 helpers
[3735]333jsToolBar.prototype.elements.mp3_insert = {
334  fncall: {},
335  data: {}
336};
[2614]337jsToolBar.prototype.elements.mp3_insert.fncall.wiki = function() {
[3880]338  const d = this.elements.mp3_insert.data;
[3735]339  if (d.player == undefined) {
340    return;
341  }
[2614]342
[3735]343  this.encloseSelection('', '', function() {
344    return '\n///html\n' + d.player + '///\n';
345  });
[2614]346};
347jsToolBar.prototype.elements.mp3_insert.fncall.xhtml = function() {
[3880]348  const d = this.elements.mp3_insert.data;
[3735]349  if (d.player == undefined) {
350    return;
351  }
[2614]352
[3735]353  this.encloseSelection('', '', function() {
354    return '\n' + d.player + '\n';
355  });
[2614]356};
357jsToolBar.prototype.elements.mp3_insert.fncall.wysiwyg = function() {
[3735]358  return;
[2614]359};
360
361// FLV helpers
[3735]362jsToolBar.prototype.elements.flv_insert = {
363  fncall: {},
364  data: {}
365};
[2614]366jsToolBar.prototype.elements.flv_insert.fncall.wiki = function() {
[3880]367  const d = this.elements.flv_insert.data;
[3735]368  if (d.player == undefined) {
369    return;
370  }
[2614]371
[3735]372  this.encloseSelection('', '', function() {
373    return '\n///html\n' + d.player + '///\n';
374  });
[2614]375};
376jsToolBar.prototype.elements.flv_insert.fncall.xhtml = function() {
[3880]377  const d = this.elements.flv_insert.data;
[3735]378  if (d.player == undefined) {
379    return;
380  }
[2614]381
[3735]382  this.encloseSelection('', '', function() {
383    return '\n' + d.player + '\n';
384  });
[2614]385};
386jsToolBar.prototype.elements.flv_insert.fncall.wysiwyg = function() {
[3735]387  return;
[2614]388};
389
390/* Posts selector
391-------------------------------------------------------- */
392jsToolBar.prototype.elements.post_link = {
[3735]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 = {};
[2614]401
[3735]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  }
[2614]406};
407jsToolBar.prototype.elements.post_link.fn.wiki = function() {
[3735]408  this.elements.post_link.popup.call(this);
[2614]409};
410jsToolBar.prototype.elements.post_link.fn.xhtml = function() {
[3735]411  this.elements.post_link.popup.call(this);
[2614]412};
413jsToolBar.prototype.elements.post_link.fn.wysiwyg = function() {
[3735]414  this.elements.post_link.popup.call(this);
[2614]415};
416
417// Last space element
418jsToolBar.prototype.elements.space3 = {
[3735]419  type: 'space',
420  format: {
421    wysiwyg: true,
422    wiki: true,
423    xhtml: true
424  }
[2614]425};
Note: See TracBrowser for help on using the repository browser.

Sites map