1
|
|
|
/** |
2
|
|
|
* Converts a string to it's actual value, if applicable |
3
|
|
|
* |
4
|
|
|
* @param {String} str |
5
|
|
|
*/ |
6
|
|
|
function strToValue( str ) |
7
|
|
|
{ |
8
|
|
|
if('true' === str.toLowerCase()) return true; |
|
|
|
|
9
|
|
|
if('false' === str.toLowerCase()) return false; |
|
|
|
|
10
|
|
|
if(!isNaN(str)) return parseFloat(str); |
|
|
|
|
11
|
|
|
return str; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Convert hyphened text to camelCase. |
16
|
|
|
* |
17
|
|
|
* @param {string} str |
18
|
|
|
* @returns {string} |
19
|
|
|
*/ |
20
|
|
|
function toCamelCase( str ) |
21
|
|
|
{ |
22
|
|
|
return str.replace(/-(.)/g,function(match){ |
23
|
|
|
return match[1].toUpperCase(); |
24
|
|
|
}); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Reads the element's 'miv-' attributes and returns their values as an object |
29
|
|
|
* |
30
|
|
|
* @param {DOMElement} el |
31
|
|
|
* @returns {Object} |
32
|
|
|
*/ |
33
|
|
|
function readAttributes( el ) |
34
|
|
|
{ |
35
|
|
|
var options = {}; |
36
|
|
|
$.each(el.attributes, function(i, attr){ |
37
|
|
|
if(/^miv-/.test(attr.name)) |
38
|
|
|
{ |
39
|
|
|
options[toCamelCase(attr.name.substr(4))] = strToValue(attr.value); |
40
|
|
|
} |
41
|
|
|
}); |
42
|
|
|
return options; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get the average value of all elements in the given array. |
47
|
|
|
* |
48
|
|
|
* @param {Array} arr |
49
|
|
|
* @returns {Number} |
50
|
|
|
*/ |
51
|
|
|
function average( arr ) |
52
|
|
|
{ |
53
|
|
|
var i = arr.length, sum = 0; |
54
|
|
|
while(i--) sum += parseFloat(arr[i]); |
|
|
|
|
55
|
|
|
return sum/arr.length; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the maximum value of all elements in the given array. |
60
|
|
|
* |
61
|
|
|
* @param {Array} arr |
62
|
|
|
* @returns {Number} |
63
|
|
|
*/ |
64
|
|
|
function max( arr ) |
65
|
|
|
{ |
66
|
|
|
var i = arr.length, maxval = arr[--i]; |
67
|
|
|
while(i--) if(arr[i] > maxval) maxval = arr[i]; |
|
|
|
|
68
|
|
|
return maxval; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the minimum value of all elements in the given array. |
73
|
|
|
* |
74
|
|
|
* @param {Array} arr |
75
|
|
|
* @returns {Number} |
76
|
|
|
*/ |
77
|
|
|
function min( arr ) |
78
|
|
|
{ |
79
|
|
|
var i = arr.length, minval = arr[--i]; |
80
|
|
|
while(i--) if(arr[i] < minval) minval = arr[i]; |
|
|
|
|
81
|
|
|
return minval; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Calculate the editor's height based on the number of lines & line height. |
86
|
|
|
* |
87
|
|
|
* @param {jQuery} $editor Ther editor wrapper element (PRE) |
88
|
|
|
* @returns {Number} |
89
|
|
|
*/ |
90
|
|
|
function getEditorHeight( $editor ) |
91
|
|
|
{ |
92
|
|
|
var height = 0; |
93
|
|
|
$editor.find('.ace_text-layer').children().each(function(){ |
94
|
|
|
height += $(this).height(); |
95
|
|
|
}); |
96
|
|
|
return height; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Convert a string like "3, 5-7" into an array of ranges in to form of |
101
|
|
|
* [ |
102
|
|
|
* {start:2, end:2}, |
103
|
|
|
* {start:4, end:6}, |
104
|
|
|
* ] |
105
|
|
|
* The string should be given as a list if comma delimited 1 based ranges. |
106
|
|
|
* The result is given as a 0 based array of ranges. |
107
|
|
|
* |
108
|
|
|
* @param {string} str |
109
|
|
|
* @returns {Array} |
110
|
|
|
*/ |
111
|
|
|
function strToRange( str ) |
112
|
|
|
{ |
113
|
|
|
var range = str.replace(' ', '').split(','), |
114
|
|
|
i = range.length, |
115
|
|
|
ranges = [], |
116
|
|
|
start, end, splitted; |
117
|
|
|
|
118
|
|
|
while(i--) |
119
|
|
|
{ |
120
|
|
|
// Multiple lines highlight |
121
|
|
|
if( range[i].indexOf('-') > -1 ) |
122
|
|
|
{ |
123
|
|
|
splitted = range[i].split('-'); |
124
|
|
|
start = parseInt(splitted[0])-1; |
125
|
|
|
end = parseInt(splitted[1])-1; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Single line highlight |
129
|
|
|
else |
130
|
|
|
{ |
131
|
|
|
start = parseInt(range[i])-1; |
132
|
|
|
end = start; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
ranges.unshift({start:start,end:end}); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return ranges; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Request animation frame. Uses setTimeout as a fallback if the browser does |
143
|
|
|
* not support requestAnimationFrame (based on 60 frames per second). |
144
|
|
|
* |
145
|
|
|
* @param {type} cb |
146
|
|
|
* @returns {Number} |
147
|
|
|
*/ |
148
|
|
|
var raf = window.requestAnimationFrame || |
149
|
|
|
window.webkitRequestAnimationFrame || |
150
|
|
|
window.mozRequestAnimationFrame || |
151
|
|
|
window.msRequestAnimationFrame || |
152
|
|
|
function(cb) { return window.setTimeout(cb, 1000 / 60); }; |
153
|
|
|
|
154
|
|
|
/* test-code */ |
155
|
|
|
testapi.strToValue = strToValue; |
|
|
|
|
156
|
|
|
testapi.toCamelCase = toCamelCase; |
157
|
|
|
testapi.readAttributes = readAttributes; |
158
|
|
|
testapi.average = average; |
159
|
|
|
testapi.max = max; |
160
|
|
|
testapi.min = min; |
161
|
|
|
testapi.getEditorHeight = getEditorHeight; |
162
|
|
|
testapi.strToRange = strToRange; |
163
|
|
|
testapi.raf = raf; |
164
|
|
|
/* end-test-code */ |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.