· 7 years ago · Oct 16, 2018, 05:10 AM
1/*global window, console, document,XMLHttpRequest, setTimeout, location, navigator*/
2(function () {
3 'use strict';
4
5 // private methods and properties are stored in this object
6 var core = {
7
8 // Constructor
9 init : function () {
10 core.parsePackages();
11 },
12
13 // Add new scripts and stylesheets into head
14 // @resource - path of file
15 // @type - JS or CSS
16 // @callback - function to be executed when file is loaded(not sure if it works in IE)
17 inject : function (resource, type, callback) {
18 var fileref = null;
19 if (resource && type === "js") {
20 fileref = document.createElement('script');
21 fileref.setAttribute("type", "text/javascript");
22 fileref.setAttribute("src", resource);
23 fileref.onreadystatechange = function () {
24 if (typeof callback === 'function') {
25 callback();
26 }
27 };
28 fileref.onload = function () {
29 if (typeof callback === 'function') {
30 callback();
31 }
32 };
33 } else if (resource && type === "css") {
34 fileref = document.createElement("link");
35 fileref.setAttribute("rel", "stylesheet");
36 fileref.setAttribute("type", "text/css");
37 fileref.setAttribute("href", resource);
38 }
39 if (typeof fileref !== "undefined") {
40 document.getElementsByTagName("head")[0].appendChild(fileref);
41 }
42 },
43
44 // Ajax stuff, based on John Resig script
45 ajax : function (opt) {
46 var options = {
47 type: opt.type || "POST",
48 url: opt.url || "",
49 timeout: opt.timeout || 5000,
50 onComplete: opt.onComplete || function () {},
51 onError: opt.onError || function () {},
52 onSuccess: opt.onSuccess || function () {},
53 data: opt.data || ""
54 },
55 timeoutLength = 5000,
56 requestDone = false,
57 xml = new XMLHttpRequest();
58
59 function httpSuccess(r) {
60 try {
61 return (!r.status && location.protocol === "file:") ||
62 (r.status >= 200 && r.status < 300) ||
63 r.status === 304 ||
64 (navigator.userAgent.indexOf("Safari") >= 0 && typeof r.status === "undefined");
65 } catch (e) {}
66 return false;
67 }
68
69 function httpData(r, type) {
70 var ct = r.getResponseHeader("content-type"),
71 data = !type && ct && ct.indexOf("xml") >= 0;
72 data = type === "xml" || data ? r.responseXML : r.responseText;
73 return data;
74 }
75
76 xml.open(options.type, options.url, true);
77
78 setTimeout(function () {
79 requestDone = true;
80 }, timeoutLength);
81
82 xml.onreadystatechange = function () {
83 if (xml.readyState === 4 && !requestDone) {
84 if (httpSuccess(xml)) {
85 options.onSuccess(httpData(xml, options.type));
86 } else {
87 options.onError();
88 }
89 options.onComplete();
90 xml = null;
91 }
92 };
93
94 xml.send();
95 },
96
97 // Just a namespace to store logs
98 LOG : {
99 critical : [],
100 message : []
101 },
102
103 // Some default properties
104 DEFAULTS : {
105 path_manifest : 'packman-manifest.json',
106 packages : null
107 },
108
109 // Load the manifest file, parse and store it in DEFAULTS.packages property
110 parsePackages : function () {
111 core.ajax({
112 type: "GET",
113 url: core.DEFAULTS.path_manifest,
114 onSuccess: function (pack) {
115 core.DEFAULTS.packages = (function () {
116 try {
117 return JSON.parse(pack);
118 } catch (ex) {
119 core.LOG.critical.push({
120 'time' : new Date(),
121 'message' : 'JSON Parse Error - Check your manifest file'
122 });
123 }
124 }());
125 },
126 onError: function () {
127 core.LOG.critical.push({
128 'time' : new Date(),
129 'message' : 'Manifest Error - Loading manifest file failed'
130 });
131 }
132 });
133 }
134 },
135
136 packman = function (settings) {
137 core.init();
138 };
139
140 // Change path of manifest file
141 packman.prototype.setPathManifest = function (path) {
142 if (path !== '' && path !== undefined) {
143 core.DEFAULTS.path_manifest = path;
144 } else {
145 core.LOG.critical.push({
146 'time' : new Date(),
147 'message' : 'Manifest Path Error - You need to specify the manifest path'
148 });
149 }
150 };
151
152 // Just return the stored, parsed packages
153 packman.prototype.getPackges = function () {
154 return core.DEFAULTS.packages.packages;
155 };
156
157 // Return one package, based on his name, or his properties
158 // @pack - String with package name
159 // @pack - Object literal with package properties
160 packman.prototype.getPackge = function (pack) {
161 var packs = core.DEFAULTS.packages.packages,
162 packsLen = packs.length,
163 i = 0,
164 curpack = null;
165 if (typeof pack === 'object') {
166 return "Not implemented";
167 } else if (typeof pack === 'string') {
168 for (i; i < packsLen; i = i + 1) {
169 if (packs[i].name === pack) {
170 curpack = packs[i];
171 }
172 }
173 if (curpack !== null) {
174 return curpack;
175 } else {
176 core.LOG.critical.push({
177 'time' : new Date(),
178 'message' : 'Package ' + pack + ' was not found'
179 });
180 }
181 } else {
182 core.LOG.critical.push({
183 'time' : new Date(),
184 'message' : 'Invalid package'
185 });
186 }
187 };
188
189 // Log messages or errors
190 packman.prototype.log = function (type, message) {
191 if (type !== 'critical' && type !== 'message' && message !== '') {
192 core.LOG.critical.push({
193 'time' : new Date(),
194 'message' : 'Inception Error - You need to specify an message and a correct type of log'
195 });
196 } else {
197 core.LOG[type].push({
198 'time' : new Date(),
199 'message' : message
200 });
201 }
202 };
203
204 // Return all errors
205 packman.prototype.debug = function () {
206 return core.LOG;
207 };
208
209 //
210 packman.prototype.use = function (pack, callback) {
211 var curPack = null,
212 deps = null,
213 i = 0,
214
215 loadDependencies = function (dependencies) {
216 var deps = dependencies.length;
217 for (i; i < deps; i = i + 1) {
218 packman.prototype.use(dependencies[i]);
219 }
220 };
221
222 if (typeof pack === 'object') {
223 if (pack.dependencies) {
224 loadDependencies(pack.dependencies);
225 }
226 core.inject(pack.path, pack.type || 'js', function () {
227 packman.prototype[pack.global] = window[pack.global];
228 try {
229 delete window[pack.global];
230 } catch (er) {
231 core.LOG.message.push({
232 'time' : new Date(),
233 'message' : 'Inject Error - Cannot delete global object ' + pack.global
234 });
235 }
236 if (typeof callback === 'function') {
237 callback(packman.prototype[pack.global]);
238 }
239 });
240 } else if (typeof pack === 'string') {
241 curPack = this.getPackge(pack);
242 deps = null;
243 i = 0;
244 if (curPack) {
245 if (curPack.dependencies) {
246 loadDependencies(curPack.dependencies);
247 }
248 core.inject(curPack.path, curPack.type || 'js', function () {
249 packman.prototype[curPack.global] = window[curPack.global];
250 try {
251 delete window[curPack.global];
252 } catch (er) {
253 core.LOG.message.push({
254 'time' : new Date(),
255 'message' : 'Inject Error - Cannot delete global object ' + pack.global
256 });
257 }
258 if (typeof callback === 'function') {
259 callback(packman.prototype[curPack.global]);
260 }
261 });
262 }
263 } else {
264 console.log('x');
265 }
266 };
267
268 window.packman = packman;
269}());
270
271
272//Sample manifest
273{
274 "packages" : [{
275 "name" : "jquery",
276 "type" : "js",
277 "path" : "js/jquery.min.js",
278 "version" : "1.7",
279 "global" : "jQuery"
280 },
281 {
282 "name" : "jqueryui-style",
283 "type" : "css",
284 "path" : "css/jqueryui.min.js",
285 "version" : "1.7"
286 },
287 {
288 "name" : "jqueryui",
289 "type" : "js",
290 "path" : "js/jqueryui.min.js",
291 "version" : "1.7",
292 "global" : "jQueryUI",
293 "dependencies" : [{
294 "name" : "core3",
295 "type" : "js",
296 "path" : "js/core3.js",
297 "version" : "1.0",
298 "global" : "Core3",
299 "dependencies" : [{
300 "name" : "core2",
301 "type" : "js",
302 "path" : "js/core2.js",
303 "version" : "1.0",
304 "global" : "Core2",
305 "dependencies" : [{
306 "name" : "core1",
307 "type" : "js",
308 "path" : "js/core1.js",
309 "version" : "1.0",
310 "global" : "Core1",
311 "dependencies" : [{
312 "name" : "core",
313 "type" : "js",
314 "path" : "js/core.js",
315 "version" : "1.0",
316 "global" : "Core"
317 }]
318 }]
319 }]
320 },
321 {
322 "name" : "base",
323 "type" : "js",
324 "path" : "js/base.js",
325 "version" : "1.0",
326 "global" : "Base"
327 },
328 {
329 "name" : "nucleo",
330 "type" : "js",
331 "path" : "js/nucleo.js",
332 "version" : "1.0",
333 "global" : "Nucleo"
334 }
335 ]
336 },
337 {
338 "name" : "yui",
339 "type" : "js",
340 "path" : "js/yui.min.js",
341 "version" : "2",
342 "global" : "YUI"
343 }]
344}