Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/* global ns */
2
/**
3
 * @class
4
 * @alias H5PEditor.SelectorLegacy
5
 */
6
ns.SelectorLegacy = function (libraries, selectedLibrary, changeLibraryDialog) {
7
  var self = this;
8
 
9
  H5P.EventDispatcher.call(this);
10
 
11
  var defaultLibraryParameterized = selectedLibrary ? selectedLibrary.replace('.', '-').toLowerCase() : undefined;
12
  this.currentLibrary = selectedLibrary;
13
 
14
  var options = '<option value="-">-</option>';
15
  for (var i = 0; i < libraries.length; i++) {
16
    var library = libraries[i];
17
    var libraryName = ns.libraryToString(library);
18
 
19
    // Never deny editing existing content
20
    // For new content deny old or restricted libs.
21
    if (selectedLibrary === libraryName ||
22
      ((library.restricted === undefined || !library.restricted) &&
23
      library.isOld !== true
24
      )
25
    ) {
26
      options += '<option value="' + libraryName + '"';
27
      if (libraryName === selectedLibrary || library.name === defaultLibraryParameterized) {
28
        options += ' selected="selected"';
29
      }
30
      if (library.tutorialUrl !== undefined) {
31
        options += ' data-tutorial-url="' + library.tutorialUrl + '"';
32
      }
33
      if (library.exampleUrl !== undefined) {
34
        options += ' data-example-url="' + library.exampleUrl + '"';
35
      }
36
      options += '>' + library.title + (library.isOld===true ? ' (deprecated)' : '') + '</option>';
37
    }
38
  }
39
 
40
  this.$selector = ns.$('' +
41
    '<select name="h5peditor-library" title="' + ns.t('core', 'selectLibrary') + '"' + '>' +
42
      options +
43
    '</select>'
44
  ).change(function () {
45
    // Use timeout to avoid bug in Chrome >44, when confirm is used inside change event.
46
    // Ref. https://code.google.com/p/chromium/issues/detail?id=525629
47
    setTimeout(function () {
48
      if (!self.currentLibrary) {
49
        self.currentLibrary = self.$selector.val();
50
        self.trigger('selected');
51
        return;
52
      }
53
 
54
      self.currentLibrary = self.$selector.val();
55
      changeLibraryDialog.show(self.$selector.offset().top);
56
    }, 0);
57
  });
58
};
59
 
60
/**
61
 * Reset selector to provided library
62
 *
63
 * @param {string} library
64
 * @param {Object} params
65
 * @param {Object} metadata
66
 */
67
ns.SelectorLegacy.prototype.resetSelection = function (library, params, metadata) {
68
  this.$selector.val(library);
69
  this.currentParams = params;
70
  this.currentMetadata = metadata;
71
  this.currentLibrary = library;
72
};
73
 
74
/**
75
 * Get currently selected library.
76
 *
77
 * @returns {string}
78
 */
79
ns.SelectorLegacy.prototype.getSelectedLibrary = function (next) {
80
  var that = this;
81
  var $option = this.$selector.find(':selected');
82
  next({
83
    uberName: that.currentLibrary,
84
    tutorialUrl: $option.data('tutorial-url'),
85
    exampleUrl: $option.data('example-url')
86
  });
87
};
88
 
89
/**
90
 * Load new params into legacy selector
91
 *
92
 * @returns {undefined}
93
 */
94
ns.SelectorLegacy.prototype.getParams = function () {
95
  return this.currentParams;
96
};
97
 
98
/**
99
 * Load new metadata into legacy selector
100
 *
101
 * @returns {undefined}
102
 */
103
ns.SelectorLegacy.prototype.getMetadata = function () {
104
  return this.currentMetadata;
105
};
106
 
107
/**
108
 * Returns the html element for the hub
109
 *
110
 * @return {HTMLElement}
111
 */
112
ns.SelectorLegacy.prototype.getElement = function () {
113
  return this.$selector.get(0);
114
};