-
Notifications
You must be signed in to change notification settings - Fork 8
/
mobl.mobl
1560 lines (1346 loc) · 42.9 KB
/
mobl.mobl
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module mobl
// Runtime Javascript files to be copied to www/
load js/gears_init.js
load js/jquery-ui-1.8.18.custom.min.js
load js/mobl.boot.js
load js/gestures.js
//load js/iscroll.js
// These will automatically be loaded
resource js/jquery-1.7.1.min.js
resource js/persistence.js
resource js/persistence.store.sql.js
resource js/persistence.store.websql.js
resource js/persistence.store.memory.js
resource js/persistence.search.js
/**
* Built-in types
*/
@doc "A value representing nothing"
external type void {}
@doc "String type"
@persistable
external type String : Object {
length : Num
// functions with variable nr of arguments:
// concat() Joins two or more strings, and returns a copy of the joined strings
// fromCharCode() Converts Unicode values to characters
sync function charAt(index : Num) : String
sync function charCodeAt(index : Num) : Num
sync function indexOf(searchstring : String, start : Num = 0) : Num
sync function lastIndexOf(searchstring : String, start : Num = 0) : Num
sync function match(regexp : RegExp) : Array<String>
sync function replace(regexp : RegExp, newstring : String) : String
sync function replace(substr : String, newstring : String) : String
sync function search(regexp : RegExp) : Num
sync function slice(start : Num, end : Num) : String
sync function split(separator : String, limit : Num = 1000) : Array<String>
sync function substr(start : Num, length : Num) : String
sync function substring(from : Num, to : Num) : String
sync function toLowerCase() : String
sync function toUpperCase() : String
}
@doc "Numeric type, represents both integers and floating point numbers"
@persistable
external type Num : Object {
sync function toFixed(digitsAfterDecimal : Num = 0) : String
sync function toPrecision(digits : Num) : String
}
@persistable
external type Bool : Object { }
external type Dynamic : Object { }
external type Style : String { }
@doc "RegExp type"
external type RegExp : Object {
global : Bool
ignoreCase : Bool
lastIndex : Num
multiline : Bool
source : String
sync function compile(regexp : RegExp) : void
sync function compile(regexp : RegExp, modifier : String) : void
sync function exec(string : String) : [String]
sync function test(string : String) : Bool
static sync function fromString(regex : String) : RegExp
}
<javascript>
__ns.Bool = {};
__ns.Num = {};
__ns.String = {};
</javascript>
<javascript for=RegExp>
__ns.RegExp = {
fromString: function(regexp) {
return new RegExp(regexp);
}
};
</javascript>
external type Array<T> {
length : Num
sync function get(n : Num) : T
sync function push(item : T) : void
sync function join(sep : String) : String
function one() : T
sync function map(fn : Function1<T, Dynamic>) : [?]
sync function filter(fn : Function1<T, Bool>) : [T]
sync function reduce(fn : Function2<T, T, ?>) : ?
sync function contains(el : T) : Bool
sync function containsEntity(el : T) : Bool
sync function containsElementWithValue(property : String, value : ?) : Bool
sync function splice(idx : Num, numToDelete : Num) : Array<T>
sync function insert(idx : Num, item : T) : void
sync function remove(item : T) : void
}
external type DOMEvent : Dynamic {
x : Num
y : Num
sync function preventDefault() : void
}
external type Map<K, V> {
sync function get(k : K) : V
sync function set(k : K, v : V) : void
sync function keys() : [K]
}
external type Tuple1<T1> {
_1 : T1
}
external type Tuple2<T1, T2> {
_1 : T1
_2 : T2
}
external type Tuple3<T1, T2, T3> {
_1 : T1
_2 : T2
_3 : T3
}
external type Tuple4<T1, T2, T3, T4> {
_1 : T1
_2 : T2
_3 : T3
_4 : T4
}
external type Control {}
external type Control1<T1> { }
external type Control2<T1, T2> { }
external type Control3<T1, T2, T3> { }
external type Control4<T1, T2, T3, T4> { }
external type Control5<T1, T2, T3, T4, T5> { }
external type Callback {}
external type Function0<RT> { }
external type Function1<T1, RT> { }
external type Function2<T1, T2, RT> { }
external type Function3<T1, T2, T3, RT> { }
external type Function4<T1, T2, T3, T4, RT> { }
external type Function5<T1, T2, T3, T4, T5, RT> { }
@persistable
external type Text : Object { }
@persistable
external type DateTime {
static sync function parse(s : String) : DateTime
static sync function fromTimestamp(timestamp : Num) : DateTime
static sync function create(year : Num, month : Num, day : Num, hour : Num = 0, minute : Num = 0, second : Num = 0, ms : Num = 0) : DateTime
sync function getFullYear() : Num
sync function getMonth() : Num
sync function getHours() : Num
sync function getMinutes() : Num
sync function getSeconds() : Num
sync function getMilliseconds() : Num
@doc "Day of the week, starting at 0 (= Sunday)"
sync function getDay() : Num
@doc "Day of the month, starting at 0"
sync function getDate() : Num
sync function setFullYear(y : Num) : Num
sync function setMonth(m : Num) : Num
@doc "Day of the month"
sync function setDate(d : Num) : Num
sync function setHours(h : Num) : void
sync function setMinutes(m : Num) : void
sync function setSeconds(s : Num) : void
sync function setMilliseconds(ms : Num) : void
sync function toString() : String
sync function toDateString() : String
sync function getTime() : Num
}
external function sleep(ms : Num) : void
external sync function repeat(ms : Num, fn : Callback) : void
external type Math {
static sync function round(n : Num) : Num
static sync function floor(n : Num) : Num
static sync function ceil(n : Num) : Num
static sync function abs(n : Num) : Num
static sync function acos(n : Num) : Num
static sync function asin(n : Num) : Num
static sync function atan(n : Num) : Num
static sync function atan2(n : Num, n2 : Num) : Num
static sync function cos(n : Num) : Num
static sync function exp(n : Num) : Num
static sync function log(n : Num) : Num
static sync function pow(n1 : Num, n2 : Num) : Num
static sync function random() : Num
static sync function sin(n : Num) : Num
static sync function sqrt(n : Num) : Num
static sync function tan(n : Num) : Num
static sync function max(n1: Num, n2 : Num) : Num
static sync function min(n1: Num, n2 : Num) : Num
static sync function pi() : Num
static sync function isNaN(n : Num) : Bool
}
external type JSON : Dynamic {
static sync function parse(s : String) : JSON
static sync function stringify(obj : Object) : String
}
external sync function now() : DateTime
external sync function parseNum(s : String) : Num
@doc "URL Encodes a string"
external sync function escape(s : String) : String
function mergeStyles(styles : [Style]) : Style {
var styleString : Dynamic = styles.join(" ");
return styleString;
}
external type Object {
sync function toString() : String
}
@doc "A virtual queryable collection"
@persistable
external type Collection<T> {
@doc "Return one item in the collection, or null if the collection is empty"
function one() : T
@doc "Prefetch a reference property"
sync function prefetch(property : String) : Collection<T>
@doc "Filter the collection on a property based on an operator `op` (options: '=', '<', '>', '<=', '>=' or '!=') and a value"
sync function filter(property : String, op : String, value : Object) : Collection<T>
@doc "Order the collection based on a property in ascending (ascending = true) or descending (ascending = false) order"
sync function order(property : String, ascending : Bool) : Collection<T>
@doc "Reverse the order of the items"
sync function reverse() : Collection<T>
@doc "Deletes all the items in the collection"
function destroyAll() : void
@doc "Count the number of items in the collection"
function count() : Num
@doc "Calculate the collection and return it as an array"
function list() : Array<T>
function selectJSON(properties : [String]) : JSON
sync function limit(n : Num) : Collection<T>
sync function skip(n : Num) : Collection<T>
sync function add(item : T) : void
sync function addAll(items : [T]) : void
sync function remove(item : T) : void
sync function updated() : void
}
external type Entity<T> {
id : String
new : Bool
dirty : Bool
delete : Bool
@doc "A virtual collection containing all instances of this entity"
static sync function all() : Collection<T>
static function load(id : String) : T
static function findBy(property : String, value : Object) : T
static sync function search(query : String) : Collection<T>
static sync function searchPrefix(query : String) : Collection<T>
static function fromSelectJSON(json : JSON) : T
function fetch(rel : String) : T
sync function toJSON() : JSON
function selectJSON(properties : [String]) : JSON
}
external type LocalStorage {
static sync function setItem(key : String, value : Object) : void
static sync function getItem(key : String, defaultValue : ? = null) : ?
static sync function getNum(key : String, defaultValue : Num = 0) : Num
static sync function getString(key : String, defaultValue : String = "") : String
static sync function getBool(key : String, defaultValue : Bool = false) : Bool
static sync function removeItem(key : String) : void
}
<javascript for=LocalStorage>
__ns.LocalStorage = {
setItem: function(key, value) {
window.localStorage.setItem(key, JSON.stringify(value));
},
removeItem: function(key) {
window.localStorage.removeItem(key);
},
getItem: function(key, defaultValue) {
var val = JSON.parse(window.localStorage.getItem(key) || "null") || defaultValue;
if(val && typeof val === 'object' && !val.addEventListener) {
return new mobl.ObservableObject(val);
} else {
return val;
}
},
getNum: function(key, defaultValue) {
return this.getItem(key, defaultValue);
},
getString: function(key, defaultValue) {
return this.getItem(key, defaultValue);
},
getBool: function(key, defaultValue) {
return this.getItem(key, defaultValue);
}
};
</javascript>
external type Type<T> {
static sync function fromJSON(json : JSON) : T
sync function toJSON() : ?
}
external sync function log(o : Object) : void
external sync function alert(o : Object) : void
external sync function add(e : Object) : void
external sync function remove(e : Object) : void
external function resetDatabase() : void
external function flushDatabase() : void
external sync function reload() : void
external sync function formatDate(d : DateTime) : String
external sync function formatDate2(d : DateTime) : String
external sync function openUrl(url : String) : void
external sync function range(from : Num, to : Num) : Array<Num>
external sync function random(max : Num) : Num
// Device checks
external sync function isIphone() : Bool
external sync function isIpad() : Bool
external sync function isAndroid() : Bool
external sync function isLandscape() : Bool
external sync function isPortrait() : Bool
external sync function isTouchDevice() : Bool
external function isOnline() : Bool
<javascript>
__ns.isIphone = function() { return !!navigator.userAgent.match(/iPhone/i) || !!navigator.userAgent.match(/iPod/i); };
__ns.isIpad = function() { return !!navigator.userAgent.match(/iPad/i); };
__ns.isAndroid = function() { return !!navigator.userAgent.match(/Android/i); };
__ns.isLandscape = function() { return window.innerHeight < window.innerWidth; };
__ns.isPortrait = function() { return window.innerHeight >= window.innerWidth; };
__ns.isTouchDevice = function() {
return 'ontouchstart' in document.documentElement;
};
__ns.isOnline = function(callback) {
var i = new Image();
i.onload = function() {
callback(true);
};
i.onerror = function() { callback(false); };
i.src = 'http://gfx2.hotmail.com/mail/uxp/w4/m4/pr014/h/s7.png?d=' + escape(Date());
};
</javascript>
external type JQuery : Dynamic {
length : Num
sync function fadeIn(fn : Callback = null) : JQuery
sync function fadeOut(fn : Callback = null) : JQuery
sync function slideUp(fn : Callback = null) : JQuery
sync function slideDown(fn : Callback = null) : JQuery
sync function slideToggle(fn : Callback = null) : JQuery
sync function eq(idx : Num) : JQuery
sync function find(selector : String) : JQuery
sync function parent() : JQuery
sync function parents(selector : String) : JQuery
sync function children() : JQuery
sync function contents() : JQuery
sync function hide() : JQuery
sync function show() : JQuery
sync function toggle() : JQuery
sync function detach() : JQuery
sync function addClass(cssClass : String) : JQuery
sync function hasClass(cssClass : String) : Bool
sync function css(name : String, val : String) : JQuery
sync function html() : String
sync function text() : String
sync function val() : String
sync function is(what : String) : Bool
sync function bind(eventName : String, fn : Callback) : JQuery
sync function unbind(eventName : String, fn : Callback) : JQuery
sync function replaceWith(coll : JQuery) : JQuery
sync function append(coll : JQuery) : JQuery
sync function prepend(coll : JQuery) : JQuery
sync function remove() : JQuery
sync function position() : JQueryPosition
sync function offset() : JQueryPosition
sync function innerWidth() : Num
sync function innerHeight() : Num
sync function outerWidth() : Num
sync function outerHeight() : Num
sync function scrollTop() : Num
}
external type JQueryPosition {
top : Num
left : Num
}
external sync function dyn(o : Object) : Dynamic
<javascript for=dyn>
__ns.dyn = function(o) { return o; };
</javascript>
external sync function $(sel : String) : JQuery
// Controls
@doc "Injects given HTML directly into the screen"
external control html(html : String)
control label(s : Object, style : Style = null, onclick : Callback = null) {
<span databind=s class=style onclick=onclick></span>
}
control block(cssClass : String = null, id : String = null, onclick : Callback = null, onswipe : Callback = null) {
<div id=id class=cssClass onclick=onclick onswipe=onswipe>
elements()
</div>
}
control span(cssClass : Style = null, id : String = null, onclick : Callback = null, onswipe : Callback = null) {
<span id=id class=cssClass onclick=onclick onswipe=onswipe>
elements()
</span>
}
control link(url : String, target : String = "_blank") {
l@<a href=url target=target>
elements()
</a>
script {
// Bit hacky, but ok, there's no elements API yet
if(l.contents().length == 0) {
l.text(url);
}
}
}
@doc "New-line control"
control nl() {
<br/>
}
control screenContext(id : String = null) {
<div class="screenContext" id=id style="position: relative;">
<div class="initialElements">
elements()
</div>
</div>
}
@doc "Load a localization bundle"
external function fetchLanguageBundle(path : String) : void
@doc "Retrieve an localized string from the loaded bundle"
external sync function _(key : String, placeholders : [Object] = []) : String
<javascript>
var bundle = {};
__ns.fetchLanguageBundle = function(path, callback) {
$.getJSON(path, function(json) {
bundle = json;
callback();
});
};
__ns._ = function(key, placeholders) {
var s = bundle[key] || key;
var parts = s.split('%%');
s = parts[0];
for(var i = 0; i < placeholders.length; i++) {
s += placeholders[i];
if(parts[i+1]) {
s += parts[i+1];
}
}
return s;
};
</javascript>
external sync function dummyMapper(d : ?) : ?
external function httpRequest(url : String, method : String = "GET", encoding : String = "json", data : String = null, mapper : Function1<?,?> = dummyMapper) : Dynamic
<javascript for=httpRequest>
__ns.httpRequest = function(url, method, encoding, data, mapper, callback) {
$.ajax({
url: url,
dataType: encoding,
type: method,
data: data,
error: function(_, message, error) {
console.error(message);
console.error(error);
callback(null);
},
success: function(data) {
var result = mapper(data, callback);
if(result !== undefined) {
callback(result);
}
}
});
};
</javascript>
<javascript>
var argspec = persistence.argspec;
__ns.$ = jQuery;
__ns.sleep = function(time, callback) {
setTimeout(callback, time);
};
__ns.Dynamic = function(props) {
for(var p in props) {
if(props.hasOwnProperty(p)) {
this[p] = props[p];
}
}
};
__ns.repeat = function(time, callback) {
setInterval(callback, time);
};
mobl.alert = function(s) {
alert(s);
};
mobl.log = function(s, _, callback) {
console.log(s);
if(callback) callback();
};
__ns.parseNum = function(s) {
return parseFloat(s, 10);
};
__ns.escape = function(s) {
return escape(s);
};
__ns.add = function(e) {
e["new"] = true;
var allEnt = persistence.define(e._type).all(); // NOTE: define() is a hack!
allEnt.add(e);
};
mobl.now = function() {
return new Date();
};
mobl.remove = function(e) {
persistence.remove(e);
var allEnt = persistence.define(e._type).all();
allEnt.triggerEvent('remove', allEnt, e);
allEnt.triggerEvent('change', allEnt, e);
};
mobl.flushDatabase = function(callback) {
persistence.flush(callback);
};
mobl.resetDatabase = function(callback) {
persistence.reset(function() {
persistence.schemaSync(callback);
});
};
mobl.reload = function() {
persistence.flush(function() {
window.location.reload();
});
};
mobl.openUrl = function(url) {
location = url;
};
mobl.random = function(max) {
return Math.round(Math.random()*max);
};
persistence.QueryCollection.prototype.updates = function() {
this.triggerEvent('change', this);
};
// Date stuff
mobl.DateTime = {
parse: function(s) {
return new Date(Date.parse(s));
},
fromTimestamp: function(timestamp) {
return new Date(timestamp);
},
create: function(year, month, day, hour, minute, second, ms) {
return new Date(year, month, day, hour, minute, second, ms);
}
};
Date.prototype.toDateString = function() {
return "" + (this.getMonth()+1) + "/" + this.getDate() + "/" + this.getFullYear();
};
mobl.Math = Math;
mobl.Math.pi = function() { return Math.PI; };
mobl.Math.isNaN = function(n) { return isNaN(n); };
mobl.JSON = JSON;
mobl.formatDate2 = function(date) {
var diff = (((new Date()).getTime() - date.getTime()) / 1000);
var day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 )
return;
return day_diff === 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff === 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff > 6 && "" + (date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear();
};
mobl.formatDate = function(date) {
var diff = (((new Date()).getTime() - date.getTime()) / 1000);
var day_diff = Math.floor(diff / 86400);
if ( isNaN(day_diff) || day_diff < 0 )
return;
return day_diff === 0 && (
diff < 60 && "just now" ||
diff < 120 && "1 minute ago" ||
diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
diff < 7200 && "1 hour ago" ||
diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
day_diff === 1 && "Yesterday" ||
day_diff < 7 && day_diff + " days ago" ||
day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
};
mobl.range = function(from, to) {
var ar = [];
if(from <= to) {
for(var i = from; i < to; i++) {
ar.push(i);
}
} else {
for(var i = from; i > to; i--) {
ar.push(i);
}
}
return ar;
};
mobl.html = function(html, elements, callback) {
var root192 = $("<span>");
var node180 = $("<span >");
var ref108 = html;
node180.html(html.get().toString());
var ignore51 = false;
ref108.addEventListener('change', function(_, ref, val) {
if(ignore51) return;
if(ref === ref108) {
node180.html(val.toString());
}
});
ref108.rebind();
root192.append(node180);
callback(root192); return;
};
mobl.defineType = function(qid, SuperType, fields) {
function Type(obj) {
this._data = {};
if(this.initialize) {
this.initialize();
}
for(var p in obj) {
if(obj.hasOwnProperty(p)) {
this[p] = obj[p];
}
}
}
Type.prototype = SuperType ? new SuperType() : new persistence.Observable();
for(var prop in fields) {
if(fields.hasOwnProperty(prop)) {
(function() {
var p = prop;
if(fields[p] === null) {
Type.prototype.__defineGetter__(p, function() {
return this._data[p];
});
Type.prototype.__defineSetter__(p, function(val) {
this._data[p] = val;
this.triggerEvent('change', this, p, val);
});
} else if(fields[p][0] === '[') {
}
}());
}
}
Type.fromJSON = function(json) {
return new Type(json);
};
Type.prototype.toJSON = function() {
var obj = {};
var type = this._data;
for(var p in this) {
if (this.hasOwnProperty(p) && p==='_data') {
if ($.isFunction(type[p]["toJSON"])) {
obj[p] = type[p].toJSON();
} else {
obj[p] = type[p];
}
}
}
for(var p in type) {
if (type.hasOwnProperty(p) && type[p] !== undefined) {
if ($.isFunction(type[p]["toJSON"])) {
obj[p] = type[p].toJSON();
} else {
obj[p] = type[p];
}
}
}
return new mobl.Dynamic(obj);
};
return Type;
};
persistence.entityDecoratorHooks.push(function(Entity) {
Entity.searchPrefix = function(query) {
return Entity.search(query, true);
};
});
Array.prototype.list = function(tx, callback) {
var args = argspec.getArgs(arguments, [
{name: 'tx', optional: true, check: function(obj) { return tx.executeSql; } },
{name: 'callback', optional: false, check: argspec.isCallback() }
]);
tx = args.tx;
callback = args.callback;
var valueCopy = [];
for(var i = 0; i < this.length; i++) {
valueCopy[i] = this[i];
}
callback(valueCopy);
};
Array.prototype.insert = function(idx, item) {
this.splice(idx, 0, item);
};
Array.prototype.get = function(idx) {
return this[idx];
};
Array.prototype.one = function(callback) {
if(this.length === 0) {
callback(null);
} else {
callback(this[0]);
}
};
Array.prototype.contains = function(el) {
for(var i = 0; i < this.length; i++) {
if(this[i] === el) {
return true;
}
}
return false;
};
Array.prototype.containsEnity = function(el) {
return this.containsElementWithValue("id", el.id);
};
Array.prototype.containsElementWithValue = function(prop, val) {
for(var i = 0; i < this.length; i++) {
if(this[i][prop] === val) {
return true;
}
}
return false;
};
Array.prototype.remove = function(el) {
for(var i = 0; i < this.length; i++) {
if(this[i] === el) {
this.splice(i, 1);
return;
}
}
};
Array.prototype.addEventListener = function() {};
mobl.dummyMapper = function(data, callback) {
callback(data);
};
mobl.Map = function() {
this.data = {};
};
mobl.Map.prototype.toJSON = function() {
var data = this.data;
var obj = {};
for(var key in data) {
if (data.hasOwnProperty(key)) {
if ($.isFunction(data[key].toJSON)) {
obj[key]=data[key].toJSON();
} else {
obj[key]=data[key];
}
}
}
return obj;
};
mobl.Map.prototype.set = function(k, v) {
this.data[k] = v;
};
mobl.Map.prototype.get = function(k) {
return this.data[k];
};
mobl.Map.prototype.keys = function() {
var keys = [];
for(var p in this.data) {
if(this.data.hasOwnProperty(p)) {
keys.push(p);
}
}
return keys;
};
mobl.screenStack = [];
mobl.innerHeight = false;
setTimeout(function() {
if(mobl.isAndroid) {
mobl.innerHeight = window.innerHeight;
}
}, 200);
function updateScrollers () {
var scrollwrappers = $("div#scrollwrapper");
if (scrollwrappers.length > 0) {
var height = mobl.innerHeight ? mobl.innerHeight : window.innerHeight;
height -= $("#footer:visible").height();
height -= $("#tabbar:visible").height();
scrollwrappers.height(height);
}
var scrollers = $("div#scrollwrapper div#content");
for ( var i = 0; i < scrollers.length; i++) {
var scroller = scrollers.eq(i).data("scroller");
if(scroller) {
scroller.refresh();
} else {
}
}
}
mobl.delayedUpdateScrollers = function() {
setTimeout(updateScrollers, 200);
};
if(!mobl.isAndroid) {
$(window).resize(updateScrollers);
}
$(function() {
// Set flushing at interval
setInterval(function() {
persistence.flush();
if(persistence.saveToLocalStorage) {
persistence.saveToLocalStorage();
}
}, 2500);
});
mobl.postCallHooks = [];
mobl.contextStack = [];
if(mobl.contextStack.length === 0) {
mobl.contextStack.push([{
screens: [],
dom: null,
id: '_top'
}]);
}
mobl.findDeepestVisibleContext = function(target) {
var idx = mobl.contextStack.length-1;
while(idx >= 0) {
var top = mobl.contextStack[idx];
for(var i = 0; i < top.length; i++) {
if(!top[i].dom) { // body
top[i].dom = $("body");
}
if(top[i].dom.is(':visible') && (!target || target === top[i].id)) {
return top[i];
}
}
idx--;
}
};
var TRANSITION_SPEED = 250;
__ns.animations = {};
__ns.animations.slide = function(prevNode, nextNode, forward, callback) {
//nextNode.show('slide', {direction: forward ? 'right' : 'left'}, TRANSITION_SPEED);
//prevNode.hide('slide', {direction: forward ? 'left' : 'right'}, TRANSITION_SPEED, callback);
var browserPrefix = jQuery.browser.mozilla ? '-moz-' : '-webkit-';
var makeCss = function(prop, value) {
var css = {};
css[browserPrefix + prop] = value;
return css;
};
nextNode.css(makeCss("transform", "translate3d(" + (forward ? "100%" : "-100%") + ",0px,0px)"));
nextNode.css(makeCss("transition-duration", TRANSITION_SPEED + "ms"));
nextNode.show();
setTimeout(function() {
nextNode.css(makeCss("transition-duration", TRANSITION_SPEED + "ms"));
nextNode.css(makeCss("transition-timing-function", "ease-in-out"));
prevNode.css(makeCss("transition-duration", TRANSITION_SPEED + "ms"));
prevNode.css(makeCss("transition-timing-function", "ease-in-out"));
nextNode.css(makeCss("transform", "translate3d(0px,0px,0px)"));
prevNode.css(makeCss("transform", "translate3d(" + (forward ? "-100%" : "100%") + ",0px,0px)"));
prevNode.bind("webkitTransitionEnd", function() {
prevNode.unbind("webkitTransitionEnd");
prevNode.hide();
nextNode.css(makeCss("transition-duration", null));
nextNode.css(makeCss("transition-timing-function", null));
prevNode.css(makeCss("transition-duration", null));
prevNode.css(makeCss("transition-timing-function", null));
callback();
});
}, 5);
};
__ns.animations.fade = function(prevNode, nextNode, forward, callback) {
nextNode.fadeIn(300);
prevNode.fadeOut(300, callback);