1
- import { root , observable , observableArray } from 'ko-jsx'
2
- import template from './template'
1
+ import { root } from 'ko-jsx' ;
2
+ import ko from 'knockout' ;
3
+ import template from './template' ;
3
4
4
5
// var startTime;
5
6
// var lastMeasure;
6
7
7
8
// var startMeasure = function (name) {
8
- // startTime = performance.now();
9
- // lastMeasure = name;
9
+ // startTime = performance.now();
10
+ // lastMeasure = name;
10
11
// };
11
12
// var stopMeasure = function () {
12
- // window.setTimeout(function () {
13
- // var stop = performance.now();
14
- // console.log(lastMeasure + " took " + (stop - startTime));
15
- // }, 0);
13
+ // window.setTimeout(function () {
14
+ // var stop = performance.now();
15
+ // console.log(lastMeasure + " took " + (stop - startTime));
16
+ // }, 0);
16
17
// };
17
18
18
19
var HomeViewModel = function ( ) {
19
- var self = this ;
20
- self . id = 1 ;
21
-
22
- var adjectives = [ "pretty" , "large" , "big" , "small" , "tall" , "short" , "long" , "handsome" , "plain" , "quaint" , "clean" , "elegant" , "easy" , "angry" , "crazy" , "helpful" , "mushy" , "odd" , "unsightly" , "adorable" , "important" , "inexpensive" , "cheap" , "expensive" , "fancy" ] ;
23
- var colours = [ "red" , "yellow" , "blue" , "green" , "pink" , "brown" , "purple" , "brown" , "white" , "black" , "orange" ] ;
24
- var nouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ] ;
25
-
26
- function _random ( max ) {
27
- return Math . round ( Math . random ( ) * 1000 ) % max ;
20
+ var self = this ;
21
+ self . id = 1 ;
22
+
23
+ var adjectives = [ "pretty" , "large" , "big" , "small" , "tall" , "short" , "long" , "handsome" , "plain" , "quaint" , "clean" , "elegant" , "easy" , "angry" , "crazy" , "helpful" , "mushy" , "odd" , "unsightly" , "adorable" , "important" , "inexpensive" , "cheap" , "expensive" , "fancy" ] ;
24
+ var colours = [ "red" , "yellow" , "blue" , "green" , "pink" , "brown" , "purple" , "brown" , "white" , "black" , "orange" ] ;
25
+ var nouns = [ "table" , "chair" , "house" , "bbq" , "desk" , "car" , "pony" , "cookie" , "sandwich" , "burger" , "pizza" , "mouse" , "keyboard" ] ;
26
+
27
+ function _random ( max ) {
28
+ return Math . round ( Math . random ( ) * 1000 ) % max ;
29
+ }
30
+
31
+ function buildData ( count ) {
32
+ var data = [ ] ;
33
+ for ( var i = 0 ; i < count ; i ++ ) {
34
+ data . push ( {
35
+ id : self . id ++ ,
36
+ label : ko . observable ( adjectives [ _random ( adjectives . length ) ] + " " + colours [ _random ( colours . length ) ] + " " + nouns [ _random ( nouns . length ) ] )
37
+ } ) ;
28
38
}
29
-
30
- function buildData ( count ) {
31
- var data = [ ] ;
32
- for ( var i = 0 ; i < count ; i ++ ) {
33
- data . push ( new ItemViewModel ( {
34
- id : self . id ++ ,
35
- label : adjectives [ _random ( adjectives . length ) ] + " " + colours [ _random ( colours . length ) ] + " " + nouns [ _random ( nouns . length ) ]
36
- } ) ) ;
37
- }
38
- return data ;
39
+ return data ;
40
+ }
41
+
42
+ self . selected = ko . observable ( null ) ;
43
+ self . data = ko . observableArray ( ) ;
44
+
45
+ self . run = function ( ) {
46
+ // startMeasure("run");
47
+ self . data ( buildData ( 1000 ) ) ;
48
+ self . selected ( null ) ;
49
+ // stopMeasure();
50
+ } ;
51
+
52
+ self . runLots = function ( ) {
53
+ // startMeasure("runLots");
54
+ self . data ( buildData ( 10000 ) ) ;
55
+ self . selected ( null ) ;
56
+ // stopMeasure();
57
+ } ;
58
+
59
+ self . add = function ( ) {
60
+ // startMeasure("add");
61
+ self . data . push . apply ( self . data , buildData ( 1000 ) ) ;
62
+ // stopMeasure();
63
+ } ;
64
+
65
+ self . update = function ( ) {
66
+ // startMeasure("update");
67
+ var tmp = self . data ( ) ;
68
+ for ( let i = 0 ; i < tmp . length ; i += 10 ) {
69
+ tmp [ i ] . label ( tmp [ i ] . label ( ) + ' !!!' ) ;
39
70
}
40
-
41
- self . selected = observable ( null ) ;
42
- self . data = observableArray ( ) ;
43
-
44
- self . run = function ( ) {
45
- // startMeasure("run");
46
- self . data ( buildData ( 1000 ) ) ;
47
- self . selected ( null ) ;
48
- // stopMeasure();
49
- } ;
50
-
51
- self . runLots = function ( ) {
52
- // startMeasure("runLots");
53
- self . data ( buildData ( 10000 ) ) ;
54
- self . selected ( null ) ;
55
- // stopMeasure();
56
- } ;
57
-
58
- self . add = function ( ) {
59
- // startMeasure("add");
60
- self . data . push . apply ( self . data , buildData ( 1000 ) ) ;
61
- // stopMeasure();
62
- } ;
63
-
64
- self . update = function ( ) {
65
- // startMeasure("update");
66
- var tmp = self . data ( ) ;
67
- for ( let i = 0 ; i < tmp . length ; i += 10 ) {
68
- tmp [ i ] . label ( tmp [ i ] . label ( ) + ' !!!' ) ;
69
- }
70
- // stopMeasure();
71
- } ;
72
-
73
- self . clear = function ( ) {
74
- // startMeasure("clear");
75
- self . data . removeAll ( ) ;
76
- self . selected ( null ) ;
77
- // stopMeasure();
78
- } ;
79
-
80
- self . swapRows = function ( ) {
81
- // startMeasure("swapRows");
82
- var tmp = self . data ( ) ;
83
- if ( tmp . length > 998 ) {
84
- var a = tmp [ 1 ] ;
85
- tmp [ 1 ] = tmp [ 998 ] ;
86
- tmp [ 998 ] = a ;
87
- self . data ( tmp ) ;
88
- }
89
- // stopMeasure();
90
- } ;
91
-
92
- self . select = function ( id ) {
93
- // startMeasure("select");
94
- self . selected ( id ) ;
95
- // stopMeasure();
96
- } ;
97
-
98
- self . del = function ( id ) {
99
- // startMeasure("delete");
100
- var tmp = self . data ( ) ;
101
- const idx = tmp . findIndex ( d => d . id === id ) ;
102
- self . data . splice ( idx , 1 ) ;
103
- // stopMeasure();
104
- } ;
105
- } ;
106
-
107
- var ItemViewModel = function ( data , parent ) {
108
- var self = this ;
109
-
110
- self . id = data . id ;
111
- self . label = observable ( data . label ) ;
71
+ // stopMeasure();
72
+ } ;
73
+
74
+ self . clear = function ( ) {
75
+ // startMeasure("clear");
76
+ self . data . removeAll ( ) ;
77
+ self . selected ( null ) ;
78
+ // stopMeasure();
79
+ } ;
80
+
81
+ self . swapRows = function ( ) {
82
+ // startMeasure("swapRows");
83
+ var tmp = self . data ( ) ;
84
+ if ( tmp . length > 998 ) {
85
+ var a = tmp [ 1 ] ;
86
+ tmp [ 1 ] = tmp [ 998 ] ;
87
+ tmp [ 998 ] = a ;
88
+ self . data ( tmp ) ;
89
+ }
90
+ // stopMeasure();
91
+ } ;
92
+
93
+ self . clickRow = function ( e , id ) {
94
+ if ( e . target . matches ( '.delete' ) ) {
95
+ // startMeasure("delete");
96
+ var tmp = self . data ( ) ;
97
+ const idx = tmp . findIndex ( d => d . id === id ) ;
98
+ self . data . splice ( idx , 1 ) ;
99
+ // stopMeasure();
100
+ } else {
101
+ // startMeasure("select");
102
+ self . selected ( id ) ;
103
+ // stopMeasure();
104
+ }
105
+ }
112
106
} ;
113
107
114
108
root ( function ( ) {
115
- document . getElementById ( 'main' ) . appendChild ( template ( new HomeViewModel ( ) ) )
109
+ document . getElementById ( 'main' ) . appendChild ( template ( new HomeViewModel ( ) ) )
116
110
} ) ;
0 commit comments