Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * The library list cache
3
 *
4
 * @type Object
5
 */
6
var llc = H5PEditor.LibraryListCache = {
7
  libraryCache: {},
8
  librariesComingIn: {},
9
  librariesMissing: {},
10
  que: []
11
};
12
 
13
/**
14
 * Get data for a list of libraries
15
 *
16
 * @param {Array} libraries - list of libraries to load info for (uber names)
17
 * @param {Function} handler - Callback when list of libraries is loaded
18
 * @param {Function} [thisArg] - Context for the callback function
19
 */
20
llc.getLibraries = function (libraries, handler, thisArg) {
21
  // Determine whether we're dealing with simple library strings or objects
22
  libraries = libraries.map(function (option) {
23
    return (typeof option === 'object') ? option.name : option;
24
  });
25
 
26
  var cachedLibraries = [];
27
  var status = 'hasAll';
28
  for (var i = 0; i < libraries.length; i++) {
29
    if (libraries[i] in llc.libraryCache) {
30
      // Libraries that are missing on the server are set to null...
31
      if (llc.libraryCache[libraries[i]] !== null) {
32
        cachedLibraries.push(llc.libraryCache[libraries[i]]);
33
      }
34
    }
35
    else if (libraries[i] in llc.librariesComingIn) {
36
      if (status === 'hasAll') {
37
        status = 'onTheWay';
38
      }
39
    }
40
    else {
41
      status = 'requestThem';
42
      llc.librariesComingIn[libraries[i]] = true;
43
    }
44
  }
45
  switch (status) {
46
    case 'hasAll':
47
      handler.call(thisArg, cachedLibraries);
48
      break;
49
    case 'onTheWay':
50
      llc.que.push({libraries: libraries, handler: handler, thisArg: thisArg});
51
      break;
52
    case 'requestThem':
53
      var ajaxParams = {
54
        type: "POST",
55
        url: H5PEditor.getAjaxUrl('libraries'),
56
        success: function (data) {
57
          llc.setLibraries(data, libraries);
58
          handler.call(thisArg, data);
59
          llc.runQue();
60
        },
61
        data: {
62
          'libraries': libraries
63
        },
64
        dataType: "json"
65
      };
66
      H5PEditor.$.ajax(ajaxParams);
67
      break;
68
  }
69
};
70
 
71
/**
72
 * Call all qued handlers
73
 */
74
llc.runQue = function () {
75
  var l = llc.que.length;
76
  for (var i = 0; i < l; i++) {
77
    var handlerObject = llc.que.shift();
78
    llc.getLibraries(handlerObject.libraries, handlerObject.handler, handlerObject.thisArg);
79
  }
80
};
81
 
82
/**
83
 * We've got new libraries from the server, save them
84
 *
85
 * @param {Array} libraries - Libraries with info from server
86
 * @param {Array} requestedLibraries - List of what libraries we requested
87
 */
88
llc.setLibraries = function (libraries, requestedLibraries) {
89
  var reqLibraries = requestedLibraries.slice();
90
  for (var i = 0; i < libraries.length; i++) {
91
    llc.libraryCache[libraries[i].uberName] = libraries[i];
92
    if (libraries[i].uberName in llc.librariesComingIn) {
93
      delete llc.librariesComingIn[libraries[i].uberName];
94
    }
95
    var index = reqLibraries.indexOf(libraries[i].uberName);
96
    if (index > -1) {
97
      reqLibraries.splice(index, 1);
98
    }
99
  }
100
  for (i = 0; i < reqLibraries.length; i++) {
101
    llc.libraryCache[reqLibraries[i]] = null;
102
    if (reqLibraries[i] in llc.librariesComingIn) {
103
      delete llc.librariesComingIn[libraries[i]];
104
    }
105
  }
106
};
107
 
108
/**
109
 * Creates a default content title, based on the title of the library
110
 * @param  {String} uberName "<machineName> <major>.<minor>"
111
 * @return {String}
112
 */
113
llc.getDefaultTitle = function (uberName) {
114
  var libraryMetadata = llc.libraryCache[uberName];
115
  var title = libraryMetadata && libraryMetadata.title ? libraryMetadata.title : '';
116
  return H5PEditor.t('core', 'untitled').replace(':libraryTitle', title);
117
};