-
Notifications
You must be signed in to change notification settings - Fork 2
/
include.js
131 lines (111 loc) · 3.69 KB
/
include.js
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
123
124
125
126
127
128
129
130
131
// Copyright 2010, Vlad Balin aka "Gaperton".
// Dual licensed under the MIT or GPL Version 2 licenses.
// Include.js wrapper for require.js
//==================================
$.include = function() {
// Content plugin interface
// ------------------------
function DummyPlugIn( a_path ){
// translate resource address
this.path = function(){ return a_path; }
}
// content transformation...
DummyPlugIn.prototype.content = function( a_content ){ return a_content; };
// Helper class to handle require.js interaction
// ---------------------------------------------
function Context(){
var _context = {};
// module's path array for require.js 'define' call
this.paths = [];
// handler's array to create the context for include.js body function
var _args = [];
// add new variable to context, and take care of plugins...
this.add = function( a_name, a_path, a_subname ){
var plugin = a_name in $.include.plugins ?
new $.include.plugins[ a_name ]( a_path )
: new DummyPlugIn( a_path );
this.paths.push( plugin.path() );
_args.push( function( a_value ){
// transform value, if plugin is present...
var value = plugin.content( a_value );
// assign value to the context...
if( a_subname ){
_context[ a_name ][ a_subname ] = value;
}else{
_context[ a_name ] = value;
}
});
}
// create callback for require.js define function
this.on_load = function( a_body_f ){
return function() {
// fill context...
for( var i = 0; i < _args.length; i++ ){
_args[ i ]( arguments[ i ] );
}
// call module's body...
_context.exports = {};
var res = a_body_f( _context );
return res ? res : _context.exports;
};
}
}
// Include interface function definition
// -------------------------------------
function include() {
// initialize plugins at the first run...
var _scripts = [];
var _context = new Context();
// loop through include's arguments...
for( var i = 0; i < arguments.length; i++ ) {
var arg = arguments[ i ];
if( arg instanceof Array ){
// Ordered load of plain js scripts...
for( var j in arg ) {
_scripts.push( "order!" + arg[ j ] );
}
}
else if( typeof arg == "string" ){
// Unordered load of plain js scripts...
_scripts.push( arg );
}else{
// Load require.js modules & other content...
for( var name in arg ) {
var path = arg[ name ];
if( typeof path == "string" ){
_context.add( name, path );
}else{
for( var subname in path ){
_context.add( name, path[ subname ], subname );
}
}
}
}
}
// create paths array for require.js
var _paths = _context.paths.concat( _scripts );
return {
main: function( a_body_f ) {
$(function(){ //TODO: detect if jQuery is not used...
if( $.include.settings )
require( include.settings, _paths, _context.on_load( a_body_f ) );
else
define( _paths, _context.on_load( a_body_f ) )
});
},
define: function( a_body_f ){
return define( _paths, _context.on_load( a_body_f ) );
}
};
}
include.plugins = {};
return include;
}();
$.define = function( a_body_f ){
function on_load(){
var _context = { exports: {} };
var res = a_body_f( _context );
return res ? res : _context.exports;
}
return define( a_body_f );
};