Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
var H5PEditor = window.H5PEditor = window.H5PEditor || {};
2
var ns = H5PEditor;
3
 
4
/**
5
 * Adds an image upload field with image editing tool to the form.
6
 *
7
 * @param {Object} parent Parent widget of this widget
8
 * @param {Object} field Semantic fields
9
 * @param {Object} params Existing image parameters
10
 * @param {function} setValue Function for updating parameters
11
 * @returns {ns.widgets.image}
12
 */
13
ns.widgets.image = function (parent, field, params, setValue) {
14
  var self = this;
15
 
16
  // Initialize inheritance
17
  ns.File.call(self, parent, field, params, setValue);
18
 
19
  this.parent = parent;
20
  this.field = field;
21
  this.params = params;
22
  this.setValue = setValue;
23
  this.library = parent.library + '/' + field.name;
24
  this.id = ns.getNextFieldId(field);
25
 
26
  if (params !== undefined) {
27
    this.copyright = params.copyright;
28
  }
29
 
30
  // Keep track of editing image
31
  this.isEditing = false;
32
 
33
  // Keep track of type of image that is being uploaded
34
  this.isOriginalImage = false;
35
 
36
  this.changes = [];
37
  this.passReadies = true;
38
  parent.ready(function () {
39
    self.passReadies = false;
40
  });
41
 
42
  this.confirmationDialog = new H5P.ConfirmationDialog({
43
    headerText: H5PEditor.t('core', 'removeImage'),
44
    bodyText: H5PEditor.t('core', 'confirmImageRemoval')
45
  });
46
 
47
  this.confirmationDialog.on('confirmed', function () {
48
    self.removeImage();
49
  });
50
 
51
  // When uploading starts
52
  self.on('upload', function () {
53
    // Hide edit image button
54
    self.$editImage.addClass('hidden');
55
  });
56
 
57
  // When a new file has been uploaded
58
  self.on('fileUploaded', function (event) {
59
    // Uploaded new original image
60
    if (self.isOriginalImage) {
61
      delete self.params.originalImage;
62
      self.editImagePopup.mime = self.params.mime
63
    }
64
 
65
    // Store width and height
66
    self.params.width = event.data.width;
67
    self.params.height = event.data.height;
68
 
69
    // Show edit image button
70
    self.$editImage.removeClass('hidden');
71
    self.isEditing = false;
72
  });
73
};
74
 
75
ns.widgets.image.prototype = Object.create(ns.File.prototype);
76
ns.widgets.image.prototype.constructor = ns.widgets.image;
77
 
78
/**
79
 * Append field to the given wrapper.
80
 *
81
 * @param {jQuery} $wrapper
82
 */
