Subversion Repositories oidplus

Rev

Rev 1042 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
597 daniel-mar 1
/**
2
 * Copyright (c) Tiny Technologies, Inc. All rights reserved.
3
 * Licensed under the LGPL or a commercial license.
4
 * For LGPL see License.txt in the project root for license information.
5
 * For commercial licenses see https://www.tiny.cloud/
6
 *
1422 daniel-mar 7
 * Version: 5.10.8 (2023-10-19)
597 daniel-mar 8
 */
9
(function () {
10
    'use strict';
11
 
637 daniel-mar 12
    var global$4 = tinymce.util.Tools.resolve('tinymce.PluginManager');
597 daniel-mar 13
 
637 daniel-mar 14
    var typeOf = function (x) {
15
      var t = typeof x;
16
      if (x === null) {
17
        return 'null';
18
      } else if (t === 'object' && (Array.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'Array')) {
19
        return 'array';
20
      } else if (t === 'object' && (String.prototype.isPrototypeOf(x) || x.constructor && x.constructor.name === 'String')) {
21
        return 'string';
22
      } else {
23
        return t;
24
      }
25
    };
26
    var isType = function (type) {
27
      return function (value) {
28
        return typeOf(value) === type;
29
      };
30
    };
31
    var isSimpleType = function (type) {
32
      return function (value) {
33
        return typeof value === type;
34
      };
35
    };
36
    var isString = isType('string');
37
    var isFunction = isSimpleType('function');
38
 
597 daniel-mar 39
    var noop = function () {
40
    };
41
    var constant = function (value) {
42
      return function () {
43
        return value;
44
      };
45
    };
637 daniel-mar 46
    var identity = function (x) {
47
      return x;
48
    };
597 daniel-mar 49
    function curry(fn) {
50
      var initialArgs = [];
51
      for (var _i = 1; _i < arguments.length; _i++) {
52
        initialArgs[_i - 1] = arguments[_i];
53
      }
54
      return function () {
55
        var restArgs = [];
56
        for (var _i = 0; _i < arguments.length; _i++) {
57
          restArgs[_i] = arguments[_i];
58
        }
59
        var all = initialArgs.concat(restArgs);
60
        return fn.apply(null, all);
61
      };
62
    }
63
    var never = constant(false);
64
    var always = constant(true);
65
 
637 daniel-mar 66
    var global$3 = tinymce.util.Tools.resolve('tinymce.util.Tools');
597 daniel-mar 67
 
68
    var global$2 = tinymce.util.Tools.resolve('tinymce.util.XHR');
69
 
70
    var getCreationDateClasses = function (editor) {
71
      return editor.getParam('template_cdate_classes', 'cdate');
72
    };
73
    var getModificationDateClasses = function (editor) {
74
      return editor.getParam('template_mdate_classes', 'mdate');
75
    };
76
    var getSelectedContentClasses = function (editor) {
77
      return editor.getParam('template_selected_content_classes', 'selcontent');
78
    };
79
    var getPreviewReplaceValues = function (editor) {
80
      return editor.getParam('template_preview_replace_values');
81
    };
82
    var getContentStyle = function (editor) {
83
      return editor.getParam('content_style', '', 'string');
84
    };
85
    var shouldUseContentCssCors = function (editor) {
86
      return editor.getParam('content_css_cors', false, 'boolean');
87
    };
88
    var getTemplateReplaceValues = function (editor) {
89
      return editor.getParam('template_replace_values');
90
    };
91
    var getTemplates = function (editor) {
92
      return editor.getParam('templates');
93
    };
94
    var getCdateFormat = function (editor) {
95
      return editor.getParam('template_cdate_format', editor.translate('%Y-%m-%d'));
96
    };
97
    var getMdateFormat = function (editor) {
98
      return editor.getParam('template_mdate_format', editor.translate('%Y-%m-%d'));
99
    };
100
    var getBodyClassFromHash = function (editor) {
101
      var bodyClass = editor.getParam('body_class', '', 'hash');
102
      return bodyClass[editor.id] || '';
103
    };
104
    var getBodyClass = function (editor) {
105
      var bodyClass = editor.getParam('body_class', '', 'string');
106
      if (bodyClass.indexOf('=') === -1) {
107
        return bodyClass;
108
      } else {
109
        return getBodyClassFromHash(editor);
110
      }
111
    };
112
 
113
    var addZeros = function (value, len) {
114
      value = '' + value;
115
      if (value.length < len) {
116
        for (var i = 0; i < len - value.length; i++) {
117
          value = '0' + value;
118
        }
119
      }
120
      return value;
121
    };
122
    var getDateTime = function (editor, fmt, date) {
637 daniel-mar 123
      if (date === void 0) {
124
        date = new Date();
125
      }
597 daniel-mar 126
      var daysShort = 'Sun Mon Tue Wed Thu Fri Sat Sun'.split(' ');
127
      var daysLong = 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday'.split(' ');
128
      var monthsShort = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
129
      var monthsLong = 'January February March April May June July August September October November December'.split(' ');
130
      fmt = fmt.replace('%D', '%m/%d/%Y');
131
      fmt = fmt.replace('%r', '%I:%M:%S %p');
132
      fmt = fmt.replace('%Y', '' + date.getFullYear());
133
      fmt = fmt.replace('%y', '' + date.getYear());
134
      fmt = fmt.replace('%m', addZeros(date.getMonth() + 1, 2));
135
      fmt = fmt.replace('%d', addZeros(date.getDate(), 2));
136
      fmt = fmt.replace('%H', '' + addZeros(date.getHours(), 2));
137
      fmt = fmt.replace('%M', '' + addZeros(date.getMinutes(), 2));
138
      fmt = fmt.replace('%S', '' + addZeros(date.getSeconds(), 2));
139
      fmt = fmt.replace('%I', '' + ((date.getHours() + 11) % 12 + 1));
140
      fmt = fmt.replace('%p', '' + (date.getHours() < 12 ? 'AM' : 'PM'));
141
      fmt = fmt.replace('%B', '' + editor.translate(monthsLong[date.getMonth()]));
142
      fmt = fmt.replace('%b', '' + editor.translate(monthsShort[date.getMonth()]));
143
      fmt = fmt.replace('%A', '' + editor.translate(daysLong[date.getDay()]));
144
      fmt = fmt.replace('%a', '' + editor.translate(daysShort[date.getDay()]));
145
      fmt = fmt.replace('%%', '%');
146
      return fmt;
147
    };
148
 
149
    var createTemplateList = function (editor, callback) {
150
      return function () {
151
        var templateList = getTemplates(editor);
637 daniel-mar 152
        if (isFunction(templateList)) {
597 daniel-mar 153
          templateList(callback);
637 daniel-mar 154
        } else if (isString(templateList)) {
597 daniel-mar 155
          global$2.send({
156
            url: templateList,
157
            success: function (text) {
158
              callback(JSON.parse(text));
159
            }
160
          });
161
        } else {
162
          callback(templateList);
163
        }
164
      };
165
    };
166
    var replaceTemplateValues = function (html, templateValues) {
637 daniel-mar 167
      global$3.each(templateValues, function (v, k) {
168
        if (isFunction(v)) {
597 daniel-mar 169
          v = v(k);
170
        }
171
        html = html.replace(new RegExp('\\{\\$' + k + '\\}', 'g'), v);
172
      });
173
      return html;
174
    };
637 daniel-mar 175
    var replaceVals = function (editor, scope) {
597 daniel-mar 176
      var dom = editor.dom, vl = getTemplateReplaceValues(editor);
637 daniel-mar 177
      global$3.each(dom.select('*', scope), function (e) {
178
        global$3.each(vl, function (v, k) {
597 daniel-mar 179
          if (dom.hasClass(e, k)) {
637 daniel-mar 180
            if (isFunction(v)) {
181
              v(e);
597 daniel-mar 182
            }
183
          }
184
        });
185
      });
186
    };
187
    var hasClass = function (n, c) {
188
      return new RegExp('\\b' + c + '\\b', 'g').test(n.className);
189
    };
190
    var insertTemplate = function (editor, _ui, html) {
191
      var dom = editor.dom;
192
      var sel = editor.selection.getContent();
193
      html = replaceTemplateValues(html, getTemplateReplaceValues(editor));
637 daniel-mar 194
      var el = dom.create('div', null, html);
597 daniel-mar 195
      var n = dom.select('.mceTmpl', el);
196
      if (n && n.length > 0) {
197
        el = dom.create('div', null);
198
        el.appendChild(n[0].cloneNode(true));
199
      }
637 daniel-mar 200
      global$3.each(dom.select('*', el), function (n) {
597 daniel-mar 201
        if (hasClass(n, getCreationDateClasses(editor).replace(/\s+/g, '|'))) {
202
          n.innerHTML = getDateTime(editor, getCdateFormat(editor));
203
        }
204
        if (hasClass(n, getModificationDateClasses(editor).replace(/\s+/g, '|'))) {
205
          n.innerHTML = getDateTime(editor, getMdateFormat(editor));
206
        }
207
        if (hasClass(n, getSelectedContentClasses(editor).replace(/\s+/g, '|'))) {
208
          n.innerHTML = sel;
209
        }
210
      });
211
      replaceVals(editor, el);
212
      editor.execCommand('mceInsertContent', false, el.innerHTML);
213
      editor.addVisual();
214
    };
215
 
216
    var none = function () {
217
      return NONE;
218
    };
219
    var NONE = function () {
220
      var call = function (thunk) {
221
        return thunk();
222
      };
637 daniel-mar 223
      var id = identity;
597 daniel-mar 224
      var me = {
225
        fold: function (n, _s) {
226
          return n();
227
        },
228
        isSome: never,
229
        isNone: always,
230
        getOr: id,
231
        getOrThunk: call,
232
        getOrDie: function (msg) {
233
          throw new Error(msg || 'error: getOrDie called on none.');
234
        },
235
        getOrNull: constant(null),
236
        getOrUndefined: constant(undefined),
237
        or: id,
238
        orThunk: call,
239
        map: none,
240
        each: noop,
241
        bind: none,
242
        exists: never,
243
        forall: always,
637 daniel-mar 244
        filter: function () {
245
          return none();
246
        },
597 daniel-mar 247
        toArray: function () {
248
          return [];
249
        },
250
        toString: constant('none()')
251
      };
252
      return me;
253
    }();
254
    var some = function (a) {
255
      var constant_a = constant(a);
256
      var self = function () {
257
        return me;
258
      };
259
      var bind = function (f) {
260
        return f(a);
261
      };
262
      var me = {
263
        fold: function (n, s) {
264
          return s(a);
265
        },
266
        isSome: always,
267
        isNone: never,
268
        getOr: constant_a,
269
        getOrThunk: constant_a,
270
        getOrDie: constant_a,
271
        getOrNull: constant_a,
272
        getOrUndefined: constant_a,
273
        or: self,
274
        orThunk: self,
275
        map: function (f) {
276
          return some(f(a));
277
        },
278
        each: function (f) {
279
          f(a);
280
        },
281
        bind: bind,
282
        exists: bind,
283
        forall: bind,
284
        filter: function (f) {
285
          return f(a) ? me : NONE;
286
        },
287
        toArray: function () {
288
          return [a];
289
        },
290
        toString: function () {
291
          return 'some(' + a + ')';
292
        }
293
      };
294
      return me;
295
    };
296
    var from = function (value) {
297
      return value === null || value === undefined ? NONE : some(value);
298
    };
299
    var Optional = {
300
      some: some,
301
      none: none,
302
      from: from
303
    };
304
 
305
    var map = function (xs, f) {
306
      var len = xs.length;
307
      var r = new Array(len);
308
      for (var i = 0; i < len; i++) {
309
        var x = xs[i];
310
        r[i] = f(x, i);
311
      }
312
      return r;
313
    };
314
    var findUntil = function (xs, pred, until) {
315
      for (var i = 0, len = xs.length; i < len; i++) {
316
        var x = xs[i];
317
        if (pred(x, i)) {
318
          return Optional.some(x);
319
        } else if (until(x, i)) {
320
          break;
321
        }
322
      }
323
      return Optional.none();
324
    };
325
    var find = function (xs, pred) {
326
      return findUntil(xs, pred, never);
327
    };
328
 
637 daniel-mar 329
    var global$1 = tinymce.util.Tools.resolve('tinymce.Env');
597 daniel-mar 330
 
637 daniel-mar 331
    var global = tinymce.util.Tools.resolve('tinymce.util.Promise');
597 daniel-mar 332
 
333
    var hasOwnProperty = Object.hasOwnProperty;
334
    var get = function (obj, key) {
335
      return has(obj, key) ? Optional.from(obj[key]) : Optional.none();
336
    };
337
    var has = function (obj, key) {
338
      return hasOwnProperty.call(obj, key);
339
    };
340
 
341
    var entitiesAttr = {
342
      '"': '&quot;',
343
      '<': '&lt;',
344
      '>': '&gt;',
345
      '&': '&amp;',
346
      '\'': '&#039;'
347
    };
348
    var htmlEscape = function (html) {
349
      return html.replace(/["'<>&]/g, function (match) {
350
        return get(entitiesAttr, match).getOr(match);
351
      });
352
    };
353
 
354
    var getPreviewContent = function (editor, html) {
355
      if (html.indexOf('<html>') === -1) {
356
        var contentCssEntries_1 = '';
357
        var contentStyle = getContentStyle(editor);
358
        var cors_1 = shouldUseContentCssCors(editor) ? ' crossorigin="anonymous"' : '';
637 daniel-mar 359
        global$3.each(editor.contentCSS, function (url) {
597 daniel-mar 360
          contentCssEntries_1 += '<link type="text/css" rel="stylesheet" href="' + editor.documentBaseURI.toAbsolute(url) + '"' + cors_1 + '>';
361
        });
362
        if (contentStyle) {
363
          contentCssEntries_1 += '<style type="text/css">' + contentStyle + '</style>';
364
        }
365
        var bodyClass = getBodyClass(editor);
366
        var encode = editor.dom.encode;
637 daniel-mar 367
        var isMetaKeyPressed = global$1.mac ? 'e.metaKey' : 'e.ctrlKey && !e.altKey';
597 daniel-mar 368
        var preventClicksOnLinksScript = '<script>' + 'document.addEventListener && document.addEventListener("click", function(e) {' + 'for (var elm = e.target; elm; elm = elm.parentNode) {' + 'if (elm.nodeName === "A" && !(' + isMetaKeyPressed + ')) {' + 'e.preventDefault();' + '}' + '}' + '}, false);' + '</script> ';
369
        var directionality = editor.getBody().dir;
370
        var dirAttr = directionality ? ' dir="' + encode(directionality) + '"' : '';
371
        html = '<!DOCTYPE html>' + '<html>' + '<head>' + '<base href="' + encode(editor.documentBaseURI.getURI()) + '">' + contentCssEntries_1 + preventClicksOnLinksScript + '</head>' + '<body class="' + encode(bodyClass) + '"' + dirAttr + '>' + html + '</body>' + '</html>';
372
      }
373
      return replaceTemplateValues(html, getPreviewReplaceValues(editor));
374
    };
375
    var open = function (editor, templateList) {
376
      var createTemplates = function () {
377
        if (!templateList || templateList.length === 0) {
378
          var message = editor.translate('No templates defined.');
379
          editor.notificationManager.open({
380
            text: message,
381
            type: 'info'
382
          });
383
          return Optional.none();
384
        }
637 daniel-mar 385
        return Optional.from(global$3.map(templateList, function (template, index) {
597 daniel-mar 386
          var isUrlTemplate = function (t) {
387
            return t.url !== undefined;
388
          };
389
          return {
390
            selected: index === 0,
391
            text: template.title,
392
            value: {
393
              url: isUrlTemplate(template) ? Optional.from(template.url) : Optional.none(),
394
              content: !isUrlTemplate(template) ? Optional.from(template.content) : Optional.none(),
395
              description: template.description
396
            }
397
          };
398
        }));
399
      };
400
      var createSelectBoxItems = function (templates) {
401
        return map(templates, function (t) {
402
          return {
403
            text: t.text,
404
            value: t.text
405
          };
406
        });
407
      };
408
      var findTemplate = function (templates, templateTitle) {
409
        return find(templates, function (t) {
410
          return t.text === templateTitle;
411
        });
412
      };
413
      var loadFailedAlert = function (api) {
414
        editor.windowManager.alert('Could not load the specified template.', function () {
415
          return api.focus('template');
416
        });
417
      };
418
      var getTemplateContent = function (t) {
637 daniel-mar 419
        return new global(function (resolve, reject) {
597 daniel-mar 420
          t.value.url.fold(function () {
421
            return resolve(t.value.content.getOr(''));
422
          }, function (url) {
423
            return global$2.send({
424
              url: url,
425
              success: function (html) {
426
                resolve(html);
427
              },
428
              error: function (e) {
429
                reject(e);
430
              }
431
            });
432
          });
433
        });
434
      };
435
      var onChange = function (templates, updateDialog) {
436
        return function (api, change) {
437
          if (change.name === 'template') {
438
            var newTemplateTitle = api.getData().template;
439
            findTemplate(templates, newTemplateTitle).each(function (t) {
440
              api.block('Loading...');
441
              getTemplateContent(t).then(function (previewHtml) {
442
                updateDialog(api, t, previewHtml);
443
              }).catch(function () {
444
                updateDialog(api, t, '');
445
                api.disable('save');
446
                loadFailedAlert(api);
447
              });
448
            });
449
          }
450
        };
451
      };
452
      var onSubmit = function (templates) {
453
        return function (api) {
454
          var data = api.getData();
455
          findTemplate(templates, data.template).each(function (t) {
456
            getTemplateContent(t).then(function (previewHtml) {
637 daniel-mar 457
              editor.execCommand('mceInsertTemplate', false, previewHtml);
597 daniel-mar 458
              api.close();
459
            }).catch(function () {
460
              api.disable('save');
461
              loadFailedAlert(api);
462
            });
463
          });
464
        };
465
      };
466
      var openDialog = function (templates) {
467
        var selectBoxItems = createSelectBoxItems(templates);
468
        var buildDialogSpec = function (bodyItems, initialData) {
469
          return {
470
            title: 'Insert Template',
471
            size: 'large',
472
            body: {
473
              type: 'panel',
474
              items: bodyItems
475
            },
476
            initialData: initialData,
477
            buttons: [
478
              {
479
                type: 'cancel',
480
                name: 'cancel',
481
                text: 'Cancel'
482
              },
483
              {
484
                type: 'submit',
485
                name: 'save',
486
                text: 'Save',
487
                primary: true
488
              }
489
            ],
490
            onSubmit: onSubmit(templates),
491
            onChange: onChange(templates, updateDialog)
492
          };
493
        };
494
        var updateDialog = function (dialogApi, template, previewHtml) {
495
          var content = getPreviewContent(editor, previewHtml);
496
          var bodyItems = [
497
            {
498
              type: 'selectbox',
499
              name: 'template',
500
              label: 'Templates',
501
              items: selectBoxItems
502
            },
503
            {
504
              type: 'htmlpanel',
505
              html: '<p aria-live="polite">' + htmlEscape(template.value.description) + '</p>'
506
            },
507
            {
508
              label: 'Preview',
509
              type: 'iframe',
510
              name: 'preview',
511
              sandboxed: false
512
            }
513
          ];
514
          var initialData = {
515
            template: template.text,
516
            preview: content
517
          };
518
          dialogApi.unblock();
519
          dialogApi.redial(buildDialogSpec(bodyItems, initialData));
520
          dialogApi.focus('template');
521
        };
522
        var dialogApi = editor.windowManager.open(buildDialogSpec([], {
523
          template: '',
524
          preview: ''
525
        }));
526
        dialogApi.block('Loading...');
527
        getTemplateContent(templates[0]).then(function (previewHtml) {
528
          updateDialog(dialogApi, templates[0], previewHtml);
529
        }).catch(function () {
530
          updateDialog(dialogApi, templates[0], '');
531
          dialogApi.disable('save');
532
          loadFailedAlert(dialogApi);
533
        });
534
      };
535
      var optTemplates = createTemplates();
536
      optTemplates.each(openDialog);
537
    };
538
 
539
    var showDialog = function (editor) {
540
      return function (templates) {
541
        open(editor, templates);
542
      };
543
    };
544
    var register$1 = function (editor) {
637 daniel-mar 545
      editor.addCommand('mceInsertTemplate', curry(insertTemplate, editor));
546
      editor.addCommand('mceTemplate', createTemplateList(editor, showDialog(editor)));
547
    };
548
 
549
    var setup = function (editor) {
550
      editor.on('PreProcess', function (o) {
551
        var dom = editor.dom, dateFormat = getMdateFormat(editor);
552
        global$3.each(dom.select('div', o.node), function (e) {
553
          if (dom.hasClass(e, 'mceTmpl')) {
554
            global$3.each(dom.select('*', e), function (e) {
555
              if (dom.hasClass(e, getModificationDateClasses(editor).replace(/\s+/g, '|'))) {
556
                e.innerHTML = getDateTime(editor, dateFormat);
557
              }
558
            });
559
            replaceVals(editor, e);
560
          }
561
        });
562
      });
563
    };
564
 
565
    var register = function (editor) {
566
      var onAction = function () {
567
        return editor.execCommand('mceTemplate');
568
      };
597 daniel-mar 569
      editor.ui.registry.addButton('template', {
570
        icon: 'template',
571
        tooltip: 'Insert template',
637 daniel-mar 572
        onAction: onAction
597 daniel-mar 573
      });
574
      editor.ui.registry.addMenuItem('template', {
575
        icon: 'template',
576
        text: 'Insert template...',
637 daniel-mar 577
        onAction: onAction
597 daniel-mar 578
      });
579
    };
580
 
581
    function Plugin () {
637 daniel-mar 582
      global$4.add('template', function (editor) {
583
        register(editor);
597 daniel-mar 584
        register$1(editor);
585
        setup(editor);
586
      });
587
    }
588
 
589
    Plugin();
590
 
591
}());