From a060f749c089f9713399c9455be04d4c129dce17 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Tue, 25 Feb 2014 12:46:51 -0500 Subject: [PATCH] nitdoc: merge js utils and base64 into lib/utils Signed-off-by: Alexandre Terrasa --- share/nitdoc/js/lib/base64.js | 137 --------------------- share/nitdoc/js/lib/github-api.js | 6 +- share/nitdoc/js/lib/utils.js | 209 ++++++++++++++++++++++++++++++++ share/nitdoc/js/plugins/filtering.js | 2 +- share/nitdoc/js/plugins/github.js | 22 ++-- share/nitdoc/js/plugins/quicksearch.js | 2 +- share/nitdoc/js/plugins/utils.js | 83 ------------- 7 files changed, 225 insertions(+), 236 deletions(-) delete mode 100644 share/nitdoc/js/lib/base64.js create mode 100644 share/nitdoc/js/lib/utils.js delete mode 100644 share/nitdoc/js/plugins/utils.js diff --git a/share/nitdoc/js/lib/base64.js b/share/nitdoc/js/lib/base64.js deleted file mode 100644 index 6fcda66..0000000 --- a/share/nitdoc/js/lib/base64.js +++ /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; -}); diff --git a/share/nitdoc/js/lib/github-api.js b/share/nitdoc/js/lib/github-api.js index d83d4bf..f03188a 100644 --- a/share/nitdoc/js/lib/github-api.js +++ b/share/nitdoc/js/lib/github-api.js @@ -21,8 +21,8 @@ */ define([ "jquery", - "base64" -], function($, Base64) { + "utils" +], function($, Utils) { return { // try to login the user to github API @@ -148,7 +148,7 @@ define([ async: false, dataType: 'json', data: JSON.stringify({ - content: Base64.encode(content), + content: content.base64Encode(), encoding: "base64" }), success: function(response) { diff --git a/share/nitdoc/js/lib/utils.js b/share/nitdoc/js/lib/utils.js new file mode 100644 index 0000000..43d437d --- /dev/null +++ b/share/nitdoc/js/lib/utils.js @@ -0,0 +1,209 @@ +/* This file is part of NIT ( http://www.nitlanguage.org ). + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + Documentation generator for the nit language. + Generate API documentation in HTML format from nit source code. +*/ + +/* + * Utils module + */ +define([ + "jquery", +], function($) { + + String.prototype.startsWith = function(prefix, caseSensitive) { + if(caseSensitive) { + return this.toUpperCase().indexOf(prefix.toUpperCase()) === 0; + } + return this.indexOf(prefix) === 0; + } + + // Compare two strings using Sorensen-Dice Coefficient + // see: http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient + String.prototype.dice = function(other) { + var length1 = this.length - 1; + var length2 = other.length - 1; + if(length1 < 1 || length2 < 1) return 0; + + var bigrams2 = []; + for(var i = 0; i < length2; i++) { + bigrams2.push(other.substr(i, 2)); + } + + var intersection = 0; + for(var i = 0; i < length1; i++) { + var bigram1 = this.substr(i, 2); + for(var j = 0; j < length2; j++) { + if(bigram1 == bigrams2[j]) { + intersection++; + bigrams2[j] = null; + break; + } + } + } + return (2.0 * intersection) / (length1 + length2); + } + + /* base64 */ + + String.prototype._keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; + + // public method for encoding + String.prototype.base64Encode = function () { + var output = ""; + var chr1, chr2, chr3, enc1, enc2, enc3, enc4; + var i = 0; + + input = this._utf8_encode(); + + 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 + String.prototype.base64Decode = function () { + var output = ""; + var chr1, chr2, chr3; + var enc1, enc2, enc3, enc4; + var i = 0; + + input = this.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); + } + + } + return output._utf8_decode();; + }; + + // private method for UTF-8 encoding + String.prototype._utf8_encode = function () { + string = this.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 + String.prototype._utf8_decode = function () { + var string = ""; + var i = 0; + var c = c1 = c2 = 0; + + while ( i < this.length ) { + + c = this.charCodeAt(i); + + if (c < 128) { + string += String.fromCharCode(c); + i++; + } + else if((c > 191) && (c < 224)) { + c2 = this.charCodeAt(i+1); + string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); + i += 2; + } + else { + c2 = this.charCodeAt(i+1); + c3 = this.charCodeAt(i+2); + string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); + i += 3; + } + + } + return string; + }; + + // JQuery Case Insensitive :icontains selector + $.expr[':'].icontains = function(obj, index, meta, stack){ + 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; + }; + + return { + // Extract anchor part (after #) from URL string + extractAnchor: function(url) { + var index = url.indexOf("#"); + if (index >= 0) { + return url.substring(index + 1); + } + return null; + }, + + delayEvent: function(handler, event) { + if(this.delayEvent.timeout) { + clearTimeout(this.delayEvent.timeout); + } + this.delayEvent.timeout = setTimeout(function() { + handler.call(event); + }, 50); + } + }; +}); diff --git a/share/nitdoc/js/plugins/filtering.js b/share/nitdoc/js/plugins/filtering.js index 27cc8ed..1bb6e20 100644 --- a/share/nitdoc/js/plugins/filtering.js +++ b/share/nitdoc/js/plugins/filtering.js @@ -24,7 +24,7 @@ define([ "jquery", 'jQueryUI', - "plugins/utils" + "utils" ], function($, UI, Utils) { var Filtering = { diff --git a/share/nitdoc/js/plugins/github.js b/share/nitdoc/js/plugins/github.js index 9a82274..5b8ca2f 100644 --- a/share/nitdoc/js/plugins/github.js +++ b/share/nitdoc/js/plugins/github.js @@ -23,18 +23,18 @@ */ define([ "jquery", - "base64", "github-api", "plugins/github/loginbox", "plugins/github/modalbox", "plugins/github/commentbox", - "Markdown.Converter", -], function($, Base64, GithubAPI, LoginBox, ModalBox, CommentBox) { + "utils", + "Markdown.Converter" +], function($, GithubAPI, LoginBox, ModalBox, CommentBox) { var GithubUser = function(login, password, repo, branch) { this.login = login; this.password = password; this.repo = repo; - this.auth = "Basic " + Base64.encode(login + ':' + password); + this.auth = "Basic " + (login + ':' + password).base64Encode(); this.branch = branch; } @@ -139,7 +139,7 @@ define([ var session = JSON.parse(localStorage.user); var isok = this._tryLogin( session.login, - Base64.decode(session.password), + session.password.base64Decode(), session.repo, session.branch ); @@ -173,7 +173,7 @@ define([ _saveSession: function(user) { localStorage.user = JSON.stringify({ login: user.login, - password: Base64.encode(user.password), + password: user.password.base64Encode(), repo: user.repo, branch: user.branch, }); @@ -248,7 +248,7 @@ define([ var request = requests[i]; $("textarea[data-comment-location=\"" + request.location + "\"]").each(function () { if(request.isClosed) { - var oldComment = Base64.decode(request.oldComment); + var oldComment = request.oldComment.base64Decode(); var htmlComment = converter.makeHtml(oldComment); $(this).val(oldComment); if(!$(this).val()) { @@ -259,7 +259,7 @@ define([ $(this).nextAll("div.comment").find("div.nitdoc").empty().html(htmlComment); $(this).nextAll("p.info").find("a.nitdoc-github-editComment").show(); } else { - var newComment = Base64.decode(request.comment); + var newComment = request.comment.base64Decode(); var htmlComment = converter.makeHtml(newComment); $(this).val(newComment); if(!$(this).val()) { @@ -331,8 +331,8 @@ define([ requests[edit.request.number] = { request: edit.request, location: edit.location.origin, - comment: Base64.encode(edit.newComment), - oldComment: Base64.encode(edit.oldComment) + comment: edit.newComment.base64Encode(), + oldComment: edit.oldComment.base64Encode() }; localStorage.requests = JSON.stringify(requests); }, @@ -417,7 +417,7 @@ define([ return; } var base64Content = origFile.content.substring(0, origFile.content.length - 1) - return Base64.decode(base64Content); + return base64Content.base64Decode(); }, _mergeComment: function(fileContent, comment, location) { diff --git a/share/nitdoc/js/plugins/quicksearch.js b/share/nitdoc/js/plugins/quicksearch.js index 32204a4..c4cd682 100644 --- a/share/nitdoc/js/plugins/quicksearch.js +++ b/share/nitdoc/js/plugins/quicksearch.js @@ -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 index 397ec12..0000000 --- a/share/nitdoc/js/plugins/utils.js +++ /dev/null @@ -1,83 +0,0 @@ -/* This file is part of NIT ( http://www.nitlanguage.org ). - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - Documentation generator for the nit language. - Generate API documentation in HTML format from nit source code. -*/ - -/* - * Utils module - */ -define([ - "jquery", -], function($) { - - String.prototype.startsWith = function(prefix, caseSensitive) { - if(caseSensitive) { - return this.toUpperCase().indexOf(prefix.toUpperCase()) === 0; - } - return this.indexOf(prefix) === 0; - } - - // Compare two strings using Sorensen-Dice Coefficient - // see: http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient - String.prototype.dice = function(other) { - var length1 = this.length - 1; - var length2 = other.length - 1; - if(length1 < 1 || length2 < 1) return 0; - - var bigrams2 = []; - for(var i = 0; i < length2; i++) { - bigrams2.push(other.substr(i, 2)); - } - - var intersection = 0; - for(var i = 0; i < length1; i++) { - var bigram1 = this.substr(i, 2); - for(var j = 0; j < length2; j++) { - if(bigram1 == bigrams2[j]) { - intersection++; - bigrams2[j] = null; - break; - } - } - } - return (2.0 * intersection) / (length1 + length2); - } - - // JQuery Case Insensitive :icontains selector - $.expr[':'].icontains = function(obj, index, meta, stack){ - 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; - }; - - return { - // Extract anchor part (after #) from URL string - extractAnchor: function(url) { - var index = url.indexOf("#"); - if (index >= 0) { - return url.substring(index + 1); - } - return null; - }, - - delayEvent: function(handler, event) { - if(this.delayEvent.timeout) { - clearTimeout(this.delayEvent.timeout); - } - this.delayEvent.timeout = setTimeout(function() { - handler.call(event); - }, 50); - } - }; -}); -- 1.7.9.5