From: Djomanix Date: Fri, 27 Jun 2014 19:59:08 +0000 (-0400) Subject: Added JS files. X-Git-Tag: v0.6.6~4^2~4 X-Git-Url: http://nitlanguage.org Added JS files. Signed-off-by: Djomanix --- diff --git a/contrib/online_ide/www/js/FileSaver.js b/contrib/online_ide/www/js/FileSaver.js new file mode 100644 index 0000000..6095017 --- /dev/null +++ b/contrib/online_ide/www/js/FileSaver.js @@ -0,0 +1,241 @@ +/* FileSaver.js + * A saveAs() FileSaver implementation. + * 2014-05-27 + * + * By Eli Grey, http://eligrey.com + * License: X11/MIT + * See https://github.com/eligrey/FileSaver.js/blob/master/LICENSE.md + */ + +/*global self */ +/*jslint bitwise: true, indent: 4, laxbreak: true, laxcomma: true, smarttabs: true, plusplus: true */ + +/*! @source http://purl.eligrey.com/github/FileSaver.js/blob/master/FileSaver.js */ + +var saveAs = saveAs + // IE 10+ (native saveAs) + || (typeof navigator !== "undefined" && + navigator.msSaveOrOpenBlob && navigator.msSaveOrOpenBlob.bind(navigator)) + // Everyone else + || (function(view) { + "use strict"; + // IE <10 is explicitly unsupported + if (typeof navigator !== "undefined" && + /MSIE [1-9]\./.test(navigator.userAgent)) { + return; + } + var + doc = view.document + // only get URL when necessary in case Blob.js hasn't overridden it yet + , get_URL = function() { + return view.URL || view.webkitURL || view; + } + , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a") + , can_use_save_link = !view.externalHost && "download" in save_link + , click = function(node) { + var event = doc.createEvent("MouseEvents"); + event.initMouseEvent( + "click", true, false, view, 0, 0, 0, 0, 0 + , false, false, false, false, 0, null + ); + node.dispatchEvent(event); + } + , webkit_req_fs = view.webkitRequestFileSystem + , req_fs = view.requestFileSystem || webkit_req_fs || view.mozRequestFileSystem + , throw_outside = function(ex) { + (view.setImmediate || view.setTimeout)(function() { + throw ex; + }, 0); + } + , force_saveable_type = "application/octet-stream" + , fs_min_size = 0 + , deletion_queue = [] + , process_deletion_queue = function() { + var i = deletion_queue.length; + while (i--) { + var file = deletion_queue[i]; + if (typeof file === "string") { // file is an object URL + get_URL().revokeObjectURL(file); + } else { // file is a File + file.remove(); + } + } + deletion_queue.length = 0; // clear queue + } + , dispatch = function(filesaver, event_types, event) { + event_types = [].concat(event_types); + var i = event_types.length; + while (i--) { + var listener = filesaver["on" + event_types[i]]; + if (typeof listener === "function") { + try { + listener.call(filesaver, event || filesaver); + } catch (ex) { + throw_outside(ex); + } + } + } + } + , FileSaver = function(blob, name) { + // First try a.download, then web filesystem, then object URLs + var + filesaver = this + , type = blob.type + , blob_changed = false + , object_url + , target_view + , get_object_url = function() { + var object_url = get_URL().createObjectURL(blob); + deletion_queue.push(object_url); + return object_url; + } + , dispatch_all = function() { + dispatch(filesaver, "writestart progress write writeend".split(" ")); + } + // on any filesys errors revert to saving with object URLs + , fs_error = function() { + // don't create more object URLs than needed + if (blob_changed || !object_url) { + object_url = get_object_url(blob); + } + if (target_view) { + target_view.location.href = object_url; + } else { + window.open(object_url, "_blank"); + } + filesaver.readyState = filesaver.DONE; + dispatch_all(); + } + , abortable = function(func) { + return function() { + if (filesaver.readyState !== filesaver.DONE) { + return func.apply(this, arguments); + } + }; + } + , create_if_not_found = {create: true, exclusive: false} + , slice + ; + filesaver.readyState = filesaver.INIT; + if (!name) { + name = "download"; + } + if (can_use_save_link) { + object_url = get_object_url(blob); + save_link.href = object_url; + save_link.download = name; + click(save_link); + filesaver.readyState = filesaver.DONE; + dispatch_all(); + return; + } + // Object and web filesystem URLs have a problem saving in Google Chrome when + // viewed in a tab, so I force save with application/octet-stream + // http://code.google.com/p/chromium/issues/detail?id=91158 + if (view.chrome && type && type !== force_saveable_type) { + slice = blob.slice || blob.webkitSlice; + blob = slice.call(blob, 0, blob.size, force_saveable_type); + blob_changed = true; + } + // Since I can't be sure that the guessed media type will trigger a download + // in WebKit, I append .download to the filename. + // https://bugs.webkit.org/show_bug.cgi?id=65440 + if (webkit_req_fs && name !== "download") { + name += ".download"; + } + if (type === force_saveable_type || webkit_req_fs) { + target_view = view; + } + if (!req_fs) { + fs_error(); + return; + } + fs_min_size += blob.size; + req_fs(view.TEMPORARY, fs_min_size, abortable(function(fs) { + fs.root.getDirectory("saved", create_if_not_found, abortable(function(dir) { + var save = function() { + dir.getFile(name, create_if_not_found, abortable(function(file) { + file.createWriter(abortable(function(writer) { + writer.onwriteend = function(event) { + target_view.location.href = file.toURL(); + deletion_queue.push(file); + filesaver.readyState = filesaver.DONE; + dispatch(filesaver, "writeend", event); + }; + writer.onerror = function() { + var error = writer.error; + if (error.code !== error.ABORT_ERR) { + fs_error(); + } + }; + "writestart progress write abort".split(" ").forEach(function(event) { + writer["on" + event] = filesaver["on" + event]; + }); + writer.write(blob); + filesaver.abort = function() { + writer.abort(); + filesaver.readyState = filesaver.DONE; + }; + filesaver.readyState = filesaver.WRITING; + }), fs_error); + }), fs_error); + }; + dir.getFile(name, {create: false}, abortable(function(file) { + // delete file if it already exists + file.remove(); + save(); + }), abortable(function(ex) { + if (ex.code === ex.NOT_FOUND_ERR) { + save(); + } else { + fs_error(); + } + })); + }), fs_error); + }), fs_error); + } + , FS_proto = FileSaver.prototype + , saveAs = function(blob, name) { + return new FileSaver(blob, name); + } + ; + FS_proto.abort = function() { + var filesaver = this; + filesaver.readyState = filesaver.DONE; + dispatch(filesaver, "abort"); + }; + FS_proto.readyState = FS_proto.INIT = 0; + FS_proto.WRITING = 1; + FS_proto.DONE = 2; + + FS_proto.error = + FS_proto.onwritestart = + FS_proto.onprogress = + FS_proto.onwrite = + FS_proto.onabort = + FS_proto.onerror = + FS_proto.onwriteend = + null; + + view.addEventListener("unload", process_deletion_queue, false); + saveAs.unload = function() { + process_deletion_queue(); + view.removeEventListener("unload", process_deletion_queue, false); + }; + return saveAs; +}( + typeof self !== "undefined" && self + || typeof window !== "undefined" && window + || this.content +)); +// `self` is undefined in Firefox for Android content script context +// while `this` is nsIContentFrameMessageManager +// with an attribute `content` that corresponds to the window + +if (typeof module !== "undefined" && module !== null) { + module.exports = saveAs; +} else if ((typeof define !== "undefined" && define !== null) && (define.amd != null)) { + define([], function() { + return saveAs; + }); +} diff --git a/contrib/online_ide/www/js/editor_configuration.js b/contrib/online_ide/www/js/editor_configuration.js new file mode 100644 index 0000000..0d67ad5 --- /dev/null +++ b/contrib/online_ide/www/js/editor_configuration.js @@ -0,0 +1,46 @@ +// This file is part of NIT ( http://www.nitlanguage.org ) +// +// Copyright 2014 Johan Kayser +// +// 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. + +// Sets up the editor. + +ace.require("ace/ext/language_tools"); +var editor = ace.edit("editor"); +editor.setTheme("ace/theme/chrome"); +editor.getSession().setMode("ace/mode/nit"); +editor.setOptions({ + enableBasicAutocompletion: true +}); +// Custom commands + +// We launch the interpreter on CTRL+E shortcut. +editor.commands.addCommand({ + name: 'interpret', + bindKey: {win: 'Ctrl-E', mac: 'Command-E'}, + exec: function(editor) { + interpret(); + }, + readOnly: true // false if this command should not apply in readOnly mode +}); + +// We download the current file in editor on CTRL+S shortcut. +editor.commands.addCommand({ + name: 'save', + bindKey: {win: 'Ctrl-S', mac: 'Command-S'}, + exec: function(editor) { + save(); + }, + readOnly: true // false if this command should not apply in readOnly mode +}); diff --git a/contrib/online_ide/www/js/functions.js b/contrib/online_ide/www/js/functions.js new file mode 100644 index 0000000..816f70d --- /dev/null +++ b/contrib/online_ide/www/js/functions.js @@ -0,0 +1,192 @@ +// This file is part of NIT ( http://www.nitlanguage.org ) +// +// Copyright 2014 Johan Kayser +// +// 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. + +// Initializes the terminal, set it to send messages to PNaCl. +function init_terminal() { + terminal = $('#terminal').terminal(function(command, term) { + pnacl_nitModule.postMessage(command + '\n'); + }, { greetings: false, name: 'terminal' }); +} + +// Edit current file name. +$('#filename').bind('click', function() { + $(this).attr('contentEditable', true); +}).blur( + function() { + $(this).attr('contentEditable', false); + }); + +// To launch the interpret process. +function interpret() { + var dictionary = { + operation: 'interpret', + args: /*"-v -v " + */document.getElementById('filename').innerText + ".nit", + content: editor.getValue() + } + pnacl_nitModule.postMessage(dictionary); +} + +// To save the current editor lines in a file. +function save() { + var blob = new Blob([editor.getValue()], {type: "text/plain;charset=utf-8"}); + saveAs(blob, document.getElementById('filename').innerText + ".nit"); +} + +// To load nit lib +function load_nit_lib() { + if (!lib_files_loaded) + { + // We get the 'nit' folder content on github. + $.get("https://api.github.com/repos/privat/nit/contents/?access_token=" + github_acces_token, function(data) { + for (var i = 0; i < data.length; i++) { + if (data[i].name == "lib") { + // We get the list of all files in the 'lib' folder. + $.get("https://api.github.com/repos/privat/nit/git/trees/" + data[i].sha + "?recursive=1&access_token=" + github_acces_token, function(data) { + for (var i = 0; i < data.tree.length; i++) { + if (data.tree[i].type == "blob") { + lib_files_number++; + sha_filename_map[data.tree[i].sha] = data.tree[i].path.split("/").pop(); + + // We get the content of each file and send it to PNaCl. + $.get(data.tree[i].url + "?access_token=" + github_acces_token, function(data) { + var dictionary = { + operation: 'load', + filename: sha_filename_map[data.sha], + content: Base64.decode(data.content), + } + pnacl_nitModule.postMessage(dictionary); + lib_files.push(dictionary); + }); + } + } + }); + } + } + }); + } + else { + // If we already have the files stored in JS, we just send them. + for (var i = 0; i < lib_files.length; i++) { + pnacl_nitModule.postMessage(lib_files[i]); + } + } +} + +// To code/decode base64 +var Base64 = { + _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", + + 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; + }, + + + 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; + }, + + _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; + }, + + _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; + } +} diff --git a/contrib/online_ide/www/js/jquery.terminal-0.8.7.js b/contrib/online_ide/www/js/jquery.terminal-0.8.7.js new file mode 100644 index 0000000..5d145f3 --- /dev/null +++ b/contrib/online_ide/www/js/jquery.terminal-0.8.7.js @@ -0,0 +1,4328 @@ +/**@license + * __ _____ ________ __ + * / // _ /__ __ _____ ___ __ _/__ ___/__ ___ ______ __ __ __ ___ / / + * __ / // // // // // _ // _// // / / // _ // _// // // \/ // _ \/ / + * / / // // // // // ___// / / // / / // ___// / / / / // // /\ // // / /__ + * \___//____ \\___//____//_/ _\_ / /_//____//_/ /_/ /_//_//_/ /_/ \__\_\___/ + * \/ /____/ version 0.8.7 + * http://terminal.jcubic.pl + * + * Licensed under GNU LGPL Version 3 license + * Copyright (c) 2011-2013 Jakub Jankiewicz + * + * Includes: + * + * Storage plugin Distributed under the MIT License + * Copyright (c) 2010 Dave Schindler + * + * jQuery Timers licenced with the WTFPL + * + * + * Cross-Browser Split 1.1.1 + * Copyright 2007-2012 Steven Levithan + * Available under the MIT License + * + * sprintf.js + * Copyright (c) 2007-2013 Alexandru Marasteanu + * licensed under 3 clause BSD license + * + * Date: Sat, 22 Mar 2014 13:32:26 +0000 + * + */ + +(function(ctx) { + var sprintf = function() { + if (!sprintf.cache.hasOwnProperty(arguments[0])) { + sprintf.cache[arguments[0]] = sprintf.parse(arguments[0]); + } + return sprintf.format.call(null, sprintf.cache[arguments[0]], arguments); + }; + + sprintf.format = function(parse_tree, argv) { + var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length; + for (i = 0; i < tree_length; i++) { + node_type = get_type(parse_tree[i]); + if (node_type === 'string') { + output.push(parse_tree[i]); + } + else if (node_type === 'array') { + match = parse_tree[i]; // convenience purposes only + if (match[2]) { // keyword argument + arg = argv[cursor]; + for (k = 0; k < match[2].length; k++) { + if (!arg.hasOwnProperty(match[2][k])) { + throw(sprintf('[sprintf] property "%s" does not exist', match[2][k])); + } + arg = arg[match[2][k]]; + } + } + else if (match[1]) { // positional argument (explicit) + arg = argv[match[1]]; + } + else { // positional argument (implicit) + arg = argv[cursor++]; + } + + if (/[^s]/.test(match[8]) && (get_type(arg) != 'number')) { + throw(sprintf('[sprintf] expecting number but found %s', get_type(arg))); + } + switch (match[8]) { + case 'b': arg = arg.toString(2); break; + case 'c': arg = String.fromCharCode(arg); break; + case 'd': arg = parseInt(arg, 10); break; + case 'e': arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); break; + case 'f': arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); break; + case 'o': arg = arg.toString(8); break; + case 's': arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg); break; + case 'u': arg = arg >>> 0; break; + case 'x': arg = arg.toString(16); break; + case 'X': arg = arg.toString(16).toUpperCase(); break; + } + arg = (/[def]/.test(match[8]) && match[3] && arg >= 0 ? '+'+ arg : arg); + pad_character = match[4] ? match[4] == '0' ? '0' : match[4].charAt(1) : ' '; + pad_length = match[6] - String(arg).length; + pad = match[6] ? str_repeat(pad_character, pad_length) : ''; + output.push(match[5] ? arg + pad : pad + arg); + } + } + return output.join(''); + }; + + sprintf.cache = {}; + + sprintf.parse = function(fmt) { + var _fmt = fmt, match = [], parse_tree = [], arg_names = 0; + while (_fmt) { + if ((match = /^[^\x25]+/.exec(_fmt)) !== null) { + parse_tree.push(match[0]); + } + else if ((match = /^\x25{2}/.exec(_fmt)) !== null) { + parse_tree.push('%'); + } + else if ((match = /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(_fmt)) !== null) { + if (match[2]) { + arg_names |= 1; + var field_list = [], replacement_field = match[2], field_match = []; + if ((field_match = /^([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) { + field_list.push(field_match[1]); + while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') { + if ((field_match = /^\.([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) { + field_list.push(field_match[1]); + } + else if ((field_match = /^\[(\d+)\]/.exec(replacement_field)) !== null) { + field_list.push(field_match[1]); + } + else { + throw('[sprintf] huh?'); + } + } + } + else { + throw('[sprintf] huh?'); + } + match[2] = field_list; + } + else { + arg_names |= 2; + } + if (arg_names === 3) { + throw('[sprintf] mixing positional and named placeholders is not (yet) supported'); + } + parse_tree.push(match); + } + else { + throw('[sprintf] huh?'); + } + _fmt = _fmt.substring(match[0].length); + } + return parse_tree; + }; + + var vsprintf = function(fmt, argv, _argv) { + _argv = argv.slice(0); + _argv.splice(0, 0, fmt); + return sprintf.apply(null, _argv); + }; + + /** + * helpers + */ + function get_type(variable) { + return Object.prototype.toString.call(variable).slice(8, -1).toLowerCase(); + } + + function str_repeat(input, multiplier) { + for (var output = []; multiplier > 0; output[--multiplier] = input) {/* do nothing */} + return output.join(''); + } + + /** + * export to either browser or node.js + */ + ctx.sprintf = sprintf; + ctx.vsprintf = vsprintf; +})(typeof exports != "undefined" ? exports : window); + +(function($, undefined) { + "use strict"; + // ----------------------------------------------------------------------- + // :: map object to object + // ----------------------------------------------------------------------- + $.omap = function(o, fn) { + var result = {}; + $.each(o, function(k, v) { + result[k] = fn.call(o, k, v); + }); + return result; + }; + // ----------------------------------------------------------------------- + // :: Storage plugin + // ----------------------------------------------------------------------- + // Private data + var isLS = typeof window.localStorage !== 'undefined'; + // Private functions + function wls(n, v) { + var c; + if (typeof n === 'string' && typeof v === 'string') { + localStorage[n] = v; + return true; + } else if (typeof n === 'object' && typeof v === 'undefined') { + for (c in n) { + if (n.hasOwnProperty(c)) { + localStorage[c] = n[c]; + } + } + return true; + } + return false; + } + function wc(n, v) { + var dt, e, c; + dt = new Date(); + dt.setTime(dt.getTime() + 31536000000); + e = '; expires=' + dt.toGMTString(); + if (typeof n === 'string' && typeof v === 'string') { + document.cookie = n + '=' + v + e + '; path=/'; + return true; + } else if (typeof n === 'object' && typeof v === 'undefined') { + for (c in n) { + if (n.hasOwnProperty(c)) { + document.cookie = c + '=' + n[c] + e + '; path=/'; + } + } + return true; + } + return false; + } + function rls(n) { + return localStorage[n]; + } + function rc(n) { + var nn, ca, i, c; + nn = n + '='; + ca = document.cookie.split(';'); + for (i = 0; i < ca.length; i++) { + c = ca[i]; + while (c.charAt(0) === ' ') { + c = c.substring(1, c.length); + } + if (c.indexOf(nn) === 0) { + return c.substring(nn.length, c.length); + } + } + return null; + } + function dls(n) { + return delete localStorage[n]; + } + function dc(n) { + return wc(n, '', -1); + } + /** + * Public API + * $.Storage.set("name", "value") + * $.Storage.set({"name1":"value1", "name2":"value2", etc}) + * $.Storage.get("name") + * $.Storage.remove("name") + */ + $.extend({ + Storage: { + set: isLS ? wls : wc, + get: isLS ? rls : rc, + remove: isLS ? dls : dc + } + }); + // ----------------------------------------------------------------------- + // :: jQuery Timers + // ----------------------------------------------------------------------- + jQuery.fn.extend({ + everyTime: function(interval, label, fn, times, belay) { + return this.each(function() { + jQuery.timer.add(this, interval, label, fn, times, belay); + }); + }, + oneTime: function(interval, label, fn) { + return this.each(function() { + jQuery.timer.add(this, interval, label, fn, 1); + }); + }, + stopTime: function(label, fn) { + return this.each(function() { + jQuery.timer.remove(this, label, fn); + }); + } + }); + + jQuery.extend({ + timer: { + guid: 1, + global: {}, + regex: /^([0-9]+)\s*(.*s)?$/, + powers: { + // Yeah this is major overkill... + 'ms': 1, + 'cs': 10, + 'ds': 100, + 's': 1000, + 'das': 10000, + 'hs': 100000, + 'ks': 1000000 + }, + timeParse: function(value) { + if (value === undefined || value === null) { + return null; + } + var result = this.regex.exec(jQuery.trim(value.toString())); + if (result[2]) { + var num = parseInt(result[1], 10); + var mult = this.powers[result[2]] || 1; + return num * mult; + } else { + return value; + } + }, + add: function(element, interval, label, fn, times, belay) { + var counter = 0; + + if (jQuery.isFunction(label)) { + if (!times) { + times = fn; + } + fn = label; + label = interval; + } + + interval = jQuery.timer.timeParse(interval); + + if (typeof interval !== 'number' || + isNaN(interval) || + interval <= 0) { + return; + } + if (times && times.constructor !== Number) { + belay = !!times; + times = 0; + } + + times = times || 0; + belay = belay || false; + + if (!element.$timers) { + element.$timers = {}; + } + if (!element.$timers[label]) { + element.$timers[label] = {}; + } + fn.$timerID = fn.$timerID || this.guid++; + + var handler = function() { + if (belay && handler.inProgress) { + return; + } + handler.inProgress = true; + if ((++counter > times && times !== 0) || + fn.call(element, counter) === false) { + jQuery.timer.remove(element, label, fn); + } + handler.inProgress = false; + }; + + handler.$timerID = fn.$timerID; + + if (!element.$timers[label][fn.$timerID]) { + element.$timers[label][fn.$timerID] = window.setInterval(handler, interval); + } + + if (!this.global[label]) { + this.global[label] = []; + } + this.global[label].push(element); + + }, + remove: function(element, label, fn) { + var timers = element.$timers, ret; + + if (timers) { + + if (!label) { + for (var lab in timers) { + if (timers.hasOwnProperty(lab)) { + this.remove(element, lab, fn); + } + } + } else if (timers[label]) { + if (fn) { + if (fn.$timerID) { + window.clearInterval(timers[label][fn.$timerID]); + delete timers[label][fn.$timerID]; + } + } else { + for (var _fn in timers[label]) { + if (timers[label].hasOwnProperty(_fn)) { + window.clearInterval(timers[label][_fn]); + delete timers[label][_fn]; + } + } + } + + for (ret in timers[label]) { + if (timers[label].hasOwnProperty(ret)) { + break; + } + } + if (!ret) { + ret = null; + delete timers[label]; + } + } + + for (ret in timers) { + if (timers.hasOwnProperty(ret)) { + break; + } + } + if (!ret) { + element.$timers = null; + } + } + } + } + }); + + if (/(msie) ([\w.]+)/.exec(navigator.userAgent.toLowerCase())) { + jQuery(window).one('unload', function() { + var global = jQuery.timer.global; + for (var label in global) { + if (global.hasOwnProperty(label)) { + var els = global[label], i = els.length; + while (--i) { + jQuery.timer.remove(els[i], label); + } + } + } + }); + } + // ----------------------------------------------------------------------- + // :: CROSS BROWSER SPLIT + // ----------------------------------------------------------------------- + + (function(undef) { + + // prevent double include + + if (!String.prototype.split.toString().match(/\[native/)) { + return; + } + + var nativeSplit = String.prototype.split, + compliantExecNpcg = /()??/.exec("")[1] === undef, // NPCG: nonparticipating capturing group + self; + + self = function (str, separator, limit) { + // If `separator` is not a regex, use `nativeSplit` + if (Object.prototype.toString.call(separator) !== "[object RegExp]") { + return nativeSplit.call(str, separator, limit); + } + var output = [], + flags = (separator.ignoreCase ? "i" : "") + + (separator.multiline ? "m" : "") + + (separator.extended ? "x" : "") + // Proposed for ES6 + (separator.sticky ? "y" : ""), // Firefox 3+ + lastLastIndex = 0, + // Make `global` and avoid `lastIndex` issues by working with a copy + separator2, match, lastIndex, lastLength; + separator = new RegExp(separator.source, flags + "g"); + str += ""; // Type-convert + if (!compliantExecNpcg) { + // Doesn't need flags gy, but they don't hurt + separator2 = new RegExp("^" + separator.source + "$(?!\\s)", flags); + } + /* Values for `limit`, per the spec: + * If undefined: 4294967295 // Math.pow(2, 32) - 1 + * If 0, Infinity, or NaN: 0 + * If positive number: limit = Math.floor(limit); if (limit > 4294967295) limit -= 4294967296; + * If negative number: 4294967296 - Math.floor(Math.abs(limit)) + * If other: Type-convert, then use the above rules + */ + // ? Math.pow(2, 32) - 1 : ToUint32(limit) + limit = limit === undef ? -1 >>> 0 : limit >>> 0; + while (match = separator.exec(str)) { + // `separator.lastIndex` is not reliable cross-browser + lastIndex = match.index + match[0].length; + if (lastIndex > lastLastIndex) { + output.push(str.slice(lastLastIndex, match.index)); + // Fix browsers whose `exec` methods don't consistently return `undefined` for + // nonparticipating capturing groups + if (!compliantExecNpcg && match.length > 1) { + match[0].replace(separator2, function () { + for (var i = 1; i < arguments.length - 2; i++) { + if (arguments[i] === undef) { + match[i] = undef; + } + } + }); + } + if (match.length > 1 && match.index < str.length) { + Array.prototype.push.apply(output, match.slice(1)); + } + lastLength = match[0].length; + lastLastIndex = lastIndex; + if (output.length >= limit) { + break; + } + } + if (separator.lastIndex === match.index) { + separator.lastIndex++; // Avoid an infinite loop + } + } + if (lastLastIndex === str.length) { + if (lastLength || !separator.test("")) { + output.push(""); + } + } else { + output.push(str.slice(lastLastIndex)); + } + return output.length > limit ? output.slice(0, limit) : output; + }; + + // For convenience + String.prototype.split = function (separator, limit) { + return self(this, separator, limit); + }; + + return self; + + })(); + // ----------------------------------------------------------------------- + // :: Split string to array of strings with the same length + // ----------------------------------------------------------------------- + function str_parts(str, length) { + var result = []; + var len = str.length; + if (len < length) { + return [str]; + } + for (var i = 0; i < len; i += length) { + result.push(str.substring(i, i + length)); + } + return result; + } + // ----------------------------------------------------------------------- + // :: CYCLE DATA STRUCTURE + // ----------------------------------------------------------------------- + function Cycle(init) { + var data = init ? [init] : []; + var pos = 0; + $.extend(this, { + get: function() { + return data; + }, + rotate: function() { + if (data.length === 1) { + return data[0]; + } else { + if (pos === data.length - 1) { + pos = 0; + } else { + ++pos; + } + return data[pos]; + } + }, + length: function() { + return data.length; + }, + set: function(item) { + for (var i = data.length; i--;) { + if (data[i] === item) { + pos = i; + return; + } + } + this.append(item); + }, + front: function() { + return data[pos]; + }, + append: function(item) { + data.push(item); + } + }); + } + // ----------------------------------------------------------------------- + // :: STACK DATA STRUCTURE + // ----------------------------------------------------------------------- + function Stack(init) { + var data = init ? [init] : []; + $.extend(this, { + map: function(fn) { + return $.map(data, fn); + }, + size: function() { + return data.length; + }, + pop: function() { + if (data.length === 0) { + return null; + } else { + var value = data[data.length - 1]; + data = data.slice(0, data.length - 1); + return value; + } + }, + push: function(value) { + data = data.concat([value]); + return value; + }, + top: function() { + return data.length > 0 ? data[data.length - 1] : null; + } + }); + } + // ----------------------------------------------------------------------- + // :: Serialize object myself (biwascheme or prototype library do something + // :: wicked with JSON serialization for Arrays) + // ----------------------------------------------------------------------- + $.json_stringify = function(object, level) { + var result = '', i; + level = level === undefined ? 1 : level; + var type = typeof object; + switch (type) { + case 'function': + result += object; + break; + case 'boolean': + result += object ? 'true' : 'false'; + break; + case 'object': + if (object === null) { + result += 'null'; + } else if (object instanceof Array) { + result += '['; + var len = object.length; + for (i = 0; i < len - 1; ++i) { + result += $.json_stringify(object[i], level + 1); + } + result += $.json_stringify(object[len - 1], level + 1) + ']'; + } else { + result += '{'; + for (var property in object) { + if (object.hasOwnProperty(property)) { + result += '"' + property + '":' + + $.json_stringify(object[property], level + 1); + } + } + result += '}'; + } + break; + case 'string': + var str = object; + var repl = { + '\\\\': '\\\\', + '"': '\\"', + '/': '\\/', + '\\n': '\\n', + '\\r': '\\r', + '\\t': '\\t'}; + for (i in repl) { + if (repl.hasOwnProperty(i)) { + str = str.replace(new RegExp(i, 'g'), repl[i]); + } + } + result += '"' + str + '"'; + break; + case 'number': + result += String(object); + break; + } + result += (level > 1 ? ',' : ''); + // quick hacks below + if (level === 1) { + // fix last comma + result = result.replace(/,([\]}])/g, '$1'); + } + // fix comma before array or object + return result.replace(/([\[{]),/g, '$1'); + }; + // ----------------------------------------------------------------------- + // :: HISTORY CLASS + // ----------------------------------------------------------------------- + function History(name, size) { + var enabled = true; + var storage_key = ''; + if (typeof name === 'string' && name !== '') { + storage_key = name + '_'; + } + storage_key += 'commands'; + var data = $.Storage.get(storage_key); + data = data ? $.parseJSON(data) : []; + var pos = data.length-1; + $.extend(this, { + append: function(item) { + if (enabled) { + if (data[data.length-1] !== item) { + data.push(item); + if (size && data.length > size) { + data = data.slice(-size); + } + pos = data.length-1; + $.Storage.set(storage_key, $.json_stringify(data)); + } + } + }, + data: function() { + return data; + }, + reset: function() { + pos = data.length-1; + }, + last: function() { + return data[length-1]; + }, + end: function() { + return pos === data.length-1; + }, + position: function() { + return pos; + }, + current: function() { + return data[pos]; + }, + next: function() { + if (pos < data.length-1) { + ++pos; + } + if (pos !== -1) { + return data[pos]; + } + }, + previous: function() { + var old = pos; + if (pos > 0) { + --pos; + } + if (old !== -1) { + return data[pos]; + } + }, + clear: function() { + data = []; + this.purge(); + }, + enabled: function() { + return enabled; + }, + enable: function() { + enabled = true; + }, + purge: function() { + $.Storage.remove(storage_key); + }, + disable: function() { + enabled = false; + } + }); + } + // ----------------------------------------------------------------------- + // :: COMMAND LINE PLUGIN + // ----------------------------------------------------------------------- + $.fn.cmd = function(options) { + var self = this; + var maybe_data = self.data('cmd'); + if (maybe_data) { + return maybe_data; + } + self.addClass('cmd'); + self.append('' + + ' '); + var clip = $('