-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe.js
262 lines (220 loc) · 5.19 KB
/
frame.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
LIMO.namespace('LIMO.FRAME')(function(ns) {
/**
* Simple Application.
* with one model, controller, view class.
*/
ns.SimpleApplication = function() {
var _modelType;
var _viewType;
var _controllerType;
LIMO.define(this, {
/**
* initialize.
* @param {Object} model
* @param {Object} view
* @param {Object} controller
*/
_initialize : function(model, view, controller) {
},
setModelType : function(constructor) {
_modelType = constructor;
},
setViewType : function(constructor) {
_viewType = constructor;
},
setControllerType : function(constructor) {
_controllerType = constructor;
},
/**
* entry point
*/
start : function() {
var model = new _modelType();
var controller = new _controllerType(model);
var view = new _viewType(model, controller);
controller.setView(view);
this.controller = controller;
this.model = model;
this.view = view;
this._initialize(model, view, controller);
if(!controller.onStart) {
throw new Error('controller.onStart must be defined.');
}
controller.onStart();
}
});
}
/**
* define simple entry point. by define appns.SimpleApplication.
* appns.Model, appns.View, appns.Controller must be defined.
* @param {String} appns namepace name
*/
ns.defineSimpleEntryPoint = function(appns, initializer) {
initializer = initializer ||
function() {
};
LIMO.namespace(appns)(function(ans) {
ans.start = function() {
var app = new ns.SimpleApplication();
ans.app = app;
app._initialize = initializer;
app.setModelType(ans.Model);
app.setViewType(ans.View);
app.setControllerType(ans.Controller);
app.start();
}
});
}
//////////////////////////////////////////////////
// SimpleObserver
ns.toPropertyChanged = function(name) {
// return LIMO.util.capitalize(name) + 'PropertyChanged';
return 'change:' + name;
};
ns.Fireble = LIMO.extend(function() {
LIMO.define(this, {
createGetter : {
override : true,
value : function(target, name) {
return function() {
return target[name];
};
}
},
/**
* create setter
* setSomething(value, [option]), option.silent(not fire)
*/
createSetter : {
override : true,
value : function(target, name) {
return function(value, option) {
target[name] = value;
if(!option || option.silent !== true) {
this.fireEvent(ns.toPropertyChanged(name), value);
}
};
}
}
});
}, LIMO.util.PropertyAppender);
var fireble = new ns.Fireble();
/**
* create objserver type.
* has getter, and setter.
* setter fire Event.
*
* if "test" is property name,
* getter: getTest
* setter: setTest
* eventName: testPropertyChanged
*
* @param {Object} properties
*/
ns.createSimpleObserverType = function(properties) {
return LIMO.extend(function(option) {
if(option) {
LIMO.puts(this, option);
}
var events = {};
var methods = {};
for(var key in properties) {
if(properties.hasOwnProperty(key)) {
fireble.add(methods, key);
events[ns.toPropertyChanged(key)] = true;
}
}
var self = this;
methods['toJSON'] = function() {
return self;
};
//this.$super(constructor, events);
LIMO.util.Observable.call(this, events);
LIMO.define(this, methods);
}, LIMO.util.Observable);
};
ns.Collection = LIMO.extend(function() {
this.$super(ns.Collection, {
add : true,
remove : true
});
var self = this;
this.items = [];
//public for read only
function fire(name, option, param) {
if(!option || option.silent !== true) {
self.fireEvent(name, param);
}
}
function seacher(callback, filter) {
var res = false;
if(LIMO.isObject(filter)) {
var kv = filter
filter = function(item) {
for(var key in kv) {
if(!kv.hasOwnProperty(key))
continue;
if(item[key] != kv[key]) return false;
}
return true;
}
}
if(LIMO.isFunction(filter)) {
LIMO.each(self.items, function(item) {
if(filter(item)) {
res = true;
if(callback(item) === false)
return false;
}
});
}
return res;
}
LIMO.define(this, {
get: function(index){
return self.items[index];
},
add : function(item, option) {
if(Array.isArray(item)){
[].push.apply(self.items, item);
}
else{
self.items.push(item);
}
fire('add', option, item);
},
remove : function(item, option) {
var index = self.items.indexOf(item);
self.removeAt(index, option);
},
removeAt : function(index, option) {
if(index < 0 || self.items.length <= index)
return;
var item = self.items[index];
self.items.splice(index, 1);
if(!option || option.silent !== true) {
self.fireEvent('remove', item, index);
}
},
toJSON : function() {
return self.items;
},
find : function(filter) {
var res = null;
seacher(function(item) {
res = item;
return false;
}, filter);
return res;
},
select : function(filter) {
var res = [];
seacher(function(item) {
res.push(item);
return true;
}, filter);
return res;
}
});
}, LIMO.util.Observable);
});