nitdoc: merge js utils and base64 into lib/utils
[nit.git] / share / nitdoc / js / lib / utils.js
diff --git a/share/nitdoc/js/lib/utils.js b/share/nitdoc/js/lib/utils.js
new file mode 100644 (file)
index 0000000..43d437d
--- /dev/null
@@ -0,0 +1,209 @@
+/* This file is part of NIT ( http://www.nitlanguage.org ).\r
+\r
+   Licensed under the Apache License, Version 2.0 (the "License");\r
+   you may not use this file except in compliance with the License.\r
+   You may obtain a copy of the License at\r
+\r
+   http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+\r
+   Documentation generator for the nit language.\r
+   Generate API documentation in HTML format from nit source code.\r
+*/\r
+\r
+/*\r
+ * Utils module\r
+ */\r
+define([\r
+       "jquery",\r
+], function($) {\r
+\r
+       String.prototype.startsWith = function(prefix, caseSensitive) {\r
+               if(caseSensitive) {\r
+                       return this.toUpperCase().indexOf(prefix.toUpperCase()) === 0;\r
+               }\r
+               return this.indexOf(prefix) === 0;\r
+       }\r
+\r
+       // Compare two strings using Sorensen-Dice Coefficient\r
+       // see: http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient\r
+       String.prototype.dice = function(other) {\r
+               var length1 = this.length - 1;\r
+               var length2 = other.length - 1;\r
+               if(length1 < 1 || length2 < 1) return 0;\r
+\r
+               var bigrams2 = [];\r
+               for(var i = 0; i < length2; i++) {\r
+                       bigrams2.push(other.substr(i, 2));\r
+               }\r
+\r
+               var intersection = 0;\r
+               for(var i = 0; i < length1; i++) {\r
+                       var bigram1 = this.substr(i, 2);\r
+                       for(var j = 0; j < length2; j++) {\r
+                               if(bigram1 == bigrams2[j]) {\r
+                                       intersection++;\r
+                                       bigrams2[j] = null;\r
+                                       break;\r
+                               }\r
+                       }\r
+               }\r
+               return (2.0 * intersection) / (length1 + length2);\r
+       }\r
+\r
+       /* base64 */\r
+\r
+       String.prototype._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";\r
+\r
+       // public method for encoding\r
+       String.prototype.base64Encode = function () {\r
+               var output = "";\r
+               var chr1, chr2, chr3, enc1, enc2, enc3, enc4;\r
+               var i = 0;\r
+\r
+               input = this._utf8_encode();\r
+\r
+               while (i < input.length) {\r
+\r
+                       chr1 = input.charCodeAt(i++);\r
+                       chr2 = input.charCodeAt(i++);\r
+                       chr3 = input.charCodeAt(i++);\r
+\r
+                       enc1 = chr1 >> 2;\r
+                       enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);\r
+                       enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);\r
+                       enc4 = chr3 & 63;\r
+\r
+                       if (isNaN(chr2)) {\r
+                               enc3 = enc4 = 64;\r
+                       } else if (isNaN(chr3)) {\r
+                               enc4 = 64;\r
+                       }\r
+\r
+                       output = output +\r
+                       this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +\r
+                       this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);\r
+\r
+               }\r
+               return output;\r
+       };\r
+\r
+       // public method for decoding\r
+       String.prototype.base64Decode = function () {\r
+               var output = "";\r
+               var chr1, chr2, chr3;\r
+               var enc1, enc2, enc3, enc4;\r
+               var i = 0;\r
+\r
+               input = this.replace(/[^A-Za-z0-9\+\/\=]/g, "");\r
+\r
+               while (i < input.length) {\r
+\r
+                       enc1 = this._keyStr.indexOf(input.charAt(i++));\r
+                       enc2 = this._keyStr.indexOf(input.charAt(i++));\r
+                       enc3 = this._keyStr.indexOf(input.charAt(i++));\r
+                       enc4 = this._keyStr.indexOf(input.charAt(i++));\r
+\r
+                       chr1 = (enc1 << 2) | (enc2 >> 4);\r
+                       chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);\r
+                       chr3 = ((enc3 & 3) << 6) | enc4;\r
+\r
+                       output = output + String.fromCharCode(chr1);\r
+\r
+                       if (enc3 != 64) {\r
+                               output = output + String.fromCharCode(chr2);\r
+                       }\r
+                       if (enc4 != 64) {\r
+                               output = output + String.fromCharCode(chr3);\r
+                       }\r
+\r
+               }\r
+               return output._utf8_decode();;\r
+       };\r
+\r
+       // private method for UTF-8 encoding\r
+       String.prototype._utf8_encode = function () {\r
+               string = this.replace(/\r\n/g,"\n");\r
+               var utftext = "";\r
+\r
+               for (var n = 0; n < string.length; n++) {\r
+\r
+                       var c = string.charCodeAt(n);\r
+\r
+                       if (c < 128) {\r
+                               utftext += String.fromCharCode(c);\r
+                       }\r
+                       else if((c > 127) && (c < 2048)) {\r
+                               utftext += String.fromCharCode((c >> 6) | 192);\r
+                               utftext += String.fromCharCode((c & 63) | 128);\r
+                       }\r
+                       else {\r
+                               utftext += String.fromCharCode((c >> 12) | 224);\r
+                               utftext += String.fromCharCode(((c >> 6) & 63) | 128);\r
+                               utftext += String.fromCharCode((c & 63) | 128);\r
+                       }\r
+\r
+               }\r
+               return utftext;\r
+       };\r
+\r
+       // private method for UTF-8 decoding\r
+       String.prototype._utf8_decode = function () {\r
+               var string = "";\r
+               var i = 0;\r
+               var c = c1 = c2 = 0;\r
+\r
+               while ( i < this.length ) {\r
+\r
+                       c = this.charCodeAt(i);\r
+\r
+                       if (c < 128) {\r
+                               string += String.fromCharCode(c);\r
+                               i++;\r
+                       }\r
+                       else if((c > 191) && (c < 224)) {\r
+                               c2 = this.charCodeAt(i+1);\r
+                               string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));\r
+                               i += 2;\r
+                       }\r
+                       else {\r
+                               c2 = this.charCodeAt(i+1);\r
+                               c3 = this.charCodeAt(i+2);\r
+                               string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));\r
+                               i += 3;\r
+                       }\r
+\r
+               }\r
+               return string;\r
+       };\r
+\r
+       // JQuery Case Insensitive :icontains selector\r
+       $.expr[':'].icontains = function(obj, index, meta, stack){\r
+               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;\r
+       };\r
+\r
+       return {\r
+               // Extract anchor part (after #) from URL string\r
+               extractAnchor: function(url) {\r
+                       var index = url.indexOf("#");\r
+                       if (index >= 0) {\r
+                               return url.substring(index + 1);\r
+                       }\r
+                       return null;\r
+               },\r
+\r
+               delayEvent: function(handler, event) {\r
+                       if(this.delayEvent.timeout) {\r
+                               clearTimeout(this.delayEvent.timeout);\r
+                       }\r
+                       this.delayEvent.timeout = setTimeout(function() {\r
+                           handler.call(event);\r
+                       }, 50);\r
+               }\r
+       };\r
+});\r