Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/* global ns CKEDITOR */
2
/**
3
 * Adds a html text field to the form.
4
 *
5
 * @param {type} parent
6
 * @param {type} field
7
 * @param {type} params
8
 * @param {type} setValue
9
 * @returns {undefined}
10
 */
11
ns.Html = function (parent, field, params, setValue) {
12
  this.parent = parent;
13
  this.field = field;
14
  this.value = params;
15
  this.setValue = setValue;
16
  this.tags = ns.$.merge(['br'], (this.field.tags || this.defaultTags));
17
};
18
ns.Html.first = true;
19
 
20
ns.Html.prototype.defaultTags = ['strong', 'em', 'del', 'h2', 'h3', 'a', 'ul', 'ol', 'table', 'hr'];
21
 
22
// This should probably be named "hasTag()" instead...
23
// And might be more efficient if this.tags.contains() were used?
24
ns.Html.prototype.inTags = function (value) {
25
  return (ns.$.inArray(value.toLowerCase(), this.tags) >= 0);
26
};
27
 
28
/**
29
 * Check if the provided button is enabled by config.
30
 *
31
 * @param {string} value
32
 * @return {boolean}
33
 */
34
ns.Html.prototype.inButtons = function (button) {
35
  return (H5PIntegration.editor !== undefined && H5PIntegration.editor.wysiwygButtons !== undefined && H5PIntegration.editor.wysiwygButtons.indexOf(button) !== -1);
36
};
37
 
