liuyg
2021-07-02 25ce610f6ecca7325e7a743dc032c4a76559c63d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
define([
  "dojo/Deferred",
  "dojo/when"
], function(Deferred, when) {
 
  //the context require object that is relative to the module that use the plugin
  var require,
 
  // plugins must be loaded with window.$ and window.jQuery set
  // to the correct jQuery version instance
  // this object serves as store the exist jquery object. After all plugins are loaded,
  // restore this jquery object.
 
  // when load more than one modules at the same time that use this loader plugin, we load it
  // by order we use cache be cause the dojo loader will cache the loaded file, which means when
  // the same jquery is loaded two times, only executed one time
  jqueryCache = {},
 
  loadjQuery = function( /*String*/ jQueryUrl) {
    // summary:
    //      returns a promise to return the requested version
    //      of jQuery
    //
    // jQueryUrl:
    //      (String) the fully-qualified jQuery URL
 
    var def = new Deferred();
 
    if(jqueryCache.locked){
      setTimeout(function(){
        loadjQuery(jQueryUrl);
      }, 50);
      return def;
    }
 
    if(jqueryCache[jQueryUrl]){
      jqueryCache.locked = true;
      def.resolve(jqueryCache[jQueryUrl]);
      return def;
    }
 
    jqueryCache.locked = true;
 
    // load the jQuery version, run noConflict(),
    require([jQueryUrl], function() {
      jqueryCache[jQueryUrl] = window.jQuery;
      def.resolve(jqueryCache[jQueryUrl]);
    });
 
    return def;
  },
 
 
  loadPlugin = function(/*String*/ pluginUrls, i, def) {
    // summary:
    //      load the given jQuery plugin into the jQuery object
 
    require([pluginUrls[i]], function() {
      if(i === pluginUrls.length - 1){
        def.resolve();
      }else{
        i ++;
        loadPlugin(pluginUrls, i, def);
      }
    });
  },
 
  loadPlugins = function( /*String*/ jQueryUrl, /*Array*/ pluginUrls) {
    /*jshint unused: false*/
    // summary:
    //      load plugins by order
    var i = 0;
    var def = new Deferred();
    if(!pluginUrls[i]){
      def.resolve();
    }else{
      loadPlugin(pluginUrls, i, def);
    }
    return def;
  },
 
 
  jRequire = function(jqueryUrl, plugins) {
    // summary:
    //      Loads a jQuery and then loads each jQuery plugins in sequence. As each plugin
    //      is loaded, the global version of jQuery is set up
    //      with all previously loaded plugins.
    //
    //      The plugins will be loaded or run in sequence as all
    //      previous dependencies become available.
    //
    // jqueryUrl:
    //      (String) the jQuery url to load.
    //
    // plugins:
    //      (Array) an of array of plugin URLs, either absolute or path-relative.
    //
 
    var def = new Deferred();
    loadjQuery(jqueryUrl).then(function(jQuery){
      loadPlugins(jqueryUrl, plugins).then(function(){
        jqueryCache.locked = false;
        def.resolve(jQuery);
      });
    });
    return def;
  };
 
  return {
    load: function(id, _require, callback){
      var parts = id.split(","), jqueryUrl, plugins = [];
      require = _require;
      if(parts.length === 0){
        callback(null);
      }else{
        jqueryUrl = parts[0];
        plugins = parts.slice(1);
        when(jRequire(jqueryUrl, plugins), callback);
      }
    }
  };
});