83
ns.widgets.image.prototype.appendTo = function ($wrapper) {
84
  var self = this;
85
 
86
  var htmlString = '<div class="file"></div>' +
87
    '<div class="h5p-editor-image-buttons">' +
88
      '<button class="h5peditor-button-textual h5p-editing-image-button">' + ns.t('core', 'editImage') + '</button>';
89
 
90
  if (!this.field.disableCopyright) {
91
    htmlString += '<button class="h5peditor-button-textual h5p-copyright-button">' + ns.t('core', 'editCopyright') + '</button>';
92
  }
93
 
94
  htmlString += '</div>' +
95
    '<div class="h5p-editor-dialog">' +
96
      '<a href="#" class="h5p-close" title="' + ns.t('core', 'close') + '"></a>' +
97
    '</div>';
98
 
99
  var html = ns.createFieldMarkup(this.field, htmlString, this.id);
100
 
101
  var $container = ns.$(html).appendTo($wrapper);
102
  this.$item = $container;
103
  this.$editImage = $container.find('.h5p-editing-image-button');
104
  this.$copyrightButton = $container.find('.h5p-copyright-button');
105
  this.$file = $container.find('.file');
106
  this.$errors = $container.find('.h5p-errors');
107
  this.addFile();
108
 
109
  var $dialog = $container.find('.h5p-editor-dialog');
110
  $container.find('.h5p-copyright-button').add($dialog.find('.h5p-close')).click(function () {
111
    $dialog.toggleClass('h5p-open');
112
    return false;
113
  });
114
 
115
  var editImagePopup = self.editImagePopup = new H5PEditor.ImageEditingPopup(this.field.ratio);
116
  editImagePopup.on('savedImage', function (e) {
117
 
118
    // Not editing any longer
119
    self.isEditing = false;
120
 
121
    // No longer an original image
122
    self.isOriginalImage = false;
123
 
124
    // Set current source as original image, if no original image
125
    if (!self.params.originalImage) {
126
      self.params.originalImage = {
127
        path: self.params.path,
128
        mime: self.params.mime,
129
        height: self.params.height,
130
        width: self.params.width
131
      };
132
    }
133
 
134
    const filenameparts = self.params.path.match(/([^\/]+)\.([^#]+)/);
135
 
136
    // Upload new image
137
    self.upload(e.data, filenameparts[1] + '-edit.' + filenameparts[2]);
138
  });
139
 
140
  editImagePopup.on('resetImage', function () {
141
    editImagePopup.setImage(self.params.originalImage ? self.params.originalImage : self.params);
142
  });
143
 
144
  editImagePopup.on('canceled', function () {
145
    self.isEditing = false;
146
  });
147
 
148
  editImagePopup.on('initialized', function () {
149
    // Remove throbber from image
150
    self.$editImage.removeClass('loading');
151
  });
152
 
153
  $container.find('.h5p-editing-image-button').click(function () {
154
    if (self.params && self.params.path) {
155
      var imageSrc;
156
      if (!self.isEditing) {
157
        imageSrc = self.params;
158
        self.isEditing = true;
159
      }
160
      self.$editImage.toggleClass('loading');
161
 
162
      // Add throbber to image
163
      editImagePopup.show(ns.$(this).offset(), imageSrc);
164
    }
165
  });
166
 
167
  ns.File.addCopyright(self, $dialog, function (field, value) {
168
    if (self.params !== undefined) {
169
      self.params.copyright = value;
170
    }
171
    self.copyright = value;
172
  });
173
 
174
};
175
 
176
 
177
/**
178
 * Sync copyright.
179
 */
180
ns.widgets.image.prototype.setCopyright = function (value) {
181
  this.copyright = this.params.copyright = value;
182
};
183
 
184
 
185
/**
186
 * Creates thumbnail HTML and actions.
187
 *
188
 * @returns {boolean} True if file was added, false if file was removed
189
 */
190
ns.widgets.image.prototype.addFile = function () {
191
  var that = this;
192
 
193
  if (this.params === undefined) {
194
 
195
    // No image look
196
    let html = '<a href="#" id="' + this.id + '" class="add"';
197
    if (this.field.description !== undefined) {
198
      html += ' aria-describedby="' + ns.getDescriptionId(this.id) + '"';
199
    }
200
    html += ' title="' + ns.t('core', 'addFile') + '">' +
201
      '<div class="h5peditor-field-file-upload-text">' + ns.t('core', 'add') + '</div>' +
202
    '</a>'
203
 
204
    this.$file
205
      .html(html)
206
      .children('.add')
207
      .click(function () {
208
        that.isOriginalImage = true;
209
        that.openFileSelector();
210
        return false;
211
      });
212
 
213
    // Remove edit image button
214
    this.$editImage.addClass('hidden');
215
    this.$copyrightButton.addClass('hidden');
216
    this.isEditing = false;
217
 
218
    return false;
219
  }
220
 
221
  var source = H5P.getPath(this.params.path, H5PEditor.contentId);
222
  var altText = (this.field.label === undefined ? '' : this.field.label);
223
  var fileHtmlString = '<a href="#" id="' + this.id + '" title="' + ns.t('core', 'changeFile') + '" class="thumbnail"';
224
  if (this.field.description !== undefined) {
225
    fileHtmlString += ' aria-describedby="' + ns.getDescriptionId(this.id) + '"';
226
  }
227
  fileHtmlString += '><img alt="' + altText + '"/></a>' +
228
    '<a href="#" class="remove" title="' + ns.t('core', 'removeFile') + '"></a>';
229
 
230
  this.$file.html(fileHtmlString)
231
    .children(':eq(0)')
232
    .click(function () {
233
      that.isOriginalImage = true;
234
      that.openFileSelector();
235
      return false;
236
    })
237
    .children('img')
238
    .attr('src', source)
239
    .end()
240
    .next()
241
    .click(function () {
242
      that.confirmRemovalDialog.show(that.$file.offset().top);
243
      return false;
244
    });
245
 
246
  var $img = this.$file.find('img');
247
  $img.one('load', function () {
248
    // Make editor resize
249
    $img.addClass('loaded');
250
  });
251
 
252
  // Uploading original image
253
  that.$editImage.removeClass('hidden');
254
  that.$copyrightButton.removeClass('hidden');
255
 
256
  // Notify listeners that image was changed to params
257
  that.trigger('changedImage', this.params);
258
 
259
  return true;
260
};
261
 
262
/**
263
 * Remove image
264
 */
265
ns.widgets.image.prototype.removeImage = function () {
266
 
267
  // Notify listeners that we removed image with params
268
  this.trigger('removedImage', this.params);
269
 
270
  delete this.params;
271
  this.setValue(this.field);
272
  this.addFile();
273
 
274
  for (var i = 0; i < this.changes.length; i++) {
275
    this.changes[i]();
276
  }
277
};
278
 
279
/**
280
 * Validate this item
281
 */
282
ns.widgets.image.prototype.validate = function () {
283
  return true;
284
};
285
 
286
/**
287
 * Remove this item.
288
 */
289
ns.widgets.image.prototype.remove = function () {
290
  // TODO: Check what happens when removed during upload.
291
  this.$file.parent().remove();
292
};
293
 
294
/**
295
 * Collect functions to execute once the tree is complete.
296
 *
297
 * @param {function} ready
298
 */
299
ns.widgets.image.prototype.ready = function (ready) {
300
  if (this.passReadies) {
301
    this.parent.ready(ready);
302
  }
303
  else {
304
    ready();
305
  }
306
};