forked from mascarenhas/scxib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.js
100 lines (82 loc) · 2.77 KB
/
core.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
// ==========================================================================
// Project: SCXIB
// Copyright: ©2010 Robert Linton
// Contributors: Devin Torres, Kurt Williams
// ==========================================================================
/*globals SCXIB DOMParser XSLTProcessor */
/*jslint evil: true */
/** @namespace
SCXIB allows you to load Interface Builder XIBs as SproutCore SC.Pages or
SC.Panels.
Example:
{{{
SCXIB.loadXibWithOptions(sc_static('MainPage.xib'), {
namespace: MyApp.NAMESPACE,
pageName: 'mainPage',
callback: function () {
MyApp.getPath('mainPage.mainPane').append();
}
});
}}}
@extends SC.Object
*/
SCXIB = SC.Object.create(
/** @scope SCXIB.prototype */ {
NAMESPACE: 'SCXIB',
VERSION: '0.1.0',
/**
Fetches a document using SC.Request and parses it as XML.
@param url {String} Location of the XML document.
@returns {Document}
*/
loadXmlDoc: function (url) {
var parser = new DOMParser(), doc;
SC.Request.getUrl(url).async(NO).notify(this, function (resp) {
if (SC.$ok(resp)) {
doc = parser.parseFromString(resp.rawRequest.responseText, 'text/xml');
} else {
SC.Logger.error('Could not fetch XML file "%@"'.fmt(url));
}
}).send();
return doc;
},
/**
Fetches a string for eval from an XSLT transformation of a XIB file.
@param xibOptions {Array} An array of XIB file paths and opts.
@param opts {Hash} A hash of opts for use during XSLT transformation.
@returns {void}
*/
loadXibsWithOptions: function (xibOptions) {
var self = this,
xslDoc = this.loadXmlDoc(sc_static('SCXIB.xslt')),
xibDoc, xsltProc, resDoc, callback;
xibOptions.forEach(function (opts) {
xibDoc = self.loadXmlDoc(opts.url);
xsltProc = new XSLTProcessor();
xsltProc.importStylesheet(xslDoc);
for (var p in opts) {
if (opts.hasOwnProperty(p)) {
xsltProc.setParameter(null, p, opts[p]);
}
}
var rp = opts.url.substring(0, opts.url.lastIndexOf('/') + 1);
xsltProc.setParameter(null, 'resourcesPath', rp);
resDoc = xsltProc.transformToFragment(xibDoc, document);
if (resDoc && resDoc.textContent) {
try {
eval(resDoc.textContent);
callback = opts.callback;
if (callback) callback.call(callback);
} catch (e) {
SC.Logger.error("Exception while evaluating XIB transform: %@, line %@".fmt(e, e.line));
}
} else {
SC.Logger.error('Could not parse XIB file "%@"'.fmt(opts.url));
}
});
},
loadXibWithOptions: function (url, options) {
var defaults = { url: url, pageName: 'mainPage' };
this.loadXibsWithOptions([SC.mixin(defaults, options)]);
}
});