nitdoc: move css files to share/css/ dir
[nit.git] / share / nitdoc / js / lib / utils.js
1 /* This file is part of NIT ( http://www.nitlanguage.org ).
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14
15 Documentation generator for the nit language.
16 Generate API documentation in HTML format from nit source code.
17 */
18
19 /*
20 * Utils module
21 */
22 define([
23 "jquery",
24 ], function($) {
25
26 String.prototype.startsWith = function(prefix, caseSensitive) {
27 if(caseSensitive) {
28 return this.toUpperCase().indexOf(prefix.toUpperCase()) === 0;
29 }
30 return this.indexOf(prefix) === 0;
31 }
32
33 // Compare two strings using Sorensen-Dice Coefficient
34 // see: http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
35 String.prototype.dice = function(other) {
36 var length1 = this.length - 1;
37 var length2 = other.length - 1;
38 if(length1 < 1 || length2 < 1) return 0;
39
40 var bigrams2 = [];
41 for(var i = 0; i < length2; i++) {
42 bigrams2.push(other.substr(i, 2));
43 }
44
45 var intersection = 0;
46 for(var i = 0; i < length1; i++) {
47 var bigram1 = this.substr(i, 2);
48 for(var j = 0; j < length2; j++) {
49 if(bigram1 == bigrams2[j]) {
50 intersection++;
51 bigrams2[j] = null;
52 break;
53 }
54 }
55 }
56 return (2.0 * intersection) / (length1 + length2);
57 }
58
59 /* base64 */
60
61 String.prototype._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
62
63 // public method for encoding
64 String.prototype.base64Encode = function () {
65 var output = "";
66 var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
67 var i = 0;
68
69 input = this._utf8_encode();
70
71 while (i < input.length) {
72
73 chr1 = input.charCodeAt(i++);
74 chr2 = input.charCodeAt(i++);
75 chr3 = input.charCodeAt(i++);
76
77 enc1 = chr1 >> 2;
78 enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
79 enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
80 enc4 = chr3 & 63;
81
82 if (isNaN(chr2)) {
83 enc3 = enc4 = 64;
84 } else if (isNaN(chr3)) {
85 enc4 = 64;
86 }
87
88 output = output +
89 this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
90 this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
91
92 }
93 return output;
94 };
95
96 // public method for decoding
97 String.prototype.base64Decode = function () {
98 var output = "";
99 var chr1, chr2, chr3;
100 var enc1, enc2, enc3, enc4;
101 var i = 0;
102
103 input = this.replace(/[^A-Za-z0-9\+\/\=]/g, "");
104
105 while (i < input.length) {
106
107 enc1 = this._keyStr.indexOf(input.charAt(i++));
108 enc2 = this._keyStr.indexOf(input.charAt(i++));
109 enc3 = this._keyStr.indexOf(input.charAt(i++));
110 enc4 = this._keyStr.indexOf(input.charAt(i++));
111
112 chr1 = (enc1 << 2) | (enc2 >> 4);
113 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
114 chr3 = ((enc3 & 3) << 6) | enc4;
115
116 output = output + String.fromCharCode(chr1);
117
118 if (enc3 != 64) {
119 output = output + String.fromCharCode(chr2);
120 }
121 if (enc4 != 64) {
122 output = output + String.fromCharCode(chr3);
123 }
124
125 }
126 return output._utf8_decode();;
127 };
128
129 // private method for UTF-8 encoding
130 String.prototype._utf8_encode = function () {
131 string = this.replace(/\r\n/g,"\n");
132 var utftext = "";
133
134 for (var n = 0; n < string.length; n++) {
135
136 var c = string.charCodeAt(n);
137
138 if (c < 128) {
139 utftext += String.fromCharCode(c);
140 }
141 else if((c > 127) && (c < 2048)) {
142 utftext += String.fromCharCode((c >> 6) | 192);
143 utftext += String.fromCharCode((c & 63) | 128);
144 }
145 else {
146 utftext += String.fromCharCode((c >> 12) | 224);
147 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
148 utftext += String.fromCharCode((c & 63) | 128);
149 }
150
151 }
152 return utftext;
153 };
154
155 // private method for UTF-8 decoding
156 String.prototype._utf8_decode = function () {
157 var string = "";
158 var i = 0;
159 var c = c1 = c2 = 0;
160
161 while ( i < this.length ) {
162
163 c = this.charCodeAt(i);
164
165 if (c < 128) {
166 string += String.fromCharCode(c);
167 i++;
168 }
169 else if((c > 191) && (c < 224)) {
170 c2 = this.charCodeAt(i+1);
171 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
172 i += 2;
173 }
174 else {
175 c2 = this.charCodeAt(i+1);
176 c3 = this.charCodeAt(i+2);
177 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
178 i += 3;
179 }
180
181 }
182 return string;
183 };
184
185 // JQuery Case Insensitive :icontains selector
186 $.expr[':'].icontains = function(obj, index, meta, stack){
187 return (obj.textContent.replace(/\[[0-9]+\]/g, "") || obj.innerText.replace(/\[[0-9]+\]/g, "") || jQuery(obj).text().replace(/\[[0-9]+\]/g, "") || '').toLowerCase().indexOf(meta[3].toLowerCase()) >= 0;
188 };
189
190 return {
191 // Extract anchor part (after #) from URL string
192 extractAnchor: function(url) {
193 var index = url.indexOf("#");
194 if (index >= 0) {
195 return url.substring(index + 1);
196 }
197 return null;
198 },
199
200 delayEvent: function(handler, event) {
201 if(this.delayEvent.timeout) {
202 clearTimeout(this.delayEvent.timeout);
203 }
204 this.delayEvent.timeout = setTimeout(function() {
205 handler.call(event);
206 }, 50);
207 }
208 };
209 });