38
ns.Html.prototype.createToolbar = function () {
39
  var basicstyles = [];
40
  var paragraph = [];
41
  var formats = [];
42
  var inserts = [];
43
  var toolbar = [];
44
 
45
  // Basic styles
46
  if (this.inTags("strong") || this.inTags("b")) {
47
    basicstyles.push('Bold');
48
    // Might make "strong" duplicated in the tag lists. Which doesn't really
49
    // matter. Note: CKeditor will only make strongs.
50
    this.tags.push("strong");
51
  }
52
  if (this.inTags("em") || this.inTags("i")) {
53
    basicstyles.push('Italic');
54
    // Might make "em" duplicated in the tag lists. Which again
55
    // doesn't really matter. Note: CKeditor will only make ems.
56
    this.tags.push("em");
57
  }
58
  if (this.inTags("u")) basicstyles.push('Underline');
59
  if (this.inTags("strike") || this.inTags("del") || this.inTags("s")) {
60
    basicstyles.push('Strike');
61
    // Might make "strike" or "del" or both duplicated in the tag lists. Which
62
    // again doesn't really matter.
63
    this.tags.push("strike");
64
    this.tags.push("del");
65
    this.tags.push("s");
66
  }
67
  if (this.inTags("sub")) basicstyles.push("Subscript");
68
  if (this.inTags("sup")) basicstyles.push("Superscript");
69
  if (basicstyles.length > 0) {
70
    basicstyles.push("-");
71
    basicstyles.push("RemoveFormat");
72
    toolbar.push({
73
      name: 'basicstyles',
74
      items: basicstyles
75
    });
76
  }
77
 
78
  // Alignment is added to all wysiwygs
79
  toolbar.push({
80
    name: "justify",
81
    items: ["JustifyLeft", "JustifyCenter", "JustifyRight"]
82
  });
83
 
84
  // Paragraph styles
85
  if (this.inTags("ul")) {
86
    paragraph.push("BulletedList");
87
    this.tags.push("li");
88
  }
89
  if (this.inTags("ol")) {
90
    paragraph.push("NumberedList");
91
    this.tags.push("li");
92
  }
93
  if (this.inTags("blockquote")) paragraph.push("Blockquote");
94
  if (paragraph.length > 0) {
95
    toolbar.push(paragraph);
96
  }
97
 
98
  // Links.
99
  if (this.inTags("a")) {
100
    var items = ["Link", "Unlink"];
101
    if (this.inTags("anchor")) {
102
      items.push("Anchor");
103
    }
104
    toolbar.push({
105
      name: "links",
106
      items: items
107
    });
108
  }
109
 
110
  // Inserts
111
  if (this.inTags("img")) inserts.push("Image");
112
  if (this.inTags("table")) {
113
    inserts.push("Table");
114
    ns.$.merge(this.tags, ["tr", "td", "th", "colgroup", "thead", "tbody", "tfoot"]);
115
  }
116
  if (this.inTags("hr")) inserts.push("HorizontalRule");
117
  if (this.inTags('code')) {
118
    if (this.inButtons('inlineCode')) {
119
      inserts.push('Code');
120
    }
121
    if (this.inTags('pre') && this.inButtons('codeSnippet')) {
122
      inserts.push('CodeSnippet');
123
    }
124
  }
125
  if (inserts.length > 0) {
126
    toolbar.push({
127
      name: "insert",
128
      items: inserts
129
    });
130
  }
131
 
132
  // Create wrapper for text styling options
133
  var styles = {
134
    name: "styles",
135
    items: []
136
  };
137
  var colors = {
138
    name: "colors",
139
    items: []
140
  };
141
 
142
  // Add format group if formatters in tags (h1, h2, etc). Formats use their
143
  // own format_tags to filter available formats.
144
  if (this.inTags("h1")) formats.push("h1");
145
  if (this.inTags("h2")) formats.push("h2");
146
  if (this.inTags("h3")) formats.push("h3");
147
  if (this.inTags("h4")) formats.push("h4");
148
  if (this.inTags("h5")) formats.push("h5");
149
  if (this.inTags("h6")) formats.push("h6");
150
  if (this.inTags("address")) formats.push("address");
151
  if (this.inTags("pre")) formats.push("pre");
152
  if (formats.length > 0 || this.inTags('p') || this.inTags('div')) {
153
    formats.push("p");   // If the formats are shown, always have a paragraph..
154
    this.tags.push("p");
155
    styles.items.push('Format');
156
  }
157
 
158
  var ret = {
159
    toolbar: toolbar
160
  };
161
 
162
  if (this.field.font !== undefined) {
163
    this.tags.push('span');
164
 
165
    /**
166
     * Help set specified values for property.
167
     *
168
     * @private
169
     * @param {Array} values list
170
     * @param {string} prop Property
171
     * @param {string} [defProp] Default property name
172
     */
173
    var setValues = function (values, prop, defProp) {
174
      ret[prop] = '';
175
      for (var i = 0; i < values.length; i++) {
176
        var val = values[i];
177
        if (val.label && val.css) {
178
          // Add label and CSS
179
          ret[prop] += val.label + '/' + val.css + ';';
180
 
181
          // Check if default value
182
          if (defProp && val.default) {
183
            ret[defProp] = val.label;
184
          }
185
        }
186
      }
187
    };
188
 
189
    /**
190
     * @private
191
     * @param {Array} values
192
     * @returns {string}
193
     */
194
    var getColors = function (values) {
195
      var colors = '';
196
      for (var i = 0; i < values.length; i++) {
197
        var val = values[i];
198
        if (val.label && val.css) {
199
          var css = val.css.match(/^#?([a-f0-9]{3}[a-f0-9]{3}?)$/i);
200
          if (!css) {
201
            continue;
202
          }
203
 
204
          // Add label and CSS
205
          if (colors) {
206
            colors += ',';
207
          }
208
          colors += val.label + '/' + css[1];
209
        }
210
      }
211
      return colors;
212
    };
213
 
214
    if (this.field.font.family) {
215
      // Font family chooser
216
      styles.items.push('Font');
217
 
218
      if (this.field.font.family instanceof Array) {
219
        // Use specified families
220
        setValues(this.field.font.family, 'font_names', 'font_defaultLabel');
221
      }
222
    }
223
 
224
    if (this.field.font.size) {
225
      // Font size chooser
226
      styles.items.push('FontSize');
227
 
228
      ret.fontSize_sizes = '';
229
      if (this.field.font.size instanceof Array) {
230
        // Use specified sizes
231
        setValues(this.field.font.size, 'fontSize_sizes', 'fontSize_defaultLabel');
232
      }
233
      else {
234
        ret.fontSize_defaultLabel = '100%';
235
 
236
        // Standard font sizes that is available.
237
        var defaultAvailable = [8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72];
238
        for (var i = 0; i < defaultAvailable.length; i++) {
239
          // Calculate percentage of standard font size. This enables scaling
240
          // in content types without rounding errors across browsers.
241
          var em = defaultAvailable[i] / 16;
242
          ret.fontSize_sizes += (em * 100) + '%/' + em + 'em;';
243
        }
244
      }
245
    }
246
 
247
    if (this.field.font.color) {
248
      // Text color chooser
249
      colors.items.push('TextColor');
250
 
251
      if (this.field.font.color instanceof Array) {
252
        ret.colorButton_colors = getColors(this.field.font.color);
253
        ret.colorButton_enableMore = false;
254
      }
255
    }
256
 
257
    if (this.field.font.background) {
258
      // Text background color chooser
259
      colors.items.push('BGColor');
260
 
261
      if (this.field.font.background instanceof Array) {
262
        ret.colorButton_colors = getColors(this.field.font.color);
263
        ret.colorButton_enableMore = false;
264
      }
265
    }
266
  }
267
 
268
  // Add the text styling options
269
  if (styles.items.length) {
270
    toolbar.push(styles);
271
  }
272
  if (colors.items.length) {
273
    toolbar.push(colors);
274
  }
275
 
276
  // Set format_tags if not empty. CKeditor does not like empty format_tags.
277
  if (formats.length) {
278
    ret.format_tags = formats.join(';');
279
  }
280
 
281
  // Enable selection of enterMode in module semantics.
282
  if (this.field.enterMode === 'p' || formats.length > 0) {
283
    this.tags.push('p');
284
    ret.enterMode = CKEDITOR.ENTER_P;
285
  }
286
  else {
287
    // Default to DIV, not allowing BR at all.
288
    this.tags.push('div');
289
    ret.enterMode = CKEDITOR.ENTER_DIV;
290
  }
291
 
292
  return ret;
293
};
294
 
295
/**
296
 * Append field to wrapper.
297
 *
298
 * @param {type} $wrapper
299
 * @returns {undefined}
300
 */
301
ns.Html.prototype.appendTo = function ($wrapper) {
302
  var that = this;
303
 
304
  this.$item = ns.$(this.createHtml()).appendTo($wrapper);
305
  this.$input = this.$item.children('.ckeditor');
306
  this.$errors = this.$item.children('.h5p-errors');
307
 
308
  ns.bindImportantDescriptionEvents(this, this.field.name, this.parent);
309
 
310
  var ckConfig = {
311
    extraPlugins: "",
312
    startupFocus: true,
313
    enterMode: CKEDITOR.ENTER_DIV,
314
    allowedContent: true, // Disables the ckeditor content filter, might consider using it later... Must make sure it doesn't remove math...
315
    protectedSource: [],
316
    contentsCss: ns.basePath + 'styles/css/cke-contents.css', // We want to customize the CSS inside the editor
317
    codeSnippet_codeClass: 'h5p-hl'
318
  };
319
  ns.$.extend(ckConfig, this.createToolbar());
320
 
321
  // Look for additions in HtmlAddons
322
  if (ns.HtmlAddons) {
323
    for (var tag in ns.HtmlAddons) {
324
      if (that.inTags(tag)) {
325
        for (var provider in ns.HtmlAddons[tag]) {
326
          ns.HtmlAddons[tag][provider](ckConfig, that.tags);
327
        }
328
      }
329
    }
330
  }
331
 
332
  this.$item.children('.ckeditor').focus(function () {
333
 
334
    // Blur is not fired on destroy. Therefore we need to keep track of it!
335
    var blurFired = false;
336
 
337
    // Remove placeholder
338
    that.$placeholder = that.$item.find('.h5peditor-ckeditor-placeholder').detach();
339
 
340
    if (ns.Html.first) {
341
      CKEDITOR.basePath = ns.basePath + '/ckeditor/';
342
    }
343
 
344
    if (ns.Html.current === that) {
345
      return;
346
    }
347
    // Remove existing CK instance.
348
    ns.Html.removeWysiwyg();
349
 
350
    CKEDITOR.document.getBody = function () {
351
      // Have to attach to an element that does not get hidden or removed, since an internal "calculator" element
352
      // inside CKeditor relies on this element to always exist and not be hidden.
353
      return new CKEDITOR.dom.element(window.document.body);
354
    };
355
 
356
    // Override convertToPx to make sure the calculator is always visible so it can make the measurements and is
357
    // removed after the measurements are done, adapted from:
358
    // https://github.com/ckeditor/ckeditor4/blob/cae20318d46745cc46c811da4e7d68b38ca32449/core/tools.js#L899-L929
359
    CKEDITOR.tools.convertToPx = function (cssLength) {
360
        const calculator = CKEDITOR.dom.element.createFromHtml( '<div style="position:absolute;left:-9999px;' +
361
          'top:-9999px;margin:0px;padding:0px;border:0px;"' +
362
          '></div>', CKEDITOR.document );
363
        CKEDITOR.document.getBody().append(calculator);
364
 
365
        if ( !( /%$/ ).test( cssLength ) ) {
366
          var isNegative = parseFloat( cssLength ) < 0,
367
            ret;
368
 
369
          if ( isNegative ) {
370
            cssLength = cssLength.replace( '-', '' );
371
          }
372
 
373
          calculator.setStyle( 'width', cssLength );
374
          ret = calculator.$.clientWidth;
375
 
376
          if (calculator.$ && calculator.$.parentNode) {
377
            calculator.$.parentNode.removeChild(calculator.$);
378
          }
379
          if ( isNegative ) {
380
            return -ret;
381
          }
382
          return ret;
383
        }
384
 
385
        if (calculator.$ && calculator.$.parentNode) {
386
          calculator.$.parentNode.removeChild(calculator.$);
387
        }
388
        return cssLength;
389
    };
390
 
391
    ns.Html.current = that;
392
    ckConfig.width = this.offsetWidth - 8; // Avoid miscalculations
393
    that.ckeditor = CKEDITOR.replace(this, ckConfig);
394
 
395
    that.ckeditor.on('focus', function () {
396
      blurFired = false;
397
    });
398
 
399
    that.ckeditor.once('destroy', function () {
400
 
401
      // In some cases, the blur event is not fired. Need to be sure it is, so that
402
      // validation and saving is done
403
      if (!blurFired) {
404
        blur();
405
      }
406
 
407
      // Display placeholder if:
408
      // -- The value held by the field is empty AND
409
      // -- The value shown in the UI is empty AND
410
      // -- A placeholder is defined
411
      var value = that.ckeditor !== undefined ? that.ckeditor.getData() : that.$input.html();
412
      if (that.$placeholder.length !== 0 && (value === undefined || value.length === 0) && (that.value === undefined || that.value.length === 0)) {
413
        that.$placeholder.appendTo(that.$item.find('.ckeditor'));
414
      }
415
    });
416
 
417
    var blur = function () {
418
      blurFired = true;
419
      // Do not validate if the field has been hidden.
420
      if (that.$item.is(':visible')) {
421
        that.validate();
422
      }
423
    };
424
 
425
    that.ckeditor.on('blur', blur);
426
 
427
    // Add events to ckeditor. It is beeing done here since we know it exists
428
    // at this point... Use case from commit message: "Make the default
429
    // linkTargetType blank for ckeditor" - STGW
430
    if (ns.Html.first) {
431
      CKEDITOR.on('dialogDefinition', function (e) {
432
        // Take the dialog name and its definition from the event data.
433
        var dialogName = e.data.name;
434
        var dialogDefinition = e.data.definition;
435
 
436
        // Check if the definition is from the dialog window you are interested in (the "Link" dialog window).
437
        if (dialogName === 'link') {
438
          // Get a reference to the "Link Info" tab.
439
          var targetTab = dialogDefinition.getContents('target');
440
 
441
          // Set the default value for the URL field.
442
          var urlField = targetTab.get('linkTargetType');
443
          urlField['default'] = '_blank';
444
        }
445
 
446
        // Override show event handler
447
        var onShow = dialogDefinition.onShow;
448
        dialogDefinition.onShow = function () {
449
          if (onShow !== undefined) {
450
            onShow.apply(this, arguments);
451
          }
452
 
453
          // Grab current item
454
          var $item = ns.Html.current.$item;
455
 
456
          // Position dialog above text field
457
          var itemPos = $item[0].getBoundingClientRect();
458
          var dialogSize = this.getSize();
459
 
460
          var x = itemPos.x + (itemPos.width / 2) - (dialogSize.width / 2);
461
          var y = itemPos.y + (itemPos.height / 2) - (dialogSize.height / 2);
462
 
463
          this.move(x, y, true);
464
        };
465
      });
466
      ns.Html.first = false;
467
    }
468
  });
469
};
470
 
471
/**
472
 * Create HTML for the HTML field.
473
 */
474
ns.Html.prototype.createHtml = function () {
475
  const id = ns.getNextFieldId(this.field);
476
  var input = '<div id="' + id + '"';
477
  if (this.field.description !== undefined) {
478
    input += ' aria-describedby="' + ns.getDescriptionId(id) + '"';
479
  }
480
  input += ' class="ckeditor" tabindex="0" contenteditable="true">';
481
  if (this.value !== undefined) {
482
    input += this.value;
483
  }
484
  else if (this.field.placeholder !== undefined) {
485
    input += '<span class="h5peditor-ckeditor-placeholder">' + this.field.placeholder + '</span>';
486
  }
487
  input += '</div>';
488
 
489
  return ns.createFieldMarkup(this.field, ns.createImportantDescription(this.field.important) + input, id);
490
};
491
 
492
/**
493
 * Validate the current text field.
494
 */
495
ns.Html.prototype.validate = function () {
496
  var that = this;
497
 
498
  if (that.$errors.children().length) {
499
    that.$errors.empty();
500
    this.$input.addClass('error');
501
  }
502
 
503
  // Get contents from editor
504
  var value = this.ckeditor !== undefined ? this.ckeditor.getData() : this.$input.html();
505
 
506
  value = value
507
    // Remove placeholder text if any:
508
    .replace(/<span class="h5peditor-ckeditor-placeholder">.*<\/span>/, '')
509
    // Workaround for Microsoft browsers that otherwise can produce non-emtpy fields causing trouble
510
    .replace(/^<br>$/, '');
511
 
512
  var $value = ns.$('<div>' + value + '</div>');
513
  var textValue = $value.text();
514
 
515
  // Check if we have any text at all.
516
  if (!this.field.optional && !textValue.length) {
517
    // We can accept empty text, if there's an image instead.
518
    if (! (this.inTags("img") && $value.find('img').length > 0)) {
519
      this.$errors.append(ns.createError(ns.t('core', 'requiredProperty', {':property': ns.t('core', 'textField')})));
520
    }
521
  }
522
 
523
  // Verify HTML tags.  Removes tags not in allowed tags.  Will replace with
524
  // the tag's content.  So if we get an unallowed container, the contents
525
  // will remain, without the container.
526
  $value.find('*').each(function () {
527
    if (! that.inTags(this.tagName)) {
528
      ns.$(this).replaceWith(ns.$(this).contents());
529
    }
530
  });
531
  value = $value.html();
532
 
533
  // Display errors and bail if set.
534
  if (that.$errors.children().length) {
535
    return false;
536
  }
537
  else {
538
    this.$input.removeClass('error');
539
  }
540
 
541
  this.value = value;
542
  this.setValue(this.field, value);
543
  this.$input.change(); // Trigger change event.
544
 
545
  return value;
546
};
547
 
548
/**
549
 * Destroy H5PEditor existing CK instance. If it exists.
550
 */
551
ns.Html.removeWysiwyg = function () {
552
  if (ns.Html.current !== undefined) {
553
    try {
554
      ns.Html.current.ckeditor.destroy();
555
    }
556
    catch (e) {
557
      // No-op, just stop error from propagating. This usually occurs if
558
      // the CKEditor DOM has been removed together with other DOM data.
559
    }
560
    ns.Html.current = undefined;
561
  }
562
};
563
 
564
/**
565
 * Remove this item.
566
 */
567
ns.Html.prototype.remove = function () {
568
  this.$item.remove();
569
};
570
 
571
/**
572
 * When someone from the outside wants to set a value.
573
 *
574
 * @param {string} value
575
 */
576
ns.Html.prototype.forceValue = function (value) {
577
  if (this.ckeditor === undefined) {
578
    this.$input.html(value);
579
  }
580
  else {
581
    this.ckeditor.setData(value);
582
  }
583
  this.validate();
584
};
585
 
586
ns.widgets.html = ns.Html;