nitdoc: merge js utils and base64 into lib/utils
authorAlexandre Terrasa <alexandre@moz-code.org>
Tue, 25 Feb 2014 17:46:51 +0000 (12:46 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 25 Feb 2014 17:46:51 +0000 (12:46 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

share/nitdoc/js/lib/base64.js [deleted file]
share/nitdoc/js/lib/github-api.js
share/nitdoc/js/lib/utils.js [new file with mode: 0644]
share/nitdoc/js/plugins/filtering.js
share/nitdoc/js/plugins/github.js
share/nitdoc/js/plugins/quicksearch.js
share/nitdoc/js/plugins/utils.js [deleted file]

diff --git a/share/nitdoc/js/lib/base64.js b/share/nitdoc/js/lib/base64.js
deleted file mode 100644 (file)
index 6fcda66..0000000
+++ /dev/null
@@ -1,137 +0,0 @@
-define(function() {
-       var Base64 = {
-
-               // private property
-               _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
-
-               // public method for encoding
-               encode : function (input) {
-                       var output = "";
-                       var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
-                       var i = 0;
-
-                       input = Base64._utf8_encode(input);
-
-                       while (i < input.length) {
-
-                               chr1 = input.charCodeAt(i++);
-                               chr2 = input.charCodeAt(i++);
-                               chr3 = input.charCodeAt(i++);
-
-                               enc1 = chr1 >> 2;
-                               enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
-                               enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
-                               enc4 = chr3 & 63;
-
-                               if (isNaN(chr2)) {
-                                       enc3 = enc4 = 64;
-                               } else if (isNaN(chr3)) {
-                                       enc4 = 64;
-                               }
-
-                               output = output +
-                               this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
-                               this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
-
-                       }
-
-                       return output;
-               },
-
-               // public method for decoding
-               decode : function (input) {
-                       var output = "";
-                       var chr1, chr2, chr3;
-                       var enc1, enc2, enc3, enc4;
-                       var i = 0;
-
-                       input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
-
-                       while (i < input.length) {
-
-                               enc1 = this._keyStr.indexOf(input.charAt(i++));
-                               enc2 = this._keyStr.indexOf(input.charAt(i++));
-                               enc3 = this._keyStr.indexOf(input.charAt(i++));
-                               enc4 = this._keyStr.indexOf(input.charAt(i++));
-
-                               chr1 = (enc1 << 2) | (enc2 >> 4);
-                               chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
-                               chr3 = ((enc3 & 3) << 6) | enc4;
-
-                               output = output + String.fromCharCode(chr1);
-
-                               if (enc3 != 64) {
-                                       output = output + String.fromCharCode(chr2);
-                               }
-                               if (enc4 != 64) {
-                                       output = output + String.fromCharCode(chr3);
-                               }
-
-                       }
-
-                       output = Base64._utf8_decode(output);
-
-                       return output;
-
-               },
-
-               // private method for UTF-8 encoding
-               _utf8_encode : function (string) {
-                       string = string.replace(/\r\n/g,"\n");
-                       var utftext = "";
-
-                       for (var n = 0; n < string.length; n++) {
-
-                               var c = string.charCodeAt(n);
-
-                               if (c < 128) {
-                                       utftext += String.fromCharCode(c);
-                               }
-                               else if((c > 127) && (c < 2048)) {
-                                       utftext += String.fromCharCode((c >> 6) | 192);
-                                       utftext += String.fromCharCode((c & 63) | 128);
-                               }
-                               else {
-                                       utftext += String.fromCharCode((c >> 12) | 224);
-                                       utftext += String.fromCharCode(((c >> 6) & 63) | 128);
-                                       utftext += String.fromCharCode((c & 63) | 128);
-                               }
-
-                       }
-
-                       return utftext;
-               },
-
-               // private method for UTF-8 decoding
-               _utf8_decode : function (utftext) {
-                       var string = "";
-                       var i = 0;
-                       var c = c1 = c2 = 0;
-
-                       while ( i < utftext.length ) {
-
-                               c = utftext.charCodeAt(i);
-
-                               if (c < 128) {
-                                       string += String.fromCharCode(c);
-                                       i++;
-                               }
-                               else if((c > 191) && (c < 224)) {
-                                       c2 = utftext.charCodeAt(i+1);
-                                       string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
-                                       i += 2;
-                               }
-                               else {
-                                       c2 = utftext.charCodeAt(i+1);
-                                       c3 = utftext.charCodeAt(i+2);
-                                       string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
-                                       i += 3;
-                               }
-
-                       }
-
-                       return string;
-               }
-       }
-       return Base64;
-});
index d83d4bf..f03188a 100644 (file)
@@ -21,8 +21,8 @@
  */\r
 define([\r
        "jquery",\r
-       "base64"\r
-], function($, Base64) {\r
+       "utils"\r
+], function($, Utils) {\r
        return {\r
 \r
                // try to login the user to github API\r
@@ -148,7 +148,7 @@ define([
                                async: false,\r
                                dataType: 'json',\r
                                data: JSON.stringify({\r
-                                       content: Base64.encode(content),\r
+                                       content: content.base64Encode(),\r
                                        encoding: "base64"\r
                                }),\r
                                success: function(response) {\r
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
index 27cc8ed..1bb6e20 100644 (file)
@@ -24,7 +24,7 @@
 define([\r
        "jquery",\r
        'jQueryUI',\r
-       "plugins/utils"\r
+       "utils"\r
 ], function($, UI, Utils) {\r
 \r
        var Filtering = {\r
index 9a82274..5b8ca2f 100644 (file)
  */\r
 define([\r
        "jquery",\r
-       "base64",\r
        "github-api",\r
        "plugins/github/loginbox",\r
        "plugins/github/modalbox",\r
        "plugins/github/commentbox",\r
-       "Markdown.Converter",\r
-], function($, Base64, GithubAPI, LoginBox, ModalBox, CommentBox) {\r
+       "utils",\r
+       "Markdown.Converter"\r
+], function($, GithubAPI, LoginBox, ModalBox, CommentBox) {\r
        var GithubUser = function(login, password, repo, branch) {\r
                this.login = login;\r
                this.password = password;\r
                this.repo = repo;\r
-               this.auth = "Basic " +  Base64.encode(login + ':' + password);\r
+               this.auth = "Basic " +  (login + ':' + password).base64Encode();\r
                this.branch = branch;\r
        }\r
 \r
@@ -139,7 +139,7 @@ define([
                                var session = JSON.parse(localStorage.user);\r
                                var isok = this._tryLogin(\r
                                        session.login,\r
-                                       Base64.decode(session.password),\r
+                                       session.password.base64Decode(),\r
                                        session.repo,\r
                                        session.branch\r
                                );\r
@@ -173,7 +173,7 @@ define([
                _saveSession: function(user) {\r
                        localStorage.user = JSON.stringify({\r
                                login: user.login,\r
-                               password: Base64.encode(user.password),\r
+                               password: user.password.base64Encode(),\r
                                repo: user.repo,\r
                                branch: user.branch,\r
                        });\r
@@ -248,7 +248,7 @@ define([
                                var request = requests[i];\r
                                $("textarea[data-comment-location=\"" + request.location + "\"]").each(function () {\r
                                        if(request.isClosed) {\r
-                                               var oldComment = Base64.decode(request.oldComment);\r
+                                               var oldComment = request.oldComment.base64Decode();\r
                                                var htmlComment = converter.makeHtml(oldComment);\r
                                                $(this).val(oldComment);\r
                                                if(!$(this).val()) {\r
@@ -259,7 +259,7 @@ define([
                                                $(this).nextAll("div.comment").find("div.nitdoc").empty().html(htmlComment);\r
                                                $(this).nextAll("p.info").find("a.nitdoc-github-editComment").show();\r
                                        } else {\r
-                                               var newComment = Base64.decode(request.comment);\r
+                                               var newComment = request.comment.base64Decode();\r
                                                var htmlComment = converter.makeHtml(newComment);\r
                                                $(this).val(newComment);\r
                                                if(!$(this).val()) {\r
@@ -331,8 +331,8 @@ define([
                        requests[edit.request.number] = {\r
                                request: edit.request,\r
                                location: edit.location.origin,\r
-                               comment: Base64.encode(edit.newComment),\r
-                               oldComment: Base64.encode(edit.oldComment)\r
+                               comment: edit.newComment.base64Encode(),\r
+                               oldComment: edit.oldComment.base64Encode()\r
                        };\r
                        localStorage.requests = JSON.stringify(requests);\r
                },\r
@@ -417,7 +417,7 @@ define([
                                return;\r
                        }\r
                        var base64Content = origFile.content.substring(0, origFile.content.length - 1)\r
-                       return Base64.decode(base64Content);\r
+                       return base64Content.base64Decode();\r
                },\r
 \r
                _mergeComment: function(fileContent, comment, location) {\r
index 32204a4..c4cd682 100644 (file)
@@ -22,7 +22,7 @@
 define([
        "jquery",
        "jQueryUI",
-       "plugins/utils",
+       "utils",
        "quicksearchList",
 ], function($, ui, utils) {
        $.widget("nitdoc.quicksearch", {
diff --git a/share/nitdoc/js/plugins/utils.js b/share/nitdoc/js/plugins/utils.js
deleted file mode 100644 (file)
index 397ec12..0000000
+++ /dev/null
@@ -1,83 +0,0 @@
-/* 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
-       // 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