incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1333961 [3/3] - in /sling/trunk/contrib: ./ scripting/script-console/ scripting/script-console/src/ scripting/script-console/src/main/ scripting/script-console/src/main/appended-resources/ scripting/script-console/src/main/appended-resourc...
Date Fri, 04 May 2012 13:44:54 GMT
Added: sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/loadmode.js
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/loadmode.js?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/loadmode.js
(added)
+++ sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/loadmode.js
Fri May  4 13:44:53 2012
@@ -0,0 +1,50 @@
+(function() {
+  if (!CodeMirror.modeURL) CodeMirror.modeURL = "../mode/%N/%N.js";
+
+  var loading = {};
+  function splitCallback(cont, n) {
+    var countDown = n;
+    return function() { if (--countDown == 0) cont(); }
+  }
+  function ensureDeps(mode, cont) {
+    var deps = CodeMirror.modes[mode].dependencies;
+    if (!deps) return cont();
+    var missing = [];
+    for (var i = 0; i < deps.length; ++i) {
+      if (!CodeMirror.modes.hasOwnProperty(deps[i]))
+        missing.push(deps[i]);
+    }
+    if (!missing.length) return cont();
+    var split = splitCallback(cont, missing.length);
+    for (var i = 0; i < missing.length; ++i)
+      CodeMirror.requireMode(missing[i], split);
+  }
+
+  CodeMirror.requireMode = function(mode, cont) {
+    if (CodeMirror.modes.hasOwnProperty(mode)) return ensureDeps(mode, cont());
+    if (loading.hasOwnProperty(mode)) return loading[mode].push(cont);
+
+    var script = document.createElement("script");
+    script.src = CodeMirror.modeURL.replace(/%N/g, mode);
+    var others = document.getElementsByTagName("script")[0];
+    others.parentNode.insertBefore(script, others);
+    var list = loading[mode] = [cont];
+    var count = 0, poll = setInterval(function() {
+      if (++count > 100) return clearInterval(poll);
+      if (CodeMirror.modes.hasOwnProperty(mode)) {
+        clearInterval(poll);
+        loading[mode] = null;
+        ensureDeps(mode, function() {
+          for (var i = 0; i < list.length; ++i) list[i]();
+        });
+      }
+    }, 200);
+  };
+
+  CodeMirror.autoLoadMode = function(instance, mode) {
+    if (!CodeMirror.modes.hasOwnProperty(mode))
+      CodeMirror.requireMode(mode, function() {
+        instance.setOption("mode", instance.getOption("mode"));
+      });
+  };
+}());

Added: sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/runmode.js
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/runmode.js?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/runmode.js
(added)
+++ sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/lib/util/runmode.js
Fri May  4 13:44:53 2012
@@ -0,0 +1,49 @@
+CodeMirror.runMode = function(string, modespec, callback, options) {
+  var mode = CodeMirror.getMode(CodeMirror.defaults, modespec);
+  var isNode = callback.nodeType == 1;
+  var tabSize = (options && options.tabSize) || CodeMirror.defaults.tabSize;
+  if (isNode) {
+    var node = callback, accum = [], col = 0;
+    callback = function(text, style) {
+      if (text == "\n") {
+        accum.push("<br>");
+        col = 0;
+        return;
+      }
+      var escaped = "";
+      // HTML-escape and replace tabs
+      for (var pos = 0;;) {
+        var idx = text.indexOf("\t", pos);
+        if (idx == -1) {
+          escaped += CodeMirror.htmlEscape(text.slice(pos));
+          col += text.length - pos;
+          break;
+        } else {
+          col += idx - pos;
+          escaped += CodeMirror.htmlEscape(text.slice(pos, idx));
+          var size = tabSize - col % tabSize;
+          col += size;
+          for (var i = 0; i < size; ++i) escaped += " ";
+          pos = idx + 1;
+        }
+      }
+
+      if (style) 
+        accum.push("<span class=\"cm-" + CodeMirror.htmlEscape(style) + "\">" + escaped
+ "</span>");
+      else
+        accum.push(escaped);
+    }
+  }
+  var lines = CodeMirror.splitLines(string), state = CodeMirror.startState(mode);
+  for (var i = 0, e = lines.length; i < e; ++i) {
+    if (i) callback("\n");
+    var stream = new CodeMirror.StringStream(lines[i]);
+    while (!stream.eol()) {
+      var style = mode.token(stream, state);
+      callback(stream.current(), style, i, stream.start);
+      stream.start = stream.pos;
+    }
+  }
+  if (isNode)
+    node.innerHTML = accum.join("");
+};

Added: sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/groovy/groovy.js
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/groovy/groovy.js?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/groovy/groovy.js
(added)
+++ sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/groovy/groovy.js
Fri May  4 13:44:53 2012
@@ -0,0 +1,210 @@
+CodeMirror.defineMode("groovy", function(config, parserConfig) {
+  function words(str) {
+    var obj = {}, words = str.split(" ");
+    for (var i = 0; i < words.length; ++i) obj[words[i]] = true;
+    return obj;
+  }
+  var keywords = words(
+    "abstract as assert boolean break byte case catch char class const continue def default
" +
+    "do double else enum extends final finally float for goto if implements import in " +
+    "instanceof int interface long native new package private protected public return " +
+    "short static strictfp super switch synchronized threadsafe throw throws transient "
+
+    "try void volatile while");
+  var blockKeywords = words("catch class do else finally for if switch try while enum interface
def");
+  var atoms = words("null true false this");
+
+  var curPunc;
+  function tokenBase(stream, state) {
+    var ch = stream.next();
+    if (ch == '"' || ch == "'") {
+      return startString(ch, stream, state);
+    }
+    if (/[\[\]{}\(\),;\:\.]/.test(ch)) {
+      curPunc = ch;
+      return null
+    }
+    if (/\d/.test(ch)) {
+      stream.eatWhile(/[\w\.]/);
+      if (stream.eat(/eE/)) { stream.eat(/\+\-/); stream.eatWhile(/\d/); }
+      return "number";
+    }
+    if (ch == "/") {
+      if (stream.eat("*")) {
+        state.tokenize.push(tokenComment);
+        return tokenComment(stream, state);
+      }
+      if (stream.eat("/")) {
+        stream.skipToEnd();
+        return "comment";
+      }
+      if (expectExpression(state.lastToken)) {
+        return startString(ch, stream, state);
+      }
+    }
+    if (ch == "-" && stream.eat(">")) {
+      curPunc = "->";
+      return null;
+    }
+    if (/[+\-*&%=<>!?|\/~]/.test(ch)) {
+      stream.eatWhile(/[+\-*&%=<>|~]/);
+      return "operator";
+    }
+    stream.eatWhile(/[\w\$_]/);
+    if (ch == "@") { stream.eatWhile(/[\w\$_\.]/); return "meta"; }
+    if (state.lastToken == ".") return "property";
+    if (stream.eat(":")) { curPunc = "proplabel"; return "property"; }
+    var cur = stream.current();
+    if (atoms.propertyIsEnumerable(cur)) { return "atom"; }
+    if (keywords.propertyIsEnumerable(cur)) {
+      if (blockKeywords.propertyIsEnumerable(cur)) curPunc = "newstatement";
+      return "keyword";
+    }
+    return "word";
+  }
+  tokenBase.isBase = true;
+
+  function startString(quote, stream, state) {
+    var tripleQuoted = false;
+    if (quote != "/" && stream.eat(quote)) {
+      if (stream.eat(quote)) tripleQuoted = true;
+      else return "string";
+    }
+    function t(stream, state) {
+      var escaped = false, next, end = !tripleQuoted;
+      while ((next = stream.next()) != null) {
+        if (next == quote && !escaped) {
+          if (!tripleQuoted) { break; }
+          if (stream.match(quote + quote)) { end = true; break; }
+        }
+        if (quote == '"' && next == "$" && !escaped && stream.eat("{"))
{
+          state.tokenize.push(tokenBaseUntilBrace());
+          return "string";
+        }
+        escaped = !escaped && next == "\\";
+      }
+      if (end) state.tokenize.pop();
+      return "string";
+    }
+    state.tokenize.push(t);
+    return t(stream, state);
+  }
+
+  function tokenBaseUntilBrace() {
+    var depth = 1;
+    function t(stream, state) {
+      if (stream.peek() == "}") {
+        depth--;
+        if (depth == 0) {
+          state.tokenize.pop();
+          return state.tokenize[state.tokenize.length-1](stream, state);
+        }
+      } else if (stream.peek() == "{") {
+        depth++;
+      }
+      return tokenBase(stream, state);
+    }
+    t.isBase = true;
+    return t;
+  }
+
+  function tokenComment(stream, state) {
+    var maybeEnd = false, ch;
+    while (ch = stream.next()) {
+      if (ch == "/" && maybeEnd) {
+        state.tokenize.pop();
+        break;
+      }
+      maybeEnd = (ch == "*");
+    }
+    return "comment";
+  }
+
+  function expectExpression(last) {
+    return !last || last == "operator" || last == "->" || /[\.\[\{\(,;:]/.test(last) ||
+      last == "newstatement" || last == "keyword" || last == "proplabel";
+  }
+
+  function Context(indented, column, type, align, prev) {
+    this.indented = indented;
+    this.column = column;
+    this.type = type;
+    this.align = align;
+    this.prev = prev;
+  }
+  function pushContext(state, col, type) {
+    return state.context = new Context(state.indented, col, type, null, state.context);
+  }
+  function popContext(state) {
+    var t = state.context.type;
+    if (t == ")" || t == "]" || t == "}")
+      state.indented = state.context.indented;
+    return state.context = state.context.prev;
+  }
+
+  // Interface
+
+  return {
+    startState: function(basecolumn) {
+      return {
+        tokenize: [tokenBase],
+        context: new Context((basecolumn || 0) - config.indentUnit, 0, "top", false),
+        indented: 0,
+        startOfLine: true,
+        lastToken: null
+      };
+    },
+
+    token: function(stream, state) {
+      var ctx = state.context;
+      if (stream.sol()) {
+        if (ctx.align == null) ctx.align = false;
+        state.indented = stream.indentation();
+        state.startOfLine = true;
+        // Automatic semicolon insertion
+        if (ctx.type == "statement" && !expectExpression(state.lastToken)) {
+          popContext(state); ctx = state.context;
+        }
+      }
+      if (stream.eatSpace()) return null;
+      curPunc = null;
+      var style = state.tokenize[state.tokenize.length-1](stream, state);
+      if (style == "comment") return style;
+      if (ctx.align == null) ctx.align = true;
+
+      if ((curPunc == ";" || curPunc == ":") && ctx.type == "statement") popContext(state);
+      // Handle indentation for {x -> \n ... }
+      else if (curPunc == "->" && ctx.type == "statement" && ctx.prev.type
== "}") {
+        popContext(state);
+        state.context.align = false;
+      }
+      else if (curPunc == "{") pushContext(state, stream.column(), "}");
+      else if (curPunc == "[") pushContext(state, stream.column(), "]");
+      else if (curPunc == "(") pushContext(state, stream.column(), ")");
+      else if (curPunc == "}") {
+        while (ctx.type == "statement") ctx = popContext(state);
+        if (ctx.type == "}") ctx = popContext(state);
+        while (ctx.type == "statement") ctx = popContext(state);
+      }
+      else if (curPunc == ctx.type) popContext(state);
+      else if (ctx.type == "}" || ctx.type == "top" || (ctx.type == "statement" &&
curPunc == "newstatement"))
+        pushContext(state, stream.column(), "statement");
+      state.startOfLine = false;
+      state.lastToken = curPunc || style;
+      return style;
+    },
+
+    indent: function(state, textAfter) {
+      if (!state.tokenize[state.tokenize.length-1].isBase) return 0;
+      var firstChar = textAfter && textAfter.charAt(0), ctx = state.context;
+      if (ctx.type == "statement" && !expectExpression(state.lastToken)) ctx = ctx.prev;
+      var closing = firstChar == ctx.type;
+      if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : config.indentUnit);
+      else if (ctx.align) return ctx.column + (closing ? 0 : 1);
+      else return ctx.indented + (closing ? 0 : config.indentUnit);
+    },
+
+    electricChars: "{}"
+  };
+});
+
+CodeMirror.defineMIME("text/x-groovy", "groovy");

Added: sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/javascript/javascript.js
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/javascript/javascript.js?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/javascript/javascript.js
(added)
+++ sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/codemirror/mode/javascript/javascript.js
Fri May  4 13:44:53 2012
@@ -0,0 +1,361 @@
+CodeMirror.defineMode("javascript", function(config, parserConfig) {
+  var indentUnit = config.indentUnit;
+  var jsonMode = parserConfig.json;
+
+  // Tokenizer
+
+  var keywords = function(){
+    function kw(type) {return {type: type, style: "keyword"};}
+    var A = kw("keyword a"), B = kw("keyword b"), C = kw("keyword c");
+    var operator = kw("operator"), atom = {type: "atom", style: "atom"};
+    return {
+      "if": A, "while": A, "with": A, "else": B, "do": B, "try": B, "finally": B,
+      "return": C, "break": C, "continue": C, "new": C, "delete": C, "throw": C,
+      "var": kw("var"), "const": kw("var"), "let": kw("var"),
+      "function": kw("function"), "catch": kw("catch"),
+      "for": kw("for"), "switch": kw("switch"), "case": kw("case"), "default": kw("default"),
+      "in": operator, "typeof": operator, "instanceof": operator,
+      "true": atom, "false": atom, "null": atom, "undefined": atom, "NaN": atom, "Infinity":
atom
+    };
+  }();
+
+  var isOperatorChar = /[+\-*&%=<>!?|]/;
+
+  function chain(stream, state, f) {
+    state.tokenize = f;
+    return f(stream, state);
+  }
+
+  function nextUntilUnescaped(stream, end) {
+    var escaped = false, next;
+    while ((next = stream.next()) != null) {
+      if (next == end && !escaped)
+        return false;
+      escaped = !escaped && next == "\\";
+    }
+    return escaped;
+  }
+
+  // Used as scratch variables to communicate multiple values without
+  // consing up tons of objects.
+  var type, content;
+  function ret(tp, style, cont) {
+    type = tp; content = cont;
+    return style;
+  }
+
+  function jsTokenBase(stream, state) {
+    var ch = stream.next();
+    if (ch == '"' || ch == "'")
+      return chain(stream, state, jsTokenString(ch));
+    else if (/[\[\]{}\(\),;\:\.]/.test(ch))
+      return ret(ch);
+    else if (ch == "0" && stream.eat(/x/i)) {
+      stream.eatWhile(/[\da-f]/i);
+      return ret("number", "number");
+    }      
+    else if (/\d/.test(ch)) {
+      stream.match(/^\d*(?:\.\d*)?(?:[eE][+\-]?\d+)?/);
+      return ret("number", "number");
+    }
+    else if (ch == "/") {
+      if (stream.eat("*")) {
+        return chain(stream, state, jsTokenComment);
+      }
+      else if (stream.eat("/")) {
+        stream.skipToEnd();
+        return ret("comment", "comment");
+      }
+      else if (state.reAllowed) {
+        nextUntilUnescaped(stream, "/");
+        stream.eatWhile(/[gimy]/); // 'y' is "sticky" option in Mozilla
+        return ret("regexp", "string-2");
+      }
+      else {
+        stream.eatWhile(isOperatorChar);
+        return ret("operator", null, stream.current());
+      }
+    }
+    else if (ch == "#") {
+        stream.skipToEnd();
+        return ret("error", "error");
+    }
+    else if (isOperatorChar.test(ch)) {
+      stream.eatWhile(isOperatorChar);
+      return ret("operator", null, stream.current());
+    }
+    else {
+      stream.eatWhile(/[\w\$_]/);
+      var word = stream.current(), known = keywords.propertyIsEnumerable(word) &&
keywords[word];
+      return (known && state.kwAllowed) ? ret(known.type, known.style, word) :
+                     ret("variable", "variable", word);
+    }
+  }
+
+  function jsTokenString(quote) {
+    return function(stream, state) {
+      if (!nextUntilUnescaped(stream, quote))
+        state.tokenize = jsTokenBase;
+      return ret("string", "string");
+    };
+  }
+
+  function jsTokenComment(stream, state) {
+    var maybeEnd = false, ch;
+    while (ch = stream.next()) {
+      if (ch == "/" && maybeEnd) {
+        state.tokenize = jsTokenBase;
+        break;
+      }
+      maybeEnd = (ch == "*");
+    }
+    return ret("comment", "comment");
+  }
+
+  // Parser
+
+  var atomicTypes = {"atom": true, "number": true, "variable": true, "string": true, "regexp":
true};
+
+  function JSLexical(indented, column, type, align, prev, info) {
+    this.indented = indented;
+    this.column = column;
+    this.type = type;
+    this.prev = prev;
+    this.info = info;
+    if (align != null) this.align = align;
+  }
+
+  function inScope(state, varname) {
+    for (var v = state.localVars; v; v = v.next)
+      if (v.name == varname) return true;
+  }
+
+  function parseJS(state, style, type, content, stream) {
+    var cc = state.cc;
+    // Communicate our context to the combinators.
+    // (Less wasteful than consing up a hundred closures on every call.)
+    cx.state = state; cx.stream = stream; cx.marked = null, cx.cc = cc;
+  
+    if (!state.lexical.hasOwnProperty("align"))
+      state.lexical.align = true;
+
+    while(true) {
+      var combinator = cc.length ? cc.pop() : jsonMode ? expression : statement;
+      if (combinator(type, content)) {
+        while(cc.length && cc[cc.length - 1].lex)
+          cc.pop()();
+        if (cx.marked) return cx.marked;
+        if (type == "variable" && inScope(state, content)) return "variable-2";
+        return style;
+      }
+    }
+  }
+
+  // Combinator utils
+
+  var cx = {state: null, column: null, marked: null, cc: null};
+  function pass() {
+    for (var i = arguments.length - 1; i >= 0; i--) cx.cc.push(arguments[i]);
+  }
+  function cont() {
+    pass.apply(null, arguments);
+    return true;
+  }
+  function register(varname) {
+    var state = cx.state;
+    if (state.context) {
+      cx.marked = "def";
+      for (var v = state.localVars; v; v = v.next)
+        if (v.name == varname) return;
+      state.localVars = {name: varname, next: state.localVars};
+    }
+  }
+
+  // Combinators
+
+  var defaultVars = {name: "this", next: {name: "arguments"}};
+  function pushcontext() {
+    if (!cx.state.context) cx.state.localVars = defaultVars;
+    cx.state.context = {prev: cx.state.context, vars: cx.state.localVars};
+  }
+  function popcontext() {
+    cx.state.localVars = cx.state.context.vars;
+    cx.state.context = cx.state.context.prev;
+  }
+  function pushlex(type, info) {
+    var result = function() {
+      var state = cx.state;
+      state.lexical = new JSLexical(state.indented, cx.stream.column(), type, null, state.lexical,
info)
+    };
+    result.lex = true;
+    return result;
+  }
+  function poplex() {
+    var state = cx.state;
+    if (state.lexical.prev) {
+      if (state.lexical.type == ")")
+        state.indented = state.lexical.indented;
+      state.lexical = state.lexical.prev;
+    }
+  }
+  poplex.lex = true;
+
+  function expect(wanted) {
+    return function expecting(type) {
+      if (type == wanted) return cont();
+      else if (wanted == ";") return pass();
+      else return cont(arguments.callee);
+    };
+  }
+
+  function statement(type) {
+    if (type == "var") return cont(pushlex("vardef"), vardef1, expect(";"), poplex);
+    if (type == "keyword a") return cont(pushlex("form"), expression, statement, poplex);
+    if (type == "keyword b") return cont(pushlex("form"), statement, poplex);
+    if (type == "{") return cont(pushlex("}"), block, poplex);
+    if (type == ";") return cont();
+    if (type == "function") return cont(functiondef);
+    if (type == "for") return cont(pushlex("form"), expect("("), pushlex(")"), forspec1,
expect(")"),
+                                      poplex, statement, poplex);
+    if (type == "variable") return cont(pushlex("stat"), maybelabel);
+    if (type == "switch") return cont(pushlex("form"), expression, pushlex("}", "switch"),
expect("{"),
+                                         block, poplex, poplex);
+    if (type == "case") return cont(expression, expect(":"));
+    if (type == "default") return cont(expect(":"));
+    if (type == "catch") return cont(pushlex("form"), pushcontext, expect("("), funarg, expect(")"),
+                                        statement, poplex, popcontext);
+    return pass(pushlex("stat"), expression, expect(";"), poplex);
+  }
+  function expression(type) {
+    if (atomicTypes.hasOwnProperty(type)) return cont(maybeoperator);
+    if (type == "function") return cont(functiondef);
+    if (type == "keyword c") return cont(maybeexpression);
+    if (type == "(") return cont(pushlex(")"), maybeexpression, expect(")"), poplex, maybeoperator);
+    if (type == "operator") return cont(expression);
+    if (type == "[") return cont(pushlex("]"), commasep(expression, "]"), poplex, maybeoperator);
+    if (type == "{") return cont(pushlex("}"), commasep(objprop, "}"), poplex, maybeoperator);
+    return cont();
+  }
+  function maybeexpression(type) {
+    if (type.match(/[;\}\)\],]/)) return pass();
+    return pass(expression);
+  }
+    
+  function maybeoperator(type, value) {
+    if (type == "operator" && /\+\+|--/.test(value)) return cont(maybeoperator);
+    if (type == "operator" || type == ":") return cont(expression);
+    if (type == ";") return;
+    if (type == "(") return cont(pushlex(")"), commasep(expression, ")"), poplex, maybeoperator);
+    if (type == ".") return cont(property, maybeoperator);
+    if (type == "[") return cont(pushlex("]"), expression, expect("]"), poplex, maybeoperator);
+  }
+  function maybelabel(type) {
+    if (type == ":") return cont(poplex, statement);
+    return pass(maybeoperator, expect(";"), poplex);
+  }
+  function property(type) {
+    if (type == "variable") {cx.marked = "property"; return cont();}
+  }
+  function objprop(type) {
+    if (type == "variable") cx.marked = "property";
+    if (atomicTypes.hasOwnProperty(type)) return cont(expect(":"), expression);
+  }
+  function commasep(what, end) {
+    function proceed(type) {
+      if (type == ",") return cont(what, proceed);
+      if (type == end) return cont();
+      return cont(expect(end));
+    }
+    return function commaSeparated(type) {
+      if (type == end) return cont();
+      else return pass(what, proceed);
+    };
+  }
+  function block(type) {
+    if (type == "}") return cont();
+    return pass(statement, block);
+  }
+  function vardef1(type, value) {
+    if (type == "variable"){register(value); return cont(vardef2);}
+    return cont();
+  }
+  function vardef2(type, value) {
+    if (value == "=") return cont(expression, vardef2);
+    if (type == ",") return cont(vardef1);
+  }
+  function forspec1(type) {
+    if (type == "var") return cont(vardef1, forspec2);
+    if (type == ";") return pass(forspec2);
+    if (type == "variable") return cont(formaybein);
+    return pass(forspec2);
+  }
+  function formaybein(type, value) {
+    if (value == "in") return cont(expression);
+    return cont(maybeoperator, forspec2);
+  }
+  function forspec2(type, value) {
+    if (type == ";") return cont(forspec3);
+    if (value == "in") return cont(expression);
+    return cont(expression, expect(";"), forspec3);
+  }
+  function forspec3(type) {
+    if (type != ")") cont(expression);
+  }
+  function functiondef(type, value) {
+    if (type == "variable") {register(value); return cont(functiondef);}
+    if (type == "(") return cont(pushlex(")"), pushcontext, commasep(funarg, ")"), poplex,
statement, popcontext);
+  }
+  function funarg(type, value) {
+    if (type == "variable") {register(value); return cont();}
+  }
+
+  // Interface
+
+  return {
+    startState: function(basecolumn) {
+      return {
+        tokenize: jsTokenBase,
+        reAllowed: true,
+        kwAllowed: true,
+        cc: [],
+        lexical: new JSLexical((basecolumn || 0) - indentUnit, 0, "block", false),
+        localVars: parserConfig.localVars,
+        context: parserConfig.localVars && {vars: parserConfig.localVars},
+        indented: 0
+      };
+    },
+
+    token: function(stream, state) {
+      if (stream.sol()) {
+        if (!state.lexical.hasOwnProperty("align"))
+          state.lexical.align = false;
+        state.indented = stream.indentation();
+      }
+      if (stream.eatSpace()) return null;
+      var style = state.tokenize(stream, state);
+      if (type == "comment") return style;
+      state.reAllowed = !!(type == "operator" || type == "keyword c" || type.match(/^[\[{}\(,;:]$/));
+      state.kwAllowed = type != '.';
+      return parseJS(state, style, type, content, stream);
+    },
+
+    indent: function(state, textAfter) {
+      if (state.tokenize != jsTokenBase) return 0;
+      var firstChar = textAfter && textAfter.charAt(0), lexical = state.lexical;
+      if (lexical.type == "stat" && firstChar == "}") lexical = lexical.prev;
+      var type = lexical.type, closing = firstChar == type;
+      if (type == "vardef") return lexical.indented + 4;
+      else if (type == "form" && firstChar == "{") return lexical.indented;
+      else if (type == "stat" || type == "form") return lexical.indented + indentUnit;
+      else if (lexical.info == "switch" && !closing)
+        return lexical.indented + (/^(?:case|default)\b/.test(textAfter) ? indentUnit : 2
* indentUnit);
+      else if (lexical.align) return lexical.column + (closing ? 0 : 1);
+      else return lexical.indented + (closing ? 0 : indentUnit);
+    },
+
+    electricChars: ":{}"
+  };
+});
+
+CodeMirror.defineMIME("text/javascript", "javascript");
+CodeMirror.defineMIME("application/json", {name: "javascript", json: true});

Added: sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/script-console.css
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/script-console.css?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/script-console.css
(added)
+++ sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/script-console.css
Fri May  4 13:44:53 2012
@@ -0,0 +1,4 @@
+.CodeMirror {
+    /*border-top: 1px solid black;*/
+    border-bottom: 1px solid black;
+}
\ No newline at end of file

Added: sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/script-console.js
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/script-console.js?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/script-console.js
(added)
+++ sling/trunk/contrib/scripting/script-console/src/main/resources/res/ui/script-console.js
Fri May  4 13:44:53 2012
@@ -0,0 +1,104 @@
+//Codemirror editor
+var inputEditor
+var outputEditor
+var OUTPUT_SEP = '\n------------------\n'
+
+function sendData(url, data) {
+    $.ajax({
+        type:"POST",
+        url:url,
+        data:data,
+//        dataType:"json",
+        timeout:30000, //In millis
+        beforeSend:function () {
+            $('#ajaxSpinner').show();
+        },
+        /* error: function() {
+         $('#status').text('Update failed—try again.').slideDown('slow');
+         },*/
+        complete:function () {
+            $('#ajaxSpinner').hide();
+        },
+        success:function (data) {
+            renderData(data)
+        }
+    });
+}
+
+function renderData(data){
+    $('#code-output').show();
+    var currentData = outputEditor.getValue()
+    //If we need to append the data then use below
+    //For now we just replace the existing output
+    //var outData = data + OUTPUT_SEP+ currentData
+
+    outputEditor.setValue(data)
+}
+
+function setUpCodeMirror() {
+    CodeMirror.modeURL = pluginRoot + "/res/ui/codemirror/mode/%N/%N.js";
+    inputEditor = CodeMirror.fromTextArea(document.getElementById("code"), {
+        lineNumbers:true,
+        extraKeys: {
+            'Ctrl-Q':clearOutput,
+            'Ctrl-X':executeScript
+        }
+    });
+    outputEditor = CodeMirror.fromTextArea(document.getElementById("result"), {
+            lineNumbers:true,
+            readOnly:"nocursor"
+        });
+}
+
+function updateWithOption(opt){
+    setLangMode(inputEditor,opt.attr('langMode'))
+    $('[name=lang]').val(opt.val())
+}
+
+function setLangMode(editor, modeName) {
+    if(!modeName){
+        modeName = "text/plain"
+    }else{
+        CodeMirror.autoLoadMode(inputEditor, modeName);
+    }
+    editor.setOption("mode", modeName);
+}
+
+function setUpLangOptions() {
+    var codeLang = $('#codeLang')
+    var options = codeLang.attr('options');
+    codeLang.empty()
+
+    for(var i in scriptConfig){
+        var config = scriptConfig[i]
+        var opt = new Option(config.langName,config.langCode);
+        if(config.mode){
+            opt.langMode = config.mode;
+        }
+        options[options.length] = opt
+    }
+    $('#codeLang').change(function(){
+        var opt = $(this).find(":selected");
+        updateWithOption(opt)
+    });
+
+    $('#codeLang option:eq(0)').attr('selected','selected')
+    updateWithOption($(options[0]))
+}
+
+function executeScript(){
+    inputEditor.save() //Copy the contents to textarea form field
+    sendData(pluginRoot, $("#consoleForm").serialize());
+}
+
+function clearOutput(){
+    outputEditor.setValue('')
+}
+
+$(document).ready(function () {
+    $("#executeButton").click(executeScript);
+    $('#ajaxSpinner').hide();
+    $('#code-output').hide();
+    setUpCodeMirror();
+    setUpLangOptions();
+});

Added: sling/trunk/contrib/scripting/script-console/src/main/resources/templates/script-console.html
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/main/resources/templates/script-console.html?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/main/resources/templates/script-console.html
(added)
+++ sling/trunk/contrib/scripting/script-console/src/main/resources/templates/script-console.html
Fri May  4 13:44:53 2012
@@ -0,0 +1,34 @@
+<script type="text/javascript" src="${pluginRoot}/res/ui/codemirror/lib/codemirror.js"></script>
+<script type="text/javascript" src="${pluginRoot}/res/ui/codemirror/lib/util/runmode.js"></script>
+<script type="text/javascript" src="${pluginRoot}/res/ui/codemirror/lib/util/loadmode.js"></script>
+<script type="text/javascript" src="${pluginRoot}/res/ui/script-console.js"></script>
+<script type="text/javascript">
+// <![CDATA[
+var scriptConfig = ${__scriptConfig__};
+// ]]>
+</script>
+
+<!-- status line -->
+<p class="statline">&nbsp;</p>
+<div class="ui-widget-header ">
+    <img id="ajaxSpinner" src="${appRoot}/res/imgs/loading.gif" tile="Working..." />
+    <button type="button" id="executeButton">${console.execute}</button>
+    <button type="button" id="langButton">${console.lang}</button>
+    <select id="codeLang">
+        <option></option>
+    </select>
+</div>
+<div id="splitter" >
+    <div id="code-input">
+        <form method="post" action="${pluginRoot}" id="consoleForm">
+            <textarea id="code" name="code"
+                      style="width: 90%; height: 7em; "></textarea>
+            <input type="hidden" name="client" value="webconsole" />
+            <input type="hidden" name="lang" value="" />
+        </form>
+    </div>
+    <div id="code-output">
+        <textarea id="result" style="width: 90%;" readonly="yes"></textarea>
+    </div>
+</div>
+

Added: sling/trunk/contrib/scripting/script-console/src/test/java/org/apache/sling/scripting/console/ConsoleTestClient.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/test/java/org/apache/sling/scripting/console/ConsoleTestClient.java?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/test/java/org/apache/sling/scripting/console/ConsoleTestClient.java
(added)
+++ sling/trunk/contrib/scripting/script-console/src/test/java/org/apache/sling/scripting/console/ConsoleTestClient.java
Fri May  4 13:44:53 2012
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.sling.scripting.console;
+
+import org.apache.http.entity.mime.MultipartEntity;
+import org.apache.http.entity.mime.content.InputStreamBody;
+import org.apache.http.entity.mime.content.StringBody;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.sling.testing.tools.http.RequestBuilder;
+import org.apache.sling.testing.tools.http.RequestExecutor;
+import org.junit.Test;
+
+public class ConsoleTestClient {
+    private DefaultHttpClient httpClient = new DefaultHttpClient();
+    private RequestExecutor executor = new RequestExecutor(httpClient);
+
+    @Test
+    public void testResourceResolver() throws Exception {
+        RequestBuilder rb = new RequestBuilder("http://localhost:9000");
+
+        final MultipartEntity entity = new MultipartEntity();
+        // Add Sling POST options
+        entity.addPart("lang", new StringBody("esp"));
+        entity.addPart("code", new InputStreamBody(getClass().getResourceAsStream("/test.js"),
"test.js"));
+        executor.execute(
+                rb.buildPostRequest("/system/console/scriptconsole.json")
+                 .withEntity(entity)
+                 .withCredentials("admin","admin")
+            ).assertStatus(200);
+    }
+}

Added: sling/trunk/contrib/scripting/script-console/src/test/resources/test.groovy
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/test/resources/test.groovy?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/test/resources/test.groovy (added)
+++ sling/trunk/contrib/scripting/script-console/src/test/resources/test.groovy Fri May  4
13:44:53 2012
@@ -0,0 +1,10 @@
+import org.apache.sling.api.resource.ResourceResolverFactory
+
+def resourceResolverFactory = sling.getService(ResourceResolverFactory.class);
+def authInfo = [
+        (ResourceResolverFactory.USER):"admin",
+        (ResourceResolverFactory.PASSWORD):"admin",
+        foo:"bar",
+]
+def resourceResolver = resourceResolverFactory.getResourceResolver(authInfo)
+assert resourceResolver.getAttribute("foo") == "bar"
\ No newline at end of file

Added: sling/trunk/contrib/scripting/script-console/src/test/resources/test.js
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/scripting/script-console/src/test/resources/test.js?rev=1333961&view=auto
==============================================================================
--- sling/trunk/contrib/scripting/script-console/src/test/resources/test.js (added)
+++ sling/trunk/contrib/scripting/script-console/src/test/resources/test.js Fri May  4 13:44:53
2012
@@ -0,0 +1,24 @@
+
+function AssertException(message) { this.message = message; }
+AssertException.prototype.toString = function () {
+  return 'AssertException: ' + this.message;
+}
+
+function assert(exp, message) {
+  if (!exp) {
+    throw new AssertException(message);
+  }
+}
+
+
+var resourceResolverFactory = sling.getService(Packages.org.apache.sling.api.resource.ResourceResolverFactory);
+var authInfo = new java.util.HashMap()
+authInfo.put("user.name","admin")
+authInfo.put("user.password",new java.lang.String("admin").toCharArray())
+authInfo.put("foo","bar")
+
+
+assert(resourceResolverFactory != null, "Factory cannot be null");
+var resourceResolver = resourceResolverFactory.getResourceResolver(authInfo)
+assert(resourceResolver.getAttribute("foo") == "bar", 'Object is null');
+out.println ("test")
\ No newline at end of file



Mime
View raw message