-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcore_tools.js
144 lines (125 loc) · 4.25 KB
/
core_tools.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
132
133
134
135
136
137
138
139
140
141
142
143
144
/*globals ThothSC*/
SC.mixin(ThothSC,{
getTopLevelName: function(object){ //to send the application name
var completeName = object.toString();
if(completeName){
return completeName.split(".")[0];
}
else return "";
},
connect: function(callback){ // callback can/will be called with (event, data)
var func, toplevel, type, cb;
if(!callback && !this.defaultResponder){
throw new Error("ThothSC needs a callback or a responder. Define a defaultResponder on the data source or pass a callback to the connect function");
}
if(callback){
// type can be string, or statechart
type = SC.typeOf(callback);
if(type === 'string'){
func = SC.objectForPropertyPath(callback);
if(func){
if(!this.client){
toplevel = callback.substr(0,callback.indexOf("."));
if(toplevel !== "") window[toplevel].store._getDataSource(); //init DS
}
if(typeof func === 'function') cb = func;
else {
cb = function(event,data){
callback.sendEvent(event,data);
};
}
this.client.applicationCallback = cb;
this.client.connect();
return true;
}
else return false;
}
else if(type === 'hash'){ // assume a state chart
cb = function(event,data){
callback.sendEvent(event,data);
};
this.client.applicationCallback = cb;
this.client.connect();
return true;
}
else return false;
}
else {
// assume default responder
cb = function(event,data){
ThothSC.get('defaultResponder').sendEvent(event,data); //will this work?
};
this.client.applicationCallback = cb;
this.client.connect();
return true;
}
},
isXDomain: function(host){
return host !== document.domain;
},
recordTypeInQuery: function(query){
var recType, bucket, msg;
recType = query.get('recordType');
if(recType){
try {
bucket = recType.prototype.bucket;
}
catch(e) {
msg = "ThothSC cannot retrieve the resource from the record model. ";
msg += "This may be caused by an improper invocation of SC.Query.local().";
var err = SC.Error.create({ message: msg });
throw(err);
}
}
return recType;
},
createKey: function(){
// the idea for this method was copied from the php site:
// http://www.php.net/manual/en/function.session-regenerate-id.php#60478
var keyLength = 32,
keySource = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
keySourceLength = keySource.length + 1, // we need to add one, to make sure the last character will be used in generating the key
ret = [],
curCharIndex;
for(var i=0;i<=keyLength;i+=1){
curCharIndex = Math.floor(Math.random()*keySourceLength);
ret.push(keySource[curCharIndex]);
}
return ret.join('');
},
benchmark: function(func,context,times){
var start = new Date().getTime(), end;
var i;
for(i=0;i<times;i+=1){
if(func) func.call(context);
}
end = new Date().getTime();
console.log('start time: ' + start);
console.log('end time: ' + end);
console.log('time difference: ' + (end - start));
},
copy: function(obj){
var i,ret,inObjType,objType;
//if(!obj) return obj;
objType = SC.typeOf(obj);
if(objType === 'hash') ret = {}; //passed by reference
if(objType === 'array') ret = []; //passed by reference
if(objType === 'number') ret = obj; // passed by value
if(objType === 'string') ret = obj; // passed by value
if(objType === 'boolean') ret = obj; // passed by value
//if(debug) sys.log("copying: objType: " + objType);
if((objType === 'hash') || (objType === 'array')){
for(i in obj){
if(obj.hasOwnProperty(i)){
inObjType = SC.typeOf(obj[i]);
//if(debug) sys.log("copying: inObjType: " + inObjType);
if((inObjType === 'hash') || (inObjType === 'array')){
ret[i] = this.copy(obj[i]); //recursive copy of nested objects or arrays
}
else ret[i] = obj[i];
}
}
}
return ret;
}
});