1
- var $serial = { } ;
1
+ var $serial = ( typeof ( exports ) !== 'undefined' ) ? exports : { } ;
2
2
( function ( ) {
3
3
"use strict" ;
4
4
5
5
// We need this so we can test locally and still save layouts to AWS
6
6
$serial . base_href = "http://www.keyboard-layout-editor.com" ;
7
7
8
+ // Helper to copy an object; doesn't handle loops/circular refs, etc.
9
+ function copy ( o ) {
10
+ if ( typeof ( o ) !== 'object' ) {
11
+ // primitive value
12
+ return o ;
13
+ } else if ( o instanceof Array ) {
14
+ // array
15
+ var result = [ ] ;
16
+ for ( var i = 0 ; i < o . length ; i ++ ) {
17
+ result [ i ] = copy ( o [ i ] ) ;
18
+ }
19
+ return result ;
20
+ } else {
21
+ // Object
22
+ var result = { } ;
23
+ for ( var prop in o ) {
24
+ result [ prop ] = copy ( o [ prop ] ) ;
25
+ }
26
+ return result ;
27
+ }
28
+ }
29
+ function isEmptyObject ( o ) {
30
+ for ( var prop in o )
31
+ return false ;
32
+ return true ;
33
+ }
34
+
8
35
// Lenient JSON reader/writer
9
36
$serial . toJsonL = function ( obj ) {
10
37
var res = [ ] , key ;
11
38
if ( obj instanceof Array ) {
12
39
obj . forEach ( function ( elem ) { res . push ( $serial . toJsonL ( elem ) ) ; } ) ;
13
40
return '[' + res . join ( ',' ) + ']' ;
14
- }
41
+ }
15
42
if ( typeof obj === 'object' ) {
16
43
for ( key in obj ) { if ( obj . hasOwnProperty ( key ) ) { res . push ( key + ':' + $serial . toJsonL ( obj [ key ] ) ) ; } }
17
44
return '{' + res . join ( ',' ) + '}' ;
18
45
}
19
46
if ( typeof obj === 'number' ) {
20
47
return Math . round10 ( obj , - 4 ) ;
21
48
}
22
- return angular . toJson ( obj ) ;
49
+ return angular . toJson ( obj ) ;
23
50
} ;
24
51
$serial . fromJsonL = function ( json ) { return jsonl . parse ( json ) ; } ;
25
52
26
53
// function to sort the key array
27
54
$serial . sortKeys = function ( keys ) {
28
55
keys . sort ( function ( a , b ) {
29
- return ( ( a . rotation_angle + 360 ) % 360 - ( b . rotation_angle + 360 ) % 360 ) ||
30
- ( a . rotation_x - b . rotation_x ) ||
31
- ( a . rotation_y - b . rotation_y ) ||
32
- ( a . y - b . y ) ||
33
- ( a . x - b . x ) ;
56
+ return ( ( a . rotation_angle + 360 ) % 360 - ( b . rotation_angle + 360 ) % 360 ) ||
57
+ ( a . rotation_x - b . rotation_x ) ||
58
+ ( a . rotation_y - b . rotation_y ) ||
59
+ ( a . y - b . y ) ||
60
+ ( a . x - b . x ) ;
34
61
} ) ;
35
62
} ;
36
63
37
64
var _defaultKeyProps = {
38
65
x : 0 , y : 0 , x2 : 0 , y2 : 0 , // position
39
66
width : 1 , height : 1 , width2 : 1 , height2 : 1 , // size
40
67
color : "#cccccc" , text : [ "#000000" ] , // colors
41
- labels :[ ] , align : 4 , fontheight : 3 , fontheight2 : 3 , // label properties
68
+ labels :[ ] , align : 4 , fontheight : 3 , fontheight2 : 3 , // label properties
42
69
rotation_angle : 0 , rotation_x : 0 , rotation_y : 0 , // rotation
43
70
profile : "" , nub : false , ghost : false , stepped : false // misc
44
71
} ;
45
72
var _defaultMetaData = { backcolor : '#eeeeee' , name : '' , author : '' , notes : '' } ;
46
- $serial . defaultKeyProps = function ( ) { return angular . copy ( _defaultKeyProps ) ; } ;
47
- $serial . defaultMetaData = function ( ) { return angular . copy ( _defaultMetaData ) ; } ;
48
-
73
+ $serial . defaultKeyProps = function ( ) { return copy ( _defaultKeyProps ) ; } ;
74
+ $serial . defaultMetaData = function ( ) { return copy ( _defaultMetaData ) ; } ;
75
+
49
76
// Convert between our in-memory format & our serialized format
50
77
function serializeProp ( props , nname , val , defval ) { if ( val !== defval ) { props [ nname ] = val ; } return val ; }
51
78
$serial . serialize = function ( keyboard ) {
52
79
var keys = keyboard . keys ;
53
80
var rows = [ ] , row = [ ] ;
54
- var current = $serial . defaultKeyProps ( ) ;
81
+ var current = $serial . defaultKeyProps ( ) ;
55
82
current . text = current . text [ 0 ] ;
56
83
var cluster = { r :0 , rx :0 , ry :0 } ;
57
84
@@ -60,7 +87,7 @@ var $serial = {};
60
87
for ( var metakey in keyboard . meta ) {
61
88
serializeProp ( meta , metakey , keyboard . meta [ metakey ] , _defaultMetaData [ metakey ] ) ;
62
89
}
63
- if ( ! $ . isEmptyObject ( meta ) ) {
90
+ if ( ! isEmptyObject ( meta ) ) {
64
91
rows . push ( meta ) ;
65
92
}
66
93
@@ -76,7 +103,7 @@ var $serial = {};
76
103
// start a new row when necessary
77
104
var clusterChanged = ( key . rotation_angle != cluster . r || key . rotation_x != cluster . rx || key . rotation_y != cluster . ry ) ;
78
105
var rowChanged = ( key . y !== current . y ) ;
79
- if ( row . length > 0 && ( rowChanged || clusterChanged ) ) {
106
+ if ( row . length > 0 && ( rowChanged || clusterChanged ) ) {
80
107
// Push the old row
81
108
rows . push ( row ) ;
82
109
row = [ ] ;
@@ -85,11 +112,11 @@ var $serial = {};
85
112
86
113
if ( newRow ) {
87
114
// Set up for the new row
88
- current . y ++ ;
115
+ current . y ++ ;
89
116
90
117
// 'y' is reset if *either* 'rx' or 'ry' are changed
91
118
if ( key . rotation_y != cluster . ry || key . rotation_x != cluster . rx )
92
- current . y = key . rotation_y ;
119
+ current . y = key . rotation_y ;
93
120
current . x = key . rotation_x ; // always reset x to rx (which defaults to zero)
94
121
95
122
// Update current cluster
@@ -154,7 +181,7 @@ var $serial = {};
154
181
for ( var k = 0 ; k < rows [ r ] . length ; ++ k ) {
155
182
var key = rows [ r ] [ k ] ;
156
183
if ( typeof key === 'string' ) {
157
- var newKey = angular . copy ( current ) ;
184
+ var newKey = copy ( current ) ;
158
185
newKey . width2 = newKey . width2 === 0 ? current . width : current . width2 ;
159
186
newKey . height2 = newKey . height2 === 0 ? current . height : current . height2 ;
160
187
newKey . labels = key . split ( '\n' ) ;
@@ -199,7 +226,7 @@ var $serial = {};
199
226
}
200
227
return { meta :meta , keys :keys } ;
201
228
}
202
-
229
+
203
230
$serial . saveLayout = function ( $http , layout , success , error ) {
204
231
var data = angular . toJson ( layout ) ;
205
232
var fn = CryptoJS . MD5 ( data ) . toString ( ) ;
@@ -221,7 +248,7 @@ var $serial = {};
221
248
transformRequest : angular . identity
222
249
} ) . success ( function ( ) { success ( fn ) ; } ) . error ( function ( data , status ) {
223
250
if ( status == 0 ) {
224
- // We seem to get a 'cancelled' notification even though the POST
251
+ // We seem to get a 'cancelled' notification even though the POST
225
252
// is successful, so we have to double-check.
226
253
$http . get ( $serial . base_href + "/layouts/" + fn ) . success ( function ( ) { success ( fn ) ; } ) . error ( error ) ;
227
254
} else {
@@ -230,5 +257,4 @@ var $serial = {};
230
257
} ) ;
231
258
} ) ;
232
259
} ;
233
-
234
260
} ( ) ) ;
0 commit comments