forked from bitovi/funcunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
88 lines (84 loc) · 2.86 KB
/
Copy pathcore.js
File metadata and controls
88 lines (84 loc) · 2.86 KB
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
steal('./init', function(oldFuncUnit) {
if(!window.QUnit && !window.jasmine){
steal('funcunit/qunit')
}
var FuncUnit = oldFuncUnit.jQuery.sub();
var origFuncUnit = FuncUnit;
// override the subbed init method
// context can be an object with frame and forceSync:
// - a number or string: this is a frame name/number, and means only do a sync query
// - true: means force the query to be sync only
FuncUnit = function( selector, context ) {
// if you pass true as context, this will avoid doing a synchronous query
var frame,
forceSync,
isSyncOnly = false,
isAsyncOnly = false;
if(typeof context === "string"){
frame = context;
}
if(context && typeof context.forceSync === "boolean"){
forceSync = context.forceSync;
if (typeof context.frame == "number" || typeof context.frame == "string") {
frame = context.frame;
context = context.frame;
}
}
isSyncOnly = typeof forceSync === "boolean"? forceSync: isSyncOnly;
// if its a function, just run it in the queue
if(typeof selector == "function"){
return FuncUnit.wait(0, selector);
}
// if the app window already exists, adjust the params (for the sync return value)
this.selector = selector;
// run this method in the queue also
if(isSyncOnly === true){
var collection = performSyncQuery(selector, context, this);
collection.frame = frame;
return collection;
} else if(isAsyncOnly === true){
performAsyncQuery(selector, context, this);
return this;
} else { // do both
performAsyncQuery(selector, context, this);
var collection = performSyncQuery(selector, context, this);
collection.frame = frame;
return collection;
}
}
var getContext = function(context){
if (typeof context === "number" || typeof context === "string") {
// try to get the context from an iframe in the funcunit document
var sel = (typeof context === "number" ? "iframe:eq(" + context + ")" : "iframe[name='" + context + "']"),
frames = new origFuncUnit.fn.init(sel, FuncUnit.win.document, true);
context = (frames.length ? frames.get(0).contentWindow : FuncUnit.win).document;
} else {
context = FuncUnit.win.document;
}
return context;
},
performAsyncQuery = function(selector, context, self){
FuncUnit.add({
method: function(success, error){
if (FuncUnit.win) {
context = getContext(context);
}
this.selector = selector;
this.bind = new origFuncUnit.fn.init( selector, context, true );
success();
return this;
},
error: "selector failed: " + selector,
type: "query"
});
},
performSyncQuery = function(selector, context, self){
if (FuncUnit.win) {
context = getContext(context);
}
return new origFuncUnit.fn.init( selector, context, true );
}
oldFuncUnit.jQuery.extend(FuncUnit, oldFuncUnit, origFuncUnit)
FuncUnit.prototype = origFuncUnit.prototype;
return FuncUnit;
})()