(function() { define('orchard/base',[],function() { var Base; return Base = (function() { function Base() {} Base.include = function(mixinClass) { var key, value, _ref; _ref = mixinClass.prototype; for (key in _ref) { value = _ref[key]; this.prototype[key] = value; } if (mixinClass.included != null) { return mixinClass.included.apply(this); } }; return Base; })(); }); }).call(this); (function() { var __slice = [].slice; define('orchard/event',[],function() { /* Port of MicroEvent in Coffeescript with some naming modifications and a new 'once' method. Original project: https://github.com/jeromeetienne/microevent.js */ var MicroEvent; return MicroEvent = (function() { function MicroEvent() {} MicroEvent.prototype._init = function() { return this._listn || (this._listn = {}); }; MicroEvent.prototype._create = function(e) { return this._init()[e] || (this._init()[e] = []); }; MicroEvent.prototype.on = function(event, callback) { return (this._create(event)).push(callback); }; MicroEvent.prototype.off = function(event, callback) { var listeners; if (event == null) { return this._listn = {}; } else if (callback == null) { this._init(); return delete this._listn[event]; } else { if ((listeners = this._init()[event]) != null) { return listeners.splice(listeners.indexOf(callback), 1); } } }; MicroEvent.prototype.once = function(e, f) { var t, _this = this; return this.on(e, (t = function() { return (_this.off(e, t)) && f.apply(_this, arguments); })); }; MicroEvent.prototype.trigger = function() { var args, e, l, t, _i, _len; e = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : []; if ((t = this._init()[e]) != null) { for (_i = 0, _len = t.length; _i < _len; _i++) { l = t[_i]; l.call.apply(l, [this].concat(__slice.call(args))); } } return 0; }; return MicroEvent; })(); }); }).call(this); (function() { var __slice = [].slice; define('orchard/debug',[],function() { var debug; return debug = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; if ((typeof window !== "undefined" && window !== null ? window.location.hostname : void 0) === "localhost") { return console.debug.apply(console, args); } }; }); }).call(this); (function() { define('orchard/document/text',["orchard/debug"], function(debug) { var DocumentText; return DocumentText = (function() { function DocumentText() {} DocumentText.included = function() { return this.shareJsUpdateHandlers.push("handleTextUpdate"); }; DocumentText.prototype.getValue = function() { var lines; lines = this.doc.at().get().lines.map(function(line) { return line.text; }); return lines.join("\n"); }; DocumentText.prototype.getLineLength = function(lineNo) { return this.doc.at(["lines", lineNo, "text"]).get().length; }; DocumentText.prototype.getText = function(lineNo, position, length) { var text; text = this.doc.at(["lines", lineNo, "text"]).get(); if (position != null) { if (length != null) { return text.slice(position, position + length); } else { return text.slice(position); } } else { return text; } }; DocumentText.prototype.getLine = function(lineNo) { if (lineNo > (this.getLineCount() - 1)) { debug("doc", "line does not exist"); return ""; } return this.doc.at(["lines", lineNo, "text"]).get(); }; DocumentText.prototype.getLines = function() { var line, _i, _len, _ref, _results; _ref = this.doc.at(["lines"]).get(); _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { line = _ref[_i]; _results.push(line.text); } return _results; }; DocumentText.prototype.getLineCount = function(lineNo) { return this.doc.at(["lines"]).get().length; }; DocumentText.prototype.applyChange = function(change) { var applyDeletes, applyInserts, i, lastInserted, nlines, text, _i, _len, _ref, _results, _this = this; debug("doc", "applying change", change); applyDeletes = function() { var beginningOfLine, deleteStartPosition, deletedTextLength, firstLine, i, isFirstLine, isLastLine, lastLine, lineNo, nextLine, _i, _results; firstLine = change.from.line; lastLine = change.to.line; nextLine = firstLine + 1; _results = []; for (lineNo = _i = firstLine; firstLine <= lastLine ? _i <= lastLine : _i >= lastLine; lineNo = firstLine <= lastLine ? ++_i : --_i) { i = lineNo - firstLine; deleteStartPosition = change.from.ch; deletedTextLength = change.deleted[i].length; beginningOfLine = 0; isFirstLine = lineNo === firstLine; isLastLine = lineNo === lastLine; if (isFirstLine) { if (deletedTextLength !== 0) { _results.push(_this.deleteText(firstLine, deleteStartPosition, deletedTextLength)); } else { _results.push(void 0); } } else if (isLastLine) { if (deletedTextLength !== 0) { _this.deleteText(nextLine, beginningOfLine, deletedTextLength); } _results.push(_this.concatLineWithNext(firstLine)); } else { _results.push(_this.deleteLine(nextLine)); } } return _results; }; applyInserts = function() { var beginningOfLine, firstLine, firstLineEndLength, firstLineEndPosition, firstLineEndText, i, insertStartPosition, insertedText, isFirstLine, isLastLine, lastLine, nextLine, _i, _len, _ref, _results; firstLine = change.from.line; lastLine = firstLine + change.inserted.length - 1; _ref = change.inserted; _results = []; for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { insertedText = _ref[i]; nextLine = firstLine + i; insertStartPosition = change.from.ch; beginningOfLine = 0; isFirstLine = i === 0; isLastLine = i === change.inserted.length - 1; if (isFirstLine) { if (insertedText.length !== 0) { _results.push(_this.insertText(firstLine, insertStartPosition, insertedText)); } else { _results.push(void 0); } } else if (isLastLine) { firstLineEndPosition = change.from.ch + change.inserted[0].length; firstLineEndLength = _this.getLineLength(firstLine) - firstLineEndPosition; firstLineEndText = _this.getText(firstLine, firstLineEndPosition, firstLineEndLength); if (firstLineEndLength !== 0) { _this.deleteText(firstLine, firstLineEndPosition, firstLineEndLength); } _results.push(_this.insertLine(lastLine, insertedText + firstLineEndText)); } else { _results.push(_this.insertLine(nextLine, insertedText)); } } return _results; }; lastInserted = change.inserted[change.inserted.length - 1]; if (change.from.ch === 0 && change.to.ch === 0 && lastInserted === "") { nlines = change.to.line - change.from.line; debug("doc", "whole lines replace", change, nlines); if (nlines > 0) { this.deleteLines(change.from.line, nlines); } _ref = change.inserted.slice(0, -1); _results = []; for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { text = _ref[i]; _results.push(this.insertLine(change.from.line + i, text)); } return _results; } else { applyDeletes(); return applyInserts(); } }; DocumentText.prototype.deleteText = function(lineNo, from, length, callback) { var line, removedText, text; if (callback == null) { callback = function(error) {}; } if (length > 0) { debug("doc", "deleting text", { line: lineNo, ch: from, length: length }); line = this.doc.at(["lines", lineNo]); text = line.at(["text"]); removedText = text.get().slice(from, from + length); return text.del(from, length, function(error) { return callback(error, removedText); }); } }; DocumentText.prototype.deleteLines = function(from, number) { var lineNo, to, _i, _ref, _results; debug("doc", "deleting lines", { from: from, number: number }); if (number === 0) { return; } to = from + number; _results = []; for (lineNo = _i = _ref = to - 1; _ref <= from ? _i <= from : _i >= from; lineNo = _ref <= from ? ++_i : --_i) { _results.push(this.deleteLine(lineNo)); } return _results; }; DocumentText.prototype.deleteLine = function(lineNo, callback) { var line; if (callback == null) { callback = function(error) {}; } debug("doc", "deleting line", { line: lineNo }); line = this.doc.at(["lines", lineNo]); return line.remove(callback); }; DocumentText.prototype.concatLineWithNext = function(lineNo, callback) { var endOfFirstLine, firstLine, firstText, secondLine, secondText; if (callback == null) { callback = function(error) {}; } debug("doc", "concatting line", { line: lineNo }); firstLine = this.doc.at(["lines", lineNo]); firstText = firstLine.at(["text"]); secondLine = this.doc.at(["lines", lineNo + 1]); secondText = secondLine.at(["text"]); endOfFirstLine = firstText.get().length; return firstText.insert(endOfFirstLine, secondText.get(), function(error) { if (error != null) { return callback(error); } return secondLine.remove(callback); }); }; DocumentText.prototype.insertText = function(lineNo, position, rawText, callback) { var line, text; if (callback == null) { callback = function(error) {}; } if (rawText.length > 0) { debug("doc", "inserting text", { line: lineNo, text: rawText, ch: position }); line = this.doc.at(["lines", lineNo]); text = line.at(["text"]); return text.insert(position, rawText, callback); } }; DocumentText.prototype.insertLine = function(lineNo, text, callback) { var lines; if (callback == null) { callback = function(error) {}; } debug("doc", "inserting line", { line: lineNo, text: text }); lines = this.doc.at(["lines"]); return lines.insert(lineNo, { text: text, annotations: [] }, callback); }; DocumentText.prototype.handleTextUpdate = function(path, op) { var annotation, ch, change, deletedText, insertedText, isLineDeletion, isLineInsertion, isLineModification, isTextDeletion, isTextInsertion, isTextModification, line, _i, _j, _len, _len1, _lines, _ref, _ref1, _text; isTextModification = path.length === 4 && path[0] === "lines" && path[2] === "text"; isLineModification = path.length === 2 && path[0] === "lines"; isTextInsertion = op.si != null; isTextDeletion = op.sd != null; isLineInsertion = op.li != null; isLineDeletion = op.ld != null; if (isTextModification) { _lines = path[0], line = path[1], _text = path[2], ch = path[3]; if (isTextInsertion) { insertedText = op.si; change = { inserted: [insertedText], deleted: [], from: { line: line, ch: ch }, to: { line: line, ch: ch } }; this.trigger("text:insert", { line: line, ch: ch, text: insertedText }); this.trigger("change", change); return true; } else if (isTextDeletion) { deletedText = op.sd; change = { inserted: [], deleted: [deletedText], from: { line: line, ch: ch }, to: { line: line, ch: ch + deletedText.length } }; this.trigger("text:remove", { line: line, ch: ch, text: deletedText }); this.trigger("change", change); return true; } else { throw new Error("Unknown op"); } } else if (isLineModification) { _lines = path[0], line = path[1]; if (isLineDeletion) { change = { inserted: [], deleted: [op.ld.text, ""], from: { line: line, ch: 0 }, to: { line: line + 1, ch: 0 } }; this.trigger("line:remove", { line: line, text: op.ld.text }); this.trigger("change", change); _ref = op.ld.annotations || []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { annotation = _ref[_i]; this.trigger("annotation:remove", annotation, line); } return true; } else if (isLineInsertion) { change = { inserted: [op.li.text, ""], deleted: [], from: { line: line, ch: 0 }, to: { line: line, ch: 0 } }; this.trigger("line:insert", { line: line, text: op.li.text }); this.trigger("change", change); _ref1 = op.li.annotations || []; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { annotation = _ref1[_j]; this.trigger("annotation:new", annotation, line); } return true; } else { throw new Error("Unknown op"); } } else { return false; } }; return DocumentText; })(); }); }).call(this); (function() { define('orchard/document/raw',["orchard/debug"], function(debug) { var DocumentRaw; return DocumentRaw = (function() { function DocumentRaw() {} DocumentRaw.prototype.getRawRange = function(from, to) { var annotation, lastLine, lines, truncatedAnnotations, _i, _j, _len, _len1, _ref, _ref1; if (from.line > to.line) { return []; } lines = this.doc.at(["lines"]).get().slice(from.line, to.line + 1); if (lines.length === 0) { return []; } lastLine = lines[lines.length - 1]; truncatedAnnotations = []; _ref = lastLine.annotations; for (_i = 0, _len = _ref.length; _i < _len; _i++) { annotation = _ref[_i]; if ((annotation.to != null) && annotation.to <= to.ch) { truncatedAnnotations.push(annotation); } } lines[lines.length - 1] = { text: lastLine.text.slice(0, to.ch), annotations: truncatedAnnotations }; truncatedAnnotations = []; _ref1 = lines[0].annotations; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { annotation = _ref1[_j]; if ((annotation.from != null) && annotation.from >= from.ch) { annotation.from -= from.ch; annotation.to -= from.ch; truncatedAnnotations.push(annotation); } } lines[0] = { text: lines[0].text.slice(from.ch), annotations: truncatedAnnotations }; return lines; }; DocumentRaw.prototype.insertRawRange = function(position, lines) { var _this = this; if (lines.length === 0) { return; } if (lines.length === 1) { return this.insertRawText(position, lines[0]); } else { return this.asSingleOp(function() { var i, line, _i, _len, _ref, _results; _this.splitLine(position.line, position.ch); _this.insertRawText({ line: position.line, ch: position.ch }, lines[0]); _this.insertRawText({ line: position.line + 1, ch: 0 }, lines[lines.length - 1]); _ref = lines.slice(1, -1); _results = []; for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { line = _ref[i]; _results.push(_this.insertRawLine(position.line + i + 1, line)); } return _results; }); } }; DocumentRaw.prototype.insertRawLine = function(lineNo, line) { var lines; if (line == null) { line = { text: "", annotations: [] }; } lines = this.doc.at(["lines"]); return lines.insert(lineNo, line); }; DocumentRaw.prototype.insertRawText = function(position, data) { var annotations, line, text; if (position == null) { position = { line: 0, ch: 0 }; } if (data == null) { data = { text: "", annotations: [] }; } line = this.doc.at(["lines", position.line]); text = line.at(["text"]); annotations = line.at(["annotations"]); return this.asSingleOp(function() { var annotation, i, _i, _j, _len, _len1, _ref, _ref1, _results; text.insert(position.ch, data.text); _ref = annotations.get(); for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { annotation = _ref[i]; if ((annotation.from != null) && (annotation.to != null) && annotation.from >= position.ch) { annotations.at([i, "from"]).add(data.text.length); annotations.at([i, "to"]).add(data.text.length); } } _ref1 = data.annotations; _results = []; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { annotation = _ref1[_j]; annotation.from = annotation.from + position.ch; annotation.to = annotation.to + position.ch; _results.push(annotations.insert(annotations.get().length, annotation)); } return _results; }); }; DocumentRaw.prototype.splitLine = function(lineNo, position) { var annotation, currentAnnotations, currentLine, i, newLine, removeAnnotationIndices, _i, _len, _ref, _this = this; currentLine = this.doc.at(["lines", lineNo]); newLine = { text: currentLine.get().text.slice(position), annotations: [] }; currentAnnotations = currentLine.at(["annotations"]); removeAnnotationIndices = []; _ref = currentAnnotations.get(); for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { annotation = _ref[i]; if ((annotation.from != null) && (annotation.to != null) && annotation.from >= position) { annotation.from -= position; annotation.to -= position; newLine.annotations.push(annotation); removeAnnotationIndices.push(i); } } return this.asSingleOp(function() { var _j, _len1, _ref1; currentLine.at(["text"]).del(position, newLine.text.length); _ref1 = removeAnnotationIndices.reverse(); for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { i = _ref1[_j]; currentAnnotations.at([i]).remove(); } return _this.insertRawLine(lineNo + 1, newLine); }); }; DocumentRaw.prototype.getRawSnapshot = function() { return this.doc.at(["lines"]).get(); }; return DocumentRaw; })(); }); }).call(this); (function() { define('orchard/document/annotations',["orchard/debug"], function(debug) { var DocumentAnnotations; return DocumentAnnotations = (function() { function DocumentAnnotations() {} DocumentAnnotations.included = function() { return this.shareJsUpdateHandlers.push("handleAnnotationUpdate"); }; DocumentAnnotations.prototype.handleAnnotationUpdate = function(path, op) { var annotation, annotationIndex, isAnnotationDeletion, isAnnotationInsert, isSubdoc, lineNo, _annotations, _lines; isAnnotationInsert = path.length === 4 && path[0] === "lines" && path[2] === "annotations" && (op.li != null); isAnnotationDeletion = path.length === 4 && path[0] === "lines" && path[2] === "annotations" && (op.ld != null); isSubdoc = path.length >= 7 && path[0] === "lines" && path[2] === "annotations" && path[4] === "data" && path[6] === "lines"; debug("subdoc", isSubdoc); if (isAnnotationInsert) { _lines = path[0], lineNo = path[1], _annotations = path[2], annotationIndex = path[3]; annotation = op.li; this.trigger("annotation:new", annotation, lineNo); return true; } else if (isAnnotationDeletion) { _lines = path[0], lineNo = path[1], _annotations = path[2], annotationIndex = path[3]; annotation = op.ld; this.trigger("annotation:remove", annotation, lineNo); return true; } else if (isSubdoc) { return true; } else { return false; } }; DocumentAnnotations.prototype.getAnnotations = function() { var annotation, annotations, line, lineNo, _i, _j, _len, _len1, _ref, _ref1; annotations = []; _ref = this.doc.at(["lines"]).get() || []; for (lineNo = _i = 0, _len = _ref.length; _i < _len; lineNo = ++_i) { line = _ref[lineNo]; _ref1 = line.annotations || []; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { annotation = _ref1[_j]; annotations.push([annotation, lineNo]); } } return annotations; }; DocumentAnnotations.prototype.getAnnotationsOnLine = function(lineNo) { return this.doc.at(["lines", lineNo, "annotations"]).get(); }; DocumentAnnotations.prototype.findAnnotation = function(annotationId) { var annotation, annotationIndex, annotationsLength, lineNo, rawAnnotation, _i, _j, _ref, _ref1; for (lineNo = _i = 0, _ref = this.getLineCount() - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; lineNo = 0 <= _ref ? ++_i : --_i) { annotationsLength = this.doc.at(["lines", lineNo, "annotations"]).get().length; for (annotationIndex = _j = 0, _ref1 = annotationsLength - 1; 0 <= _ref1 ? _j <= _ref1 : _j >= _ref1; annotationIndex = 0 <= _ref1 ? ++_j : --_j) { annotation = this.doc.at(["lines", lineNo, "annotations", annotationIndex]); rawAnnotation = annotation.get(); if ((rawAnnotation != null) && rawAnnotation.id === annotationId) { return [annotation, lineNo]; } } } return [null, null]; }; DocumentAnnotations.prototype._createAnnotationId = function() { return "xxxxxxxx".replace(/x/g, function() { return (Math.random() * 36 | 0).toString(36); }); }; DocumentAnnotations.prototype.recreateAnnotationIds = function(lines) { var annotation, line, _i, _len, _results; _results = []; for (_i = 0, _len = lines.length; _i < _len; _i++) { line = lines[_i]; _results.push((function() { var _j, _len1, _ref, _results1; _ref = line.annotations; _results1 = []; for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) { annotation = _ref[_j]; _results1.push(annotation.id = this._createAnnotationId()); } return _results1; }).call(this)); } return _results; }; return DocumentAnnotations; })(); }); }).call(this); (function() { define('orchard/document/line-annotations',["orchard/debug"], function(debug) { var DocumentLineAnnotations; return DocumentLineAnnotations = (function() { function DocumentLineAnnotations() {} DocumentLineAnnotations.prototype.addLineAnnotation = function(lineNo, className, annotationId) { var annotations; if (annotationId == null) { annotationId = this._createAnnotationId(); } debug("doc", "adding line annotation new", { line: lineNo, "class": className, id: annotationId }); annotations = this.doc.at(["lines", lineNo, "annotations"]); annotations.push({ type: "line", "class": className, id: annotationId }); return annotationId; }; DocumentLineAnnotations.prototype.removeLineAnnotation = function(annotationId) { var annotation, lineNo, _ref; debug("doc", "removing line annotation", { id: annotationId }); _ref = this.findAnnotation(annotationId), annotation = _ref[0], lineNo = _ref[1]; return annotation.remove(); }; DocumentLineAnnotations.prototype.getLineAnnotationsOnLine = function(lineNo) { var annotation, annotations, _i, _len, _ref; debug("doc", "getting annotations on line", { line: lineNo }); if (lineNo > (this.getLineCount() - 1)) { debug("doc", "line does not exist"); return []; } annotations = []; _ref = this.doc.at(["lines", lineNo, "annotations"]).get() || []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { annotation = _ref[_i]; if (annotation.type === "line") { annotations.push(annotation); } } return annotations; }; return DocumentLineAnnotations; })(); }); }).call(this); (function() { var __slice = [].slice; define('orchard/document/widgets',["orchard/debug"], function(debug) { var DocumentWidgets; return DocumentWidgets = (function() { function DocumentWidgets() {} DocumentWidgets.included = function() { return this.shareJsUpdateHandlers.push("handleWidgetDataUpdate"); }; DocumentWidgets.prototype.handleWidgetDataUpdate = function(path, op) { var annotation, annotationIndex, isWidgetDataUpdate, lineNo, _annotations, _lines; isWidgetDataUpdate = path.length > 5 && path[0] === "lines" && path[2] === "annotations" && path[4] === "data"; if (isWidgetDataUpdate) { _lines = path[0], lineNo = path[1], _annotations = path[2], annotationIndex = path[3]; annotation = this.doc.at(["lines", lineNo, "annotations", annotationIndex]).get(); this.trigger("annotation:update", annotation, lineNo); return true; } else { return false; } }; DocumentWidgets.prototype.addWidget = function(lineNo, className, data, annotationId) { var annotations; if (annotationId == null) { annotationId = this._createAnnotationId(); } debug("doc", "adding widget", { line: lineNo, "class": className, id: annotationId }); annotations = this.doc.at(["lines", lineNo, "annotations"]); annotations.push({ type: "widget", "class": className, id: annotationId, data: data }); return annotationId; }; DocumentWidgets.prototype.removeWidget = function(annotationId) { var annotation, lineNo, _ref; debug("doc", "removing widget", { id: annotationId }); _ref = this.findAnnotation(annotationId), annotation = _ref[0], lineNo = _ref[1]; if (annotation == null) { return; } return annotation.remove(); }; DocumentWidgets.prototype.getWidgetDataSubdoc = function(annotationId, path) { var annotation, lineNo, subdoc, _ref, _this = this; _ref = this.findAnnotation(annotationId), annotation = _ref[0], lineNo = _ref[1]; if (annotation == null) { return; } subdoc = annotation.at(["data"]).at(path); subdoc._getCurrentPath = function() { return _this.getWidgetDataSubdocPath(annotationId, path); }; return subdoc; }; DocumentWidgets.prototype.getWidgetDataSubdocPath = function(annotationId, path) { var annotation, lineNo, _ref; _ref = this.findAnnotation(annotationId), annotation = _ref[0], lineNo = _ref[1]; if (annotation == null) { return; } return annotation.path.concat(["data"].concat(__slice.call(path))); }; DocumentWidgets.prototype.getWidgetLineNo = function(annotationId) { var annotation, lineNo, _ref; _ref = this.findAnnotation(annotationId), annotation = _ref[0], lineNo = _ref[1]; return lineNo; }; DocumentWidgets.prototype.setWidgetAttribute = function(annotationId, attribute, value) { var annotation, lineNo, _ref; _ref = this.findAnnotation(annotationId), annotation = _ref[0], lineNo = _ref[1]; if (annotation == null) { return; } return annotation.at(["data", attribute]).set(value); }; return DocumentWidgets; })(); }); }).call(this); (function() { define('orchard/document/inline-widgets',["orchard/debug"], function(debug) { var DocumentInlineWidgets; return DocumentInlineWidgets = (function() { function DocumentInlineWidgets() {} DocumentInlineWidgets.prototype.addInlineWidget = function(lineNo, from, to, className, data, annotationId) { var annotations; if (annotationId == null) { annotationId = this._createAnnotationId(); } annotations = this.doc.at(["lines", lineNo, "annotations"]); annotations.push({ type: "inline-widget", "class": className, id: annotationId, data: data, from: from, to: to }); return annotationId; }; DocumentInlineWidgets.prototype.moveInlineWidget = function(annotationId, from, to) { var annotation, fromDelta, lineNo, rawAnnotation, toDelta, _ref; _ref = this.findAnnotation(annotationId), annotation = _ref[0], lineNo = _ref[1]; rawAnnotation = annotation.get(); fromDelta = from - rawAnnotation.from; toDelta = to - rawAnnotation.to; if (fromDelta !== 0) { annotation.at(["from"]).add(fromDelta); } if (toDelta !== 0) { return annotation.at(["to"]).add(toDelta); } }; DocumentInlineWidgets.prototype.removeInlineWidget = function(annotationId) { var annotation, lineNo, _ref; debug("doc", "removing inline widget", { id: annotationId }); _ref = this.findAnnotation(annotationId), annotation = _ref[0], lineNo = _ref[1]; return annotation.remove(); }; DocumentInlineWidgets.prototype.getInlineWidgetsOnLine = function(lineNo) { var annotation, widgets, _i, _len, _ref; debug("doc", "getting inline widgets on line", { line: lineNo }); if (lineNo > (this.getLineCount() - 1)) { debug("doc", "line does not exist"); return []; } widgets = []; _ref = this.doc.at(["lines", lineNo, "annotations"]).get() || []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { annotation = _ref[_i]; if (annotation.type === "inline-widget") { widgets.push(annotation); } } return widgets; }; DocumentInlineWidgets.prototype.getInlineWidgetPosition = function(annotationId) { var annotation, lineNo, _ref; _ref = this.findAnnotation(annotationId), annotation = _ref[0], lineNo = _ref[1]; return annotation.get().from; }; return DocumentInlineWidgets; })(); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, __slice = [].slice; define('orchard/document',["orchard/base", "orchard/event", "orchard/document/text", "orchard/document/raw", "orchard/document/annotations", "orchard/document/line-annotations", "orchard/document/widgets", "orchard/document/inline-widgets", "orchard/debug"], function(Base, Event, DocumentText, DocumentRaw, DocumentAnnotations, DocumentLineAnnotations, DocumentWidgets, DocumentInlineWidgets, debug) { var Document; return Document = (function(_super) { __extends(Document, _super); Document.include(Event); Document.shareJsUpdateHandlers = []; Document.include(DocumentText); Document.include(DocumentRaw); Document.include(DocumentAnnotations); Document.include(DocumentLineAnnotations); Document.include(DocumentWidgets); Document.include(DocumentInlineWidgets); function Document(subdoc) { if (subdoc != null) { this.doc = subdoc; } this.shareJsUpdateHandlers = Document.shareJsUpdateHandlers; } Document.prototype.detach = function() {}; Document.prototype.initialize = function(callback) { var init, _this = this; if (callback == null) { callback = function(error) {}; } init = function(error, doc) { _this.doc = doc; if (error != null) { return callback(error); } _this._initializeDynamicPath(); if (_this.doc.at().get() == null) { _this.doc.at().set({}); } if ((_this.doc.at(["lines"]).get() == null) || _this.doc.at(["lines"]).get().length === 0) { _this.doc.at(["lines"]).set([ { text: "", annotations: [] } ]); } _this.bindToDocEvents(); return callback(); }; if (this.doc == null) { return sharejs.open("test", "json", init); } else { return init(null, this.doc); } }; Document.prototype.bindToDocEvents = function() { var submitOp, _this = this; this.doc.at().on("child op", function(path, op) { var i, part, _i, _len, _ref; if (_this.doc._getCurrentPath != null) { _this.doc.path = _this.doc._getCurrentPath(); } if (_this.doc.path != null) { _ref = _this.doc.path; for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { part = _ref[i]; if (part !== op.p[i]) { return; } } } _this.trigger("op:remote", op); return _this.triggerChangeEvents(path, op); }); submitOp = this.doc.submitOp; return this.doc.submitOp = function(op) { var atomOp, _i, _len; _this.trigger("op:local", op); if (_this._triggerChangeEvents) { for (_i = 0, _len = op.length; _i < _len; _i++) { atomOp = op[_i]; _this.triggerChangeEvents(atomOp.p, atomOp); } } return submitOp.apply(_this.doc, arguments); }; }; Document.prototype.triggeringEvents = function(operation) { if (operation == null) { operation = function() {}; } this._triggerChangeEvents = true; operation(); return delete this._triggerChangeEvents; }; Document.prototype.triggerChangeEvents = function(path, op) { var handled, handler, _i, _len, _ref, _ref1; debug("child op", path, op); handled = false; _ref = this.shareJsUpdateHandlers; for (_i = 0, _len = _ref.length; _i < _len; _i++) { handler = _ref[_i]; handled = (_ref1 = this[handler]) != null ? _ref1.call(this, path, op) : void 0; if (handled) { break; } } if (!handled) { throw new Error("Unknown path"); } return true; }; Document.prototype.topLevelDoc = function() { var doc; if (this.doc.doc != null) { return doc = this.doc.doc; } else { return doc = this.doc; } }; Document.prototype.applyRawOp = function(op) { return this.doc.submitOp(op); }; Document.prototype.revertRawOp = function(op) { return this.applyRawOp(sharejs.types.json.invert(op)); }; Document.prototype.startBufferingOps = function() { var doc, _this = this; doc = this.topLevelDoc(); doc._bufferedOpsStack || (doc._bufferedOpsStack = 0); if (doc._bufferedOpsStack === 0) { doc._submitOp = doc.submitOp; doc._bufferedOps = []; doc.submitOp = function(op, callback) { if (callback == null) { callback = function(error) {}; } doc.snapshot = doc.type.apply(doc.snapshot, op); doc._bufferedOps = doc._bufferedOps.concat(op); return callback(); }; } return doc._bufferedOpsStack++; }; Document.prototype.submitBufferedOps = function(callback) { var doc, _this = this; if (callback == null) { callback = function(error) {}; } doc = this.topLevelDoc(); doc._bufferedOpsStack--; if (doc._bufferedOpsStack === 0) { doc.submitOp = doc._submitOp; doc.type._apply = doc.type.apply; doc.type.apply = function() { return doc.snapshot; }; if (doc._bufferedOps.length > 0) { doc.submitOp(doc._bufferedOps, callback); } return doc.type.apply = doc.type._apply; } }; Document.prototype.asSingleOp = function(operation) { if (operation == null) { operation = function() {}; } this.startBufferingOps(); operation(); return this.submitBufferedOps(); }; Document.prototype._initializeDynamicPath = function() { if ((this.doc.path != null) && (this.doc._getCurrentPath != null)) { this.doc._at = this.doc.at; return this.doc.at = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; this.path = this._getCurrentPath(); return this._at.apply(this, args); }; } }; return Document; })(Base); }); }).call(this); (function() { define('orchard/codemirror/line-annotations',["orchard/debug"], function(debug) { var CodeMirrorLineAnnotations; return CodeMirrorLineAnnotations = (function() { function CodeMirrorLineAnnotations() {} CodeMirrorLineAnnotations.prototype.addLineAnnotation = function(line, className, annotationId) { var handle; debug("codemirror", "add line annotation", { line: line, "class": className, id: annotationId }); handle = this.codemirror.getLineHandle(line); handle.annotationIds || (handle.annotationIds = []); handle.classes || (handle.classes = []); handle.annotationIds.push(annotationId); handle.classes.push(className); return this.codemirror.addLineClass(handle, "text", "oe-" + className); }; CodeMirrorLineAnnotations.prototype.updateLineAnnotation = function(annotationId, newLineNo) { var annotation, hasChanged, lineHandle, oldLineNo, _ref; _ref = this._findLineHandleWithAnnotationId(annotationId), lineHandle = _ref[0], oldLineNo = _ref[1]; if (lineHandle == null) { return; } hasChanged = oldLineNo !== newLineNo; debug("codemirror", "updating line annotation", { id: annotationId, oldLine: oldLineNo, newLine: newLineNo, changed: hasChanged }); if (!hasChanged) { return; } annotation = this._removeLineAnnotationFromLineHandle(lineHandle, oldLineNo, annotationId); return this.addLineAnnotation(newLineNo, annotation["class"], annotationId); }; CodeMirrorLineAnnotations.prototype.removeLineAnnotation = function(annotationId) { var annotation, lineHandle, lineNo, _ref; debug("codemirror", "removing line annotation", annotationId); _ref = this._findLineHandleWithAnnotationId(annotationId), lineHandle = _ref[0], lineNo = _ref[1]; if (lineHandle == null) { return; } annotation = this._removeLineAnnotationFromLineHandle(lineHandle, lineNo, annotationId); return annotation; }; CodeMirrorLineAnnotations.prototype.getLineAnnotationsOnLine = function(line) { var annotationId, annotations, i, _i, _len, _ref; if (typeof line === "number") { line = this.codemirror.getLineHandle(line); } annotations = []; _ref = line.annotationIds || []; for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) { annotationId = _ref[i]; annotations.push({ id: annotationId, type: "line", line: this.codemirror.getLineNumber(line), "class": line.classes[i] }); } return annotations; }; CodeMirrorLineAnnotations.prototype.getAllLineAnnotations = function() { var annotations, line, lineNo; lineNo = 0; annotations = []; while ((line = this.codemirror.getLineHandle(lineNo))) { annotations = annotations.concat(this.getLineAnnotationsOnLine(line)); lineNo++; } return annotations; }; CodeMirrorLineAnnotations.prototype.getLineHandle = function(lineNo) { return this.codemirror.getLineHandle(lineNo); }; CodeMirrorLineAnnotations.prototype.getLineNumber = function(lineHandle) { return this.codemirror.getLineNumber(lineHandle); }; CodeMirrorLineAnnotations.prototype._findLineHandleWithAnnotationId = function(annotationId) { var line, lineNo; lineNo = 0; while ((line = this.codemirror.getLineHandle(lineNo))) { if ((line.annotationIds != null) && line.annotationIds.indexOf(annotationId) > -1) { return [line, lineNo]; } lineNo++; } return [null, null]; }; CodeMirrorLineAnnotations.prototype._removeLineAnnotationFromLineHandle = function(lineHandle, lineNo, annotationId) { var className, index; debug("codemirror", "removing annotation from line handle", { handle: lineHandle, line: lineNo, id: annotationId }); if (lineHandle.annotationIds == null) { return; } if (lineHandle.classes == null) { return; } index = lineHandle.annotationIds.indexOf(annotationId); if (!(index > -1)) { return; } className = lineHandle.classes[index]; lineHandle.annotationIds.splice(index, 1); lineHandle.classes.splice(index, 1); this.codemirror.removeLineClass(lineHandle, "text", "oe-" + className); return { type: "line", id: annotationId, "class": className, line: lineNo }; }; return CodeMirrorLineAnnotations; })(); }); }).call(this); (function() { define('orchard/codemirror/visual-markers',["orchard/debug"], function(debug) { var CodeMirrorVisualMarkers; return CodeMirrorVisualMarkers = (function() { function CodeMirrorVisualMarkers() {} CodeMirrorVisualMarkers.prototype.addVisualMarker = function(from, to, className, options) { if (options == null) { options = {}; } options.className = "oe-" + className; options.visual = true; return this.codemirror.markText(from, to, options); }; CodeMirrorVisualMarkers.prototype.getVisualMarkersOnLine = function(lineNo) { var marker, visualMarkers, _i, _len, _ref; visualMarkers = []; _ref = this.getMarkersOnLine(lineNo); for (_i = 0, _len = _ref.length; _i < _len; _i++) { marker = _ref[_i]; if (marker.visual) { visualMarkers.push(marker); } } return visualMarkers; }; return CodeMirrorVisualMarkers; })(); }); }).call(this); (function() { define('orchard/codemirror/widgets',["orchard/debug"], function(debug) { var CodeMirrorWidgets; return CodeMirrorWidgets = (function() { function CodeMirrorWidgets() {} CodeMirrorWidgets.prototype.addWidget = function(lineNo, element, annotationId) { var lineHandle, onChange, widget, _this = this; widget = this.codemirror.addLineWidget(lineNo, element); widget.annotationId = annotationId; this.codemirror.addLineClass(lineNo, "text", "oe-widget-line"); lineHandle = widget.line; onChange = function() { if (lineHandle.parent == null) { debug("codemirror", "widget removed", widget); _this.trigger("annotation:remove", { type: "widget", id: widget.annotationId }); return _this.codemirror.off("change", onChange); } }; return this.codemirror.on("change", onChange); }; CodeMirrorWidgets.prototype.removeWidget = function(annotationId) { var lineNo, widget, _ref; debug("codemirror", "removing widget", annotationId); _ref = this._findLineWidgetWithAnnotationId(annotationId), widget = _ref[0], lineNo = _ref[1]; if (widget == null) { return; } widget.clear(); this.codemirror.removeLineClass(widget.line, "text", "oe-widget-line"); return this.trigger("annotation:remove", { type: "widget", id: annotationId }); }; CodeMirrorWidgets.prototype.markWidgetElementAsChanged = function(annotationId) { var lineNo, widget, _ref; debug("codemirror", "marking widget element as changed", annotationId); _ref = this._findLineWidgetWithAnnotationId(annotationId), widget = _ref[0], lineNo = _ref[1]; debug("codemirror", widget); if (widget == null) { return; } return widget.changed(); }; CodeMirrorWidgets.prototype._findLineWidgetWithAnnotationId = function(annotationId) { var line, lineNo, widget, _i, _len, _ref; debug("codemirror", "WARNING FULL DOCUMENT SCAN OF LINE WIDGETS"); lineNo = 0; while ((line = this.codemirror.getLineHandle(lineNo))) { _ref = line.widgets || []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { widget = _ref[_i]; if (widget.annotationId === annotationId) { return [widget, lineNo]; } } lineNo++; } return [null, null]; }; return CodeMirrorWidgets; })(); }); }).call(this); (function() { define('orchard/codemirror/inline-widgets',["orchard/debug"], function(debug) { var CodeMirrorInlineWidgets; return CodeMirrorInlineWidgets = (function() { function CodeMirrorInlineWidgets() {} CodeMirrorInlineWidgets.prototype.addInlineWidget = function(lineNo, from, to, element, annotationId) { var options; options = { inlineWidget: true, replacedWith: element, annotationId: annotationId }; debug("codemirror", "adding inline widget", options, lineNo, from, to); return this.codemirror.markText({ line: lineNo, ch: from }, { line: lineNo, ch: to }, options); }; CodeMirrorInlineWidgets.prototype.removeInlineWidget = function(annotationId) { var widget; widget = this._findInlineWidgetWithAnnotationId(annotationId); return widget != null ? widget.clear() : void 0; }; CodeMirrorInlineWidgets.prototype.getInlineWidgetsOnLine = function(line) { var markedSpan, marker, pos, widgets, _i, _len, _ref, _ref1; if (typeof line === "number") { line = this.codemirror.getLineHandle(line); } widgets = []; _ref = line.markedSpans || []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { markedSpan = _ref[_i]; if ((markedSpan.marker != null) && (((_ref1 = markedSpan.marker) != null ? _ref1.inlineWidget : void 0) != null)) { marker = markedSpan.marker; pos = marker.find(); widgets.push({ id: marker.annotationId, type: "inline-widget", line: this.codemirror.getLineNumber(line), from: pos != null ? pos.from : void 0, to: pos != null ? pos.to : void 0 }); } } return widgets; }; CodeMirrorInlineWidgets.prototype.markInlineWidgetElementAsChanged = function(annotationId) { var widget; debug("codemirror", "marking inline widget element as changed", annotationId); widget = this._findInlineWidgetWithAnnotationId(annotationId); debug("codemirror", widget); if (widget == null) { return; } return widget.changed(); }; CodeMirrorInlineWidgets.prototype._findInlineWidgetWithAnnotationId = function(annotationId) { var marker, _i, _len, _ref; debug("codemirror", "WARNING FULL DOCUMENT SCAN OF INLINE WIDGETS"); _ref = this.codemirror.getAllMarks(); for (_i = 0, _len = _ref.length; _i < _len; _i++) { marker = _ref[_i]; if (marker.annotationId === annotationId) { return marker; } } return null; }; return CodeMirrorInlineWidgets; })(); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define('orchard/codemirror',["orchard/base", "orchard/event", "orchard/codemirror/line-annotations", "orchard/codemirror/visual-markers", "orchard/codemirror/widgets", "orchard/codemirror/inline-widgets", "orchard/debug"], function(Base, Event, CodeMirrorLineAnnotations, CodeMirrorVisualMarkers, CodeMirrorWidgets, CodeMirrorInlineWidgets, debug) { var CodeMirrorInterface; return CodeMirrorInterface = (function(_super) { __extends(CodeMirrorInterface, _super); CodeMirrorInterface.include(Event); CodeMirrorInterface.include(CodeMirrorLineAnnotations); CodeMirrorInterface.include(CodeMirrorVisualMarkers); CodeMirrorInterface.include(CodeMirrorWidgets); CodeMirrorInterface.include(CodeMirrorInlineWidgets); function CodeMirrorInterface(element, options) { var placeholder, _base; this.options = options != null ? options : {}; (_base = this.options).readonly || (_base.readonly = false); placeholder = $(element).data("placeholder"); this.codemirror = CodeMirror(element, { lineNumbers: false, lineWrapping: true, mode: "latex", readOnly: this.options.readonly, placeholder: placeholder }); this.bindToEvents(); } CodeMirrorInterface.prototype.detach = function() {}; CodeMirrorInterface.prototype.setValue = function(value) { return this.codemirror.setValue(value); }; CodeMirrorInterface.prototype.bindToEvents = function() { var _this = this; this.codemirror.on("change", function(cm, change) { return _this.onCodeMirrorChange(change); }); this.codemirror.on("beforeSelectionChange", function(cm, selection) { return _this.onBeforeSelectionChange(selection); }); this.codemirror.on("hitSide", function(cm, direction) { return _this.onHitSide(direction); }); this.codemirror.on("focus", function() { return _this.trigger("focus"); }); this.codemirror.on("blur", function() { return _this.trigger("blur"); }); this.codemirror.on("keydown", function() { return _this.trigger("keydown"); }); this.codemirror.on("keyup", function() { return _this.trigger("keyup"); }); this.codemirror.on("copy", function() { return _this.trigger("copy"); }); this.codemirror.on("paste", function(e) { return _this.trigger("paste", e); }); this.codemirror.undo = function() { return setTimeout(function() { return _this.trigger("undo"); }, 0); }; return this.codemirror.redo = function() { return setTimeout(function() { return _this.trigger("redo"); }, 0); }; }; CodeMirrorInterface.prototype.onCodeMirrorChange = function(cmChange) { if (!(cmChange.origin === "setValue")) { this.trigger("change", { inserted: cmChange.text, deleted: cmChange.removed, from: { line: cmChange.from.line, ch: cmChange.from.ch }, to: { line: cmChange.to.line, ch: cmChange.to.ch } }); } if (cmChange.next != null) { return this.onCodeMirrorChange(cmChange.next); } }; CodeMirrorInterface.prototype.onBeforeSelectionChange = function(selection) { this.selection = this._formatSelection(selection); return this.trigger("selection:change", this.selection); }; CodeMirrorInterface.prototype.onHitSide = function(direction) { return this.trigger("selection:hitSide", direction); }; CodeMirrorInterface.prototype.getSelection = function() { return this.selection; }; CodeMirrorInterface.prototype.getSelectedText = function() { return this.codemirror.getSelection(); }; CodeMirrorInterface.prototype.replaceSelection = function(text, collapse) { return this.codemirror.replaceSelection(text, collapse); }; CodeMirrorInterface.prototype.setCursor = function(position) { return this.codemirror.setCursor(position); }; CodeMirrorInterface.prototype.focus = function() { return this.codemirror.focus(); }; CodeMirrorInterface.prototype.getMarkersOnLine = function(line) { var handle, markedSpan, markedSpans, markers, _i, _len; markers = []; handle = this.codemirror.getLineHandle(line); if (handle != null) { markedSpans = handle.markedSpans || []; } else { markedSpans = []; } for (_i = 0, _len = markedSpans.length; _i < _len; _i++) { markedSpan = markedSpans[_i]; markers.push(markedSpan.marker); } return markers; }; CodeMirrorInterface.prototype.getStylesOnLine = function(line) { var from, name, styleInfo, styles, to; styleInfo = this.codemirror.getLineStyles(line); from = styleInfo.shift() - 1; styles = []; while (styleInfo.length > 0) { to = styleInfo.shift(); name = styleInfo.shift(); styles.push({ "class": name, to: to, from: from }); from = to; } return styles; }; CodeMirrorInterface.prototype.getRange = function(from, to) { return this.codemirror.getRange(from, to); }; CodeMirrorInterface.prototype.replaceRange = function(string, from, to) { return this.codemirror.replaceRange(string, from, to); }; CodeMirrorInterface.prototype._formatSelection = function(selection) { var from, to, wrongOrder, _ref; from = { line: selection.head.line, ch: selection.head.ch }; to = { line: selection.anchor.line, ch: selection.anchor.ch }; wrongOrder = false; if (to.line < from.line) { wrongOrder = true; } else if (to.line === from.line && to.ch < from.ch) { wrongOrder = true; } if (wrongOrder) { _ref = [from, to], to = _ref[0], from = _ref[1]; } return { from: from, to: to }; }; CodeMirrorInterface.prototype.insertText = function(line, ch, text) { var pos; debug("INSERTING TEXT", line, ch, text); pos = { line: line, ch: ch }; return this.codemirror.replaceRange(text, pos, pos); }; CodeMirrorInterface.prototype.deleteText = function(line, ch, length) { debug("DELETING TEXT", line, ch, length); return this.codemirror.replaceRange("", { line: line, ch: ch }, { line: line, ch: ch + length }); }; CodeMirrorInterface.prototype.deleteLine = function(lineNo, text) { var isLastLine; debug("DELETING LINE", lineNo, text); isLastLine = lineNo === this.codemirror.lineCount() - 1; if (isLastLine) { return this.codemirror.replaceRange("", { line: lineNo - 1, ch: this.codemirror.getLine(lineNo - 1).length }, { line: lineNo, ch: text.length }); } else { return this.codemirror.replaceRange("", { line: lineNo, ch: 0 }, { line: lineNo + 1, ch: 0 }); } }; CodeMirrorInterface.prototype.insertLine = function(lineNo, text) { var isLastLine; debug("INSERTING LINE", lineNo, text); isLastLine = lineNo === this.codemirror.lineCount(); if (isLastLine) { return this.codemirror.replaceRange("\n" + text, { line: lineNo - 1, ch: this.codemirror.getLine(lineNo - 1).length }, { line: lineNo, ch: 0 }); } else { return this.codemirror.replaceRange(text + "\n", { line: lineNo, ch: 0 }, { line: lineNo, ch: 0 }); } }; CodeMirrorInterface.prototype.refresh = function() { return this.codemirror.refresh(); }; CodeMirrorInterface.prototype.addBookmark = function(position, element) { return this.codemirror.setBookmark(position, element); }; return CodeMirrorInterface; })(Base); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define('orchard/latex-visualisation',["orchard/debug"], function(debug) { var LatexVisualisation, Marker, MathMarker; Marker = (function() { function Marker() {} Marker.prototype.getPosition = function() { var _ref; return (_ref = this.marker) != null ? _ref.find() : void 0; }; Marker.prototype.clear = function() { if (this.marker != null) { this.marker.clear(); return delete this.marker; } }; return Marker; })(); MathMarker = (function(_super) { __extends(MathMarker, _super); function MathMarker(editor, from, to, value) { this.editor = editor; this.value = value; this._createRenderedMarker(from, to, value); } MathMarker.prototype.setValue = function(value) { var oldValue; oldValue = this.value; this.value = value; if (oldValue !== this.value) { return this.renderIfNotSelected({ forceRender: true }); } }; MathMarker.prototype.onCursorEnter = function() { var pos; debug("inline math", "cursor entered", this); pos = this.getPosition(); if (pos == null) { throw new Error("Unknown position"); } return this._createSourceMarker(pos.from, pos.to, this.value); }; MathMarker.prototype.renderIfNotSelected = function(options) { var pos; if (options == null) { options = { forceRender: false }; } if ((this.display !== "rendered" || options.forceRender) && !this._isSelected()) { pos = this.getPosition(); return this._createRenderedMarker(pos.from, pos.to, this.value); } }; MathMarker.prototype._isSelected = function() { var isBeforeOrEqual, pos, selection; selection = this.editor.getSelection(); pos = this.getPosition(); isBeforeOrEqual = function(from, to) { if (from.line < to.line) { return true; } else if (from.line === to.line && from.ch <= to.ch) { return true; } else { return false; } }; if (isBeforeOrEqual(pos.from, selection.from) && isBeforeOrEqual(selection.to, pos.to)) { return true; } else { return false; } }; MathMarker.prototype._createRenderedMarker = function(from, to, value) { var mathJaxEl, _this = this; this.clear(); this.display = "rendered"; mathJaxEl = document.createElement("span"); $(mathJaxEl).text(value); $(mathJaxEl).click(function() { return _this.onCursorEnter(); }); this.marker = this.editor.addVisualMarker(from, to, "math", { replacedWith: mathJaxEl, handler: this }); this.marker.on("beforeCursorEnter", function() { return _this.onCursorEnter(); }); MathJax.Hub.Queue([ "Typeset", MathJax.Hub, mathJaxEl, function() { return _this.marker.changed(); } ]); return this.marker; }; MathMarker.prototype._createSourceMarker = function(from, to, value) { this.clear(); this.display = "source"; this.marker = this.editor.addVisualMarker(from, to, "math", { handler: this }); return this.marker; }; return MathMarker; })(Marker); return LatexVisualisation = (function() { function LatexVisualisation(editor) { var line, _i, _ref, _this = this; this.editor = editor; for (line = _i = 0, _ref = this.editor.getLineCount() - 1; 0 <= _ref ? _i <= _ref : _i >= _ref; line = 0 <= _ref ? ++_i : --_i) { this.processLine(line); } this.editor.on("change", function(change) { return _this.onChange(change); }); this.editor.on("selection:change", function(selection) { return _this.onSelectionChange(selection); }); } LatexVisualisation.prototype.onChange = function(change) { var line, _i, _ref, _ref1, _results; _results = []; for (line = _i = _ref = change.from.line, _ref1 = change.to.line; _ref <= _ref1 ? _i <= _ref1 : _i >= _ref1; line = _ref <= _ref1 ? ++_i : --_i) { _results.push(this.processLine(line)); } return _results; }; LatexVisualisation.prototype.onSelectionChange = function(selection) { if (this._oldSelection != null) { this._renderUnselectedMarkers(this._oldSelection); } this._renderUnselectedMarkers(selection); return this._oldSelection = selection; }; LatexVisualisation.prototype._renderUnselectedMarkers = function(selection) { var line, marker, _i, _ref, _ref1, _results; _results = []; for (line = _i = _ref = selection.from.line, _ref1 = selection.to.line; _ref <= _ref1 ? _i <= _ref1 : _i >= _ref1; line = _ref <= _ref1 ? ++_i : --_i) { _results.push((function() { var _j, _len, _ref2, _results1; _ref2 = this._getMarkersOnLine(line); _results1 = []; for (_j = 0, _len = _ref2.length; _j < _len; _j++) { marker = _ref2[_j]; if (marker.renderIfNotSelected != null) { _results1.push(marker.renderIfNotSelected()); } else { _results1.push(void 0); } } return _results1; }).call(this)); } return _results; }; LatexVisualisation.prototype.processLine = function(lineNo) { var lineText, m, markers, remainingMarker, style, styles, update, updates, _fn, _i, _j, _k, _len, _len1, _len2, _results, _this = this; if (this.editor.codemirror == null) { return; } lineText = this.editor.getLineText(lineNo); if ((m = lineText.match(/(^\$|[^\\]\$)/g))) { if (m.length % 2 !== 0) { return; } } styles = this.editor.codemirror.getStylesOnLine(lineNo); markers = this._getMarkersOnLine(lineNo); updates = []; _fn = function(style) { var hasMarker, i, marker, pos, value, _j, _len1; if (style["class"] !== "math") { return; } value = _this.editor.getRange({ line: lineNo, ch: style.from }, { line: lineNo, ch: style.to }); hasMarker = false; for (i = _j = 0, _len1 = markers.length; _j < _len1; i = ++_j) { marker = markers[i]; pos = marker.getPosition(); if ((pos != null) && pos.from.ch === style.from && pos.to.ch === style.to) { markers.splice(i, 1); hasMarker = true; updates.push(function() { if (marker.setValue != null) { marker.setValue(value); } if (marker.renderIfNotSelected != null) { return marker.renderIfNotSelected(); } }); break; } } if (!hasMarker) { return updates.push(function() { return new MathMarker(_this.editor, { line: lineNo, ch: style.from }, { line: lineNo, ch: style.to }, value); }); } }; for (_i = 0, _len = styles.length; _i < _len; _i++) { style = styles[_i]; _fn(style); } for (_j = 0, _len1 = markers.length; _j < _len1; _j++) { remainingMarker = markers[_j]; remainingMarker.clear(); } _results = []; for (_k = 0, _len2 = updates.length; _k < _len2; _k++) { update = updates[_k]; _results.push(update()); } return _results; }; LatexVisualisation.prototype._getMarkersOnLine = function(line) { var marker, markers, _i, _len, _ref; markers = []; _ref = this.editor.getVisualMarkersOnLine(line); for (_i = 0, _len = _ref.length; _i < _len; _i++) { marker = _ref[_i]; if ((marker.handler != null) && (marker.handler instanceof MathMarker)) { markers.push(marker.handler); } } return markers; }; return LatexVisualisation; })(); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, __slice = [].slice; define('orchard/orchard/widgets',["orchard/base", "orchard/debug"], function(Base, debug) { var OrchardEditorWidgets; return OrchardEditorWidgets = (function(_super) { __extends(OrchardEditorWidgets, _super); OrchardEditorWidgets.WIDGET_TYPES = {}; function OrchardEditorWidgets(editor) { var _this = this; this.editor = editor; this.editor.on("selection:change", function(newSelection, oldSelection) { return _this.onSelectionChange(newSelection, oldSelection); }); this.editor.on("codemirror:annotation:remove", function(annotation) { return _this.onRemoveCodeMirrorAnnotation(annotation); }); this.editor.on("document:annotation:new", function(annotation, lineNo) { return _this.onNewDocumentAnnotation(annotation, lineNo); }); this.editor.on("document:annotation:sync", function(annotation, lineNo) { return _this.onSyncDocumentAnnotation(annotation, lineNo); }); this.editor.on("document:annotation:remove", function(annotation, lineNo) { return _this.onRemoveDocumentAnnotation(annotation, lineNo); }); this.editor.on("document:annotation:update", function(annotation, lineNo) { return _this.onUpdateDocumentAnnotation(annotation, lineNo); }); this.editor.addWidget = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return _this.addWidget.apply(_this, args); }; } OrchardEditorWidgets.prototype.onSelectionChange = function(newSelection, oldSelection) { var enteredWidget; enteredWidget = false; if (oldSelection != null) { enteredWidget = this._checkIfEnteringWidget(newSelection, oldSelection); } if (!enteredWidget) { return this.deselectWidgets(); } }; OrchardEditorWidgets.prototype.onRemoveCodeMirrorAnnotation = function(annotation) { if (annotation.type === "widget") { this.editor.document.removeWidget(annotation.id); return this.clearWidget(annotation.id); } }; OrchardEditorWidgets.prototype.onNewDocumentAnnotation = function(annotation, lineNo) { if (annotation.type === "widget") { return this._initializeWidget(annotation.id, lineNo, annotation["class"], annotation.data); } }; OrchardEditorWidgets.prototype.onSyncDocumentAnnotation = function(annotation, lineNo) { return this.onNewDocumentAnnotation(annotation, lineNo); }; OrchardEditorWidgets.prototype.onRemoveDocumentAnnotation = function(annotation) { var _ref; if (annotation.type === "widget") { return (_ref = this.editor.codemirror) != null ? _ref.removeWidget(annotation.id) : void 0; } }; OrchardEditorWidgets.prototype.onUpdateDocumentAnnotation = function(annotation) { var widget; if (annotation.type === "widget") { widget = this.getWidget(annotation.id); if (widget == null) { return; } return widget.setAttributes(annotation.data); } }; OrchardEditorWidgets.prototype.addWidget = function(lineNo, className, data) { var annotationId; if (this.editor.options.readonly) { return; } annotationId = this.editor.document.addWidget(lineNo, className, data); return this._initializeWidget(annotationId, lineNo, className, data); }; OrchardEditorWidgets.prototype._initializeWidget = function(annotationId, lineNo, className, data) { var widget, widgetClass, _ref, _this = this; debug("editor", "initializing widget", { id: annotationId, "class": className, data: data }); widgetClass = OrchardEditorWidgets.WIDGET_TYPES[className]; if (widgetClass == null) { return; } widget = new widgetClass(this.editor, annotationId, data); widget.on("cursorLeave", function(direction) { var enteredWidget, ingoingDirection, offset, _i, _len, _ref; if (direction === "top") { offset = -1; ingoingDirection = "bottom"; } else { offset = 1; ingoingDirection = "top"; } lineNo = _this.getWidgetLineNo(annotationId) + offset; enteredWidget = false; _ref = _this.getWidgetsOnLine(lineNo); for (_i = 0, _len = _ref.length; _i < _len; _i++) { widget = _ref[_i]; widget.trigger("cursorEnter", ingoingDirection); enteredWidget = true; } if (!enteredWidget) { _this.editor.focus(); return _this.editor.setCursor({ line: lineNo, ch: 0 }); } }); this._handlers || (this._handlers = {}); this._handlers[annotationId] = widget; if ((_ref = this.editor.codemirror) != null) { _ref.addWidget(lineNo, widget.el, annotationId); } widget.trigger("dom:insert"); this.editor.trigger("widget:new", widget); this.editor.refreshDocumentNumbering(); return widget; }; OrchardEditorWidgets.prototype.clearWidget = function(annotationId) { var widget; widget = this._handlers[annotationId]; if (widget == null) { return; } delete this._handlers[annotationId]; return widget.destroy(); }; OrchardEditorWidgets.prototype.markWidgetElementAsChanged = function(annotationId) { var _ref; return (_ref = this.editor.codemirror) != null ? _ref.markWidgetElementAsChanged(annotationId) : void 0; }; OrchardEditorWidgets.prototype.getWidgetDataSubdoc = function(annotationId, path) { return this.editor.document.getWidgetDataSubdoc(annotationId, path); }; OrchardEditorWidgets.prototype.getWidgetLineNo = function(annotationId) { return this.editor.document.getWidgetLineNo(annotationId); }; OrchardEditorWidgets.prototype.getWidgetsOnLine = function(lineNo) { var annotation, annotations, widget, widgets, _i, _len, _ref, _ref1, _ref2; widgets = []; _ref = annotations = this.editor.document.getAnnotationsOnLine(lineNo); for (_i = 0, _len = _ref.length; _i < _len; _i++) { annotation = _ref[_i]; debug("widget", annotation, (_ref1 = this._handlers) != null ? _ref1[annotation.id] : void 0); if (widget = (_ref2 = this._handlers) != null ? _ref2[annotation.id] : void 0) { widgets.push(widget); } } return widgets; }; OrchardEditorWidgets.prototype.getWidget = function(annotationId) { return this._handlers[annotationId]; }; OrchardEditorWidgets.prototype.getWidgets = function() { var annotationId, handler; return (function() { var _ref, _results; _ref = this._handlers; _results = []; for (annotationId in _ref) { handler = _ref[annotationId]; _results.push(handler); } return _results; }).call(this); }; OrchardEditorWidgets.prototype.getWidgetsOrderedByLine = function() { var annotationId, handler, handlers; handlers = (function() { var _ref, _results; _ref = this._handlers; _results = []; for (annotationId in _ref) { handler = _ref[annotationId]; _results.push(handler); } return _results; }).call(this); return handlers.sort(function(a, b) { a = a.getLineNumber(); b = b.getLineNumber(); if (a > b) { return 1; } else if (a < b) { return -1; } else { return 0; } }); }; OrchardEditorWidgets.prototype._checkIfEnteringWidget = function(newSelection, oldSelection) { var direction, enteredFirstLine, enteredWidget, haveSkippedLineAbove, haveSkippedLineBelow, nextLine, prevLine, widget, widgets, _i, _len; if (!this.editor.keydown) { return; } haveSkippedLineBelow = newSelection.from.line - oldSelection.to.line > 1; haveSkippedLineAbove = oldSelection.from.line - newSelection.to.line > 1; enteredFirstLine = newSelection.from.line === 0; if (haveSkippedLineBelow) { nextLine = oldSelection.to.line + 1; widgets = this.getWidgetsOnLine(nextLine); direction = "top"; } else if (haveSkippedLineAbove) { prevLine = oldSelection.from.line - 1; widgets = this.getWidgetsOnLine(prevLine); direction = "bottom"; } else if (enteredFirstLine) { widgets = this.getWidgetsOnLine(0); direction = "bottom"; } else { widgets = []; } enteredWidget = false; for (_i = 0, _len = widgets.length; _i < _len; _i++) { widget = widgets[_i]; widget.trigger("cursorEnter", direction); enteredWidget = true; } return enteredWidget; }; OrchardEditorWidgets.prototype.deselectWidgets = function() { var annotationId, widget, _ref, _results; _ref = this._handlers; _results = []; for (annotationId in _ref) { widget = _ref[annotationId]; _results.push(widget.deselect()); } return _results; }; return OrchardEditorWidgets; })(Base); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, __slice = [].slice; define('orchard/orchard/line-annotations',["orchard/base", "orchard/event", "orchard/debug"], function(Base, Events, debug) { var ChapterLineAnnotation, HeaderLineAnnotation, LineAnnotation, OrchardEditorLineAnnotations, SectionLineAnnotation, SubsectionLineAnnotation, _ref, _ref1, _ref2; LineAnnotation = (function(_super) { __extends(LineAnnotation, _super); LineAnnotation.include(Events); function LineAnnotation(editor, annotationId, lineHandle) { this.editor = editor; this.annotationId = annotationId; this.lineHandle = lineHandle; } LineAnnotation.prototype.destroy = function() {}; LineAnnotation.prototype.getLineNumber = function() { var _ref; return (_ref = this.editor.codemirror) != null ? _ref.getLineNumber(this.lineHandle) : void 0; }; return LineAnnotation; })(Base); HeaderLineAnnotation = (function(_super) { __extends(HeaderLineAnnotation, _super); function HeaderLineAnnotation(editor, annotationId, lineHandle) { var _ref, _this = this; this.editor = editor; this.annotationId = annotationId; this.lineHandle = lineHandle; HeaderLineAnnotation.__super__.constructor.call(this, this.editor, this.annotationId, this.lineHandle); if (typeof document !== "undefined" && document !== null) { this.numberEl = document.createElement("span"); this.numberEl.innerText = "?. "; this.numberEl.className = "oe-non-editable"; this.numberEl.onclick = function() { return _this.onNumberClick(); }; } this.bookmark = (_ref = this.editor.codemirror) != null ? _ref.addBookmark({ line: this.getLineNumber(), ch: 0 }, this.numberEl) : void 0; this.on("codemirror:sync", function() { return _this.refresh(); }); } HeaderLineAnnotation.prototype.refresh = function() { var _ref, _ref1; if ((_ref = this.bookmark) != null) { _ref.clear(); } return this.bookmark = (_ref1 = this.editor.codemirror) != null ? _ref1.addBookmark({ line: this.getLineNumber(), ch: 0 }, this.numberEl) : void 0; }; HeaderLineAnnotation.prototype.onNumberClick = function() { this.editor.setCursor({ line: this.getLineNumber(), ch: 0 }); return this.editor.focus(); }; HeaderLineAnnotation.prototype.destroy = function() { var _ref, _ref1; HeaderLineAnnotation.__super__.destroy.apply(this, arguments); if ((_ref = this.bookmark) != null) { _ref.clear(); } return (_ref1 = this.numberEl) != null ? _ref1.remove() : void 0; }; HeaderLineAnnotation.prototype.convertToTex = function(text) { return "\\" + this["class"] + "{" + text + "}"; }; HeaderLineAnnotation.prototype.updateDocumentNumbering = function(currentNumbering) { this.currentNumbering = currentNumbering; }; return HeaderLineAnnotation; })(LineAnnotation); ChapterLineAnnotation = (function(_super) { __extends(ChapterLineAnnotation, _super); function ChapterLineAnnotation() { _ref = ChapterLineAnnotation.__super__.constructor.apply(this, arguments); return _ref; } ChapterLineAnnotation.prototype["class"] = "chapter"; ChapterLineAnnotation.prototype.updateDocumentNumbering = function(currentNumbering) { ChapterLineAnnotation.__super__.updateDocumentNumbering.apply(this, arguments); currentNumbering.chapter++; currentNumbering.section = 0; currentNumbering.subsection = 0; currentNumbering.equation = 0; currentNumbering.figure = 0; this.numericLabel = "" + currentNumbering.chapter; this.label = "Chapter " + this.numericLabel; this.numberEl.innerText = "" + currentNumbering.chapter + ". "; return currentNumbering; }; return ChapterLineAnnotation; })(HeaderLineAnnotation); SectionLineAnnotation = (function(_super) { __extends(SectionLineAnnotation, _super); function SectionLineAnnotation() { _ref1 = SectionLineAnnotation.__super__.constructor.apply(this, arguments); return _ref1; } SectionLineAnnotation.prototype["class"] = "section"; SectionLineAnnotation.prototype.updateDocumentNumbering = function(currentNumbering) { SectionLineAnnotation.__super__.updateDocumentNumbering.apply(this, arguments); currentNumbering.section++; currentNumbering.subsection = 0; this.numericLabel = "" + currentNumbering.chapter + "." + currentNumbering.section; this.label = "Section " + this.numericLabel; this.numberEl.innerText = "" + currentNumbering.chapter + "." + currentNumbering.section + ". "; return currentNumbering; }; return SectionLineAnnotation; })(HeaderLineAnnotation); SubsectionLineAnnotation = (function(_super) { __extends(SubsectionLineAnnotation, _super); function SubsectionLineAnnotation() { _ref2 = SubsectionLineAnnotation.__super__.constructor.apply(this, arguments); return _ref2; } SubsectionLineAnnotation.prototype["class"] = "subsection"; SubsectionLineAnnotation.prototype.updateDocumentNumbering = function(currentNumbering) { SubsectionLineAnnotation.__super__.updateDocumentNumbering.apply(this, arguments); currentNumbering.subsection++; this.numericLabel = "" + currentNumbering.chapter + "." + currentNumbering.section + "." + currentNumbering.subsection; this.label = "Subsection " + this.numericLabel; this.numberEl.innerText = "" + currentNumbering.chapter + "." + currentNumbering.section + "." + currentNumbering.subsection + ". "; return currentNumbering; }; return SubsectionLineAnnotation; })(HeaderLineAnnotation); return OrchardEditorLineAnnotations = (function(_super) { __extends(OrchardEditorLineAnnotations, _super); function OrchardEditorLineAnnotations(editor) { var _this = this; this.editor = editor; this._handlers = {}; this.editor.on("document:annotation:new", function(annotation, lineNo) { return _this._onNewDocumentAnnotation(annotation, lineNo); }); this.editor.on("document:annotation:sync", function(annotation, lineNo) { return _this._onSyncDocumentAnnotation(annotation, lineNo); }); this.editor.on("document:annotation:remove", function(annotation, lineNo) { return _this._onRemoveDocumentAnnotation(annotation, lineNo); }); this.editor.on("document:lines:change", function(lineNo) { return _this._onDocumentLinesChange(lineNo); }); this.editor.on("codemirror:line:annotations:change", function(lineNo) { return _this._onCodeMirrorLineAnnotationsChange(lineNo); }); this.editor.on("codemirror:lines:change", function(lineNo) { return _this._onCodeMirrorLinesChange(lineNo); }); this.editor.on("selection:change", function(newSelection, oldSelection) { return _this._onSelectionChange(newSelection, oldSelection); }); this.editor.addLineAnnotation = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return _this.addLineAnnotation.apply(_this, args); }; this.editor.removeLineAnnotation = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return _this.removeLineAnnotation.apply(_this, args); }; this.editor.getLineAnnotationsOnLine = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return _this.getLineAnnotationsOnLine.apply(_this, args); }; } OrchardEditorLineAnnotations.prototype.addLineAnnotation = function(line, className) { var annotationId, _ref3; if (this.editor.options.readonly) { return; } annotationId = this.editor.document.addLineAnnotation(line, className); this._initializeLineAnnotationHandler(annotationId, line, className); return (_ref3 = this.editor.codemirror) != null ? _ref3.addLineAnnotation(line, className, annotationId) : void 0; }; OrchardEditorLineAnnotations.prototype.removeLineAnnotation = function(annotationId) { var _ref3; if (this.editor.options.readonly) { return; } this.editor.document.removeLineAnnotation(annotationId); this._destroyLineAnnotationHandler(annotationId); return (_ref3 = this.editor.codemirror) != null ? _ref3.removeLineAnnotation(annotationId) : void 0; }; OrchardEditorLineAnnotations.prototype.getAllLineAnnotations = function() { var annotationId, handler, _ref3, _results; _ref3 = this._handlers; _results = []; for (annotationId in _ref3) { handler = _ref3[annotationId]; _results.push(handler); } return _results; }; OrchardEditorLineAnnotations.prototype.getLineAnnotationsOnLine = function(line) { var annotation, annotations, _i, _len, _ref3; annotations = []; _ref3 = this.editor.document.getLineAnnotationsOnLine(line); for (_i = 0, _len = _ref3.length; _i < _len; _i++) { annotation = _ref3[_i]; if (this._handlers[annotation.id] != null) { annotations.push(this._handlers[annotation.id]); } } return annotations; }; OrchardEditorLineAnnotations.prototype.getLineAnnotationsOrderedByLine = function() { var annotationId, handler, handlers; handlers = (function() { var _ref3, _results; _ref3 = this._handlers; _results = []; for (annotationId in _ref3) { handler = _ref3[annotationId]; _results.push(handler); } return _results; }).call(this); return handlers.sort(function(a, b) { a = a.getLineNumber(); b = b.getLineNumber(); if (a > b) { return 1; } else if (a < b) { return -1; } else { return 0; } }); }; OrchardEditorLineAnnotations.prototype._onNewDocumentAnnotation = function(annotation, lineNo) { var _ref3; if (annotation.type === "line") { this._initializeLineAnnotationHandler(annotation.id, lineNo, annotation["class"]); return (_ref3 = this.editor.codemirror) != null ? _ref3.addLineAnnotation(lineNo, annotation["class"], annotation.id) : void 0; } }; OrchardEditorLineAnnotations.prototype._onSyncDocumentAnnotation = function(annotation, lineNo) { return this._onNewDocumentAnnotation(annotation, lineNo); }; OrchardEditorLineAnnotations.prototype._onRemoveDocumentAnnotation = function(annotation) { var _ref3; if (annotation.type === "line") { this._destroyLineAnnotationHandler(annotation.id); return (_ref3 = this.editor.codemirror) != null ? _ref3.removeLineAnnotation(annotation.id) : void 0; } }; OrchardEditorLineAnnotations.prototype._onCodeMirrorLineAnnotationsChange = function(lineNo) { var annotation, documentAnnotationIds, documentAnnotations, editorAnnotation, editorAnnotations, handler, i, remainingAnnotationId, _i, _j, _len, _len1, _results; editorAnnotations = this.editor.codemirror.getLineAnnotationsOnLine(lineNo); documentAnnotations = this.editor.document.getLineAnnotationsOnLine(lineNo); documentAnnotationIds = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = documentAnnotations.length; _i < _len; _i++) { annotation = documentAnnotations[_i]; _results.push(annotation.id); } return _results; })(); debug("editor", "syncing codemirror annotations to doc", editorAnnotations, documentAnnotations, lineNo); for (_i = 0, _len = editorAnnotations.length; _i < _len; _i++) { editorAnnotation = editorAnnotations[_i]; if ((i = documentAnnotationIds.indexOf(editorAnnotation.id)) > -1) { documentAnnotationIds.splice(i, 1); } else { this.editor.document.addLineAnnotation(lineNo, editorAnnotation["class"], editorAnnotation.id); } handler = this._getLineAnnotationHandler(editorAnnotation.id); if (handler != null) { handler.trigger("codemirror:sync", lineNo); } } debug("editor", "removing remaining document ids", documentAnnotationIds); _results = []; for (_j = 0, _len1 = documentAnnotationIds.length; _j < _len1; _j++) { remainingAnnotationId = documentAnnotationIds[_j]; _results.push(this.editor.document.removeLineAnnotation(remainingAnnotationId)); } return _results; }; OrchardEditorLineAnnotations.prototype._onCodeMirrorLinesChange = function() { var annotationId, handler, _ref3, _results; debug("WARNING", "FULL LINE HANDLER SCAN"); _ref3 = this._handlers; _results = []; for (annotationId in _ref3) { handler = _ref3[annotationId]; if (handler.getLineNumber() == null) { _results.push(this._destroyLineAnnotationHandler(annotationId)); } else { _results.push(void 0); } } return _results; }; OrchardEditorLineAnnotations.prototype._onDocumentLinesChange = function() { var annotationId, handler, _ref3, _results; debug("WARNING", "FULL LINE HANDLER SCAN"); _ref3 = this._handlers; _results = []; for (annotationId in _ref3) { handler = _ref3[annotationId]; _results.push(handler.refresh()); } return _results; }; OrchardEditorLineAnnotations.prototype._onSelectionChange = function(newSelection, oldSelection) { var headingType, lineNo, m, match, text, value; if ((oldSelection != null) && newSelection.from.line !== oldSelection.from.line) { lineNo = oldSelection.from.line; text = this.editor.getLineText(lineNo); if ((m = text.match(/^\\(chapter|section|subsection)\{(.*?)\}$/))) { match = m[0]; headingType = m[1]; value = m[2]; this.editor.replaceRange(value, { line: lineNo, ch: 0 }, { line: lineNo, ch: match.length }); return this.addLineAnnotation(lineNo, headingType); } } }; OrchardEditorLineAnnotations.prototype._initializeLineAnnotationHandler = function(annotationId, lineNo, className) { var HandlerClass, lineHandle, _ref3; lineHandle = (_ref3 = this.editor.codemirror) != null ? _ref3.getLineHandle(lineNo) : void 0; HandlerClass = { "chapter": ChapterLineAnnotation, "section": SectionLineAnnotation, "subsection": SubsectionLineAnnotation }[className]; if (HandlerClass == null) { throw new Error("Unknown line annotation class: " + className); } this._handlers[annotationId] = new HandlerClass(this.editor, annotationId, lineHandle); return this.editor.refreshDocumentNumbering(); }; OrchardEditorLineAnnotations.prototype._destroyLineAnnotationHandler = function(annotationId) { var _ref3; debug("codemirror", "destroying line handle", annotationId); if ((_ref3 = this._handlers[annotationId]) != null) { _ref3.destroy(); } delete this._handlers[annotationId]; return this.editor.refreshDocumentNumbering(); }; OrchardEditorLineAnnotations.prototype._getLineAnnotationHandler = function(annotationId) { return this._handlers[annotationId]; }; return OrchardEditorLineAnnotations; })(Base); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, __slice = [].slice; define('orchard/orchard/inline-widgets',["orchard/base", "orchard/debug"], function(Base, debug) { var OrchardEditorInlineWidgets; return OrchardEditorInlineWidgets = (function(_super) { __extends(OrchardEditorInlineWidgets, _super); OrchardEditorInlineWidgets.INLINE_WIDGET_TYPES = {}; function OrchardEditorInlineWidgets(editor) { var _this = this; this.editor = editor; this.editor.on("document:annotation:new", function(annotation, lineNo) { return _this._onNewDocumentAnnotation(annotation, lineNo); }); this.editor.on("document:annotation:sync", function(annotation, lineNo) { return _this._onSyncDocumentAnnotation(annotation, lineNo); }); this.editor.on("document:annotation:remove", function(annotation, lineNo) { return _this._onRemoveDocumentAnnotation(annotation, lineNo); }); this.editor.on("document:annotation:update", function(annotation, lineNo) { return _this._onUpdateDocumentAnnotation(annotation, lineNo); }); this.editor.on("codemirror:line:annotations:change", function(lineNo) { return _this._onCodeMirrorInlineWidgetsChange(lineNo); }); this.editor.addInlineWidget = function() { var args; args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; return _this.addInlineWidget.apply(_this, args); }; } OrchardEditorInlineWidgets.prototype.addInlineWidget = function(lineNo, at, className, data) { var annotationId, from, to; if (this.editor.options.readonly) { return; } from = at; to = at + 1; this.editor.startOperation(); this.editor.insertText(lineNo, at, "?"); annotationId = this.editor.document.addInlineWidget(lineNo, from, to, className, data); this._initializeInlineWidget(annotationId, lineNo, from, to, className, data); return this.editor.endOperation(); }; OrchardEditorInlineWidgets.prototype.markInlineWidgetElementAsChanged = function(annotationId) { var _ref; return (_ref = this.editor.codemirror) != null ? _ref.markInlineWidgetElementAsChanged(annotationId) : void 0; }; OrchardEditorInlineWidgets.prototype.getInlineWidgetPosition = function(annotationId) { return this.editor.document.getInlineWidgetPosition(annotationId); }; OrchardEditorInlineWidgets.prototype.getInlineWidgetsOnLine = function(lineNo) { var annotation, widget, widgets, _i, _len, _ref, _ref1; widgets = []; _ref = this.editor.document.getInlineWidgetsOnLine(lineNo); for (_i = 0, _len = _ref.length; _i < _len; _i++) { annotation = _ref[_i]; if (widget = (_ref1 = this._handlers) != null ? _ref1[annotation.id] : void 0) { widgets.push(widget); } } return widgets; }; OrchardEditorInlineWidgets.prototype.getInlineWidget = function(annotationId) { return this._handlers[annotationId]; }; OrchardEditorInlineWidgets.prototype._initializeInlineWidget = function(annotationId, lineNo, from, to, className, data) { var WidgetClass, widget, _ref; debug("editor", "initializing inline widget", { id: annotationId, "class": className, data: data }); WidgetClass = OrchardEditorInlineWidgets.INLINE_WIDGET_TYPES[className]; widget = new WidgetClass(this.editor, annotationId, data); this._handlers || (this._handlers = {}); this._handlers[annotationId] = widget; if ((_ref = this.editor.codemirror) != null) { _ref.addInlineWidget(lineNo, from, to, widget.el, annotationId); } widget.trigger("dom:insert"); this.editor.trigger("inlineWidget:new", widget); return widget; }; OrchardEditorInlineWidgets.prototype._destroyInlineWidgetHandler = function(annotationId) { var _ref; if ((_ref = this._handlers[annotationId]) != null) { if (typeof _ref.destroy === "function") { _ref.destroy(); } } return delete this._handlers[annotationId]; }; OrchardEditorInlineWidgets.prototype._onNewDocumentAnnotation = function(annotation, lineNo) { if (annotation.type === "inline-widget") { return this._initializeInlineWidget(annotation.id, lineNo, annotation.from, annotation.to, annotation["class"], annotation.data); } }; OrchardEditorInlineWidgets.prototype._onSyncDocumentAnnotation = function(annotation, lineNo) { return this._onNewDocumentAnnotation(annotation, lineNo); }; OrchardEditorInlineWidgets.prototype._onRemoveDocumentAnnotation = function(annotation, lineNo) { var _ref; if (annotation.type === "inline-widget") { this._destroyInlineWidgetHandler(annotation.id); return (_ref = this.editor.codemirror) != null ? _ref.removeInlineWidget(annotation.id) : void 0; } }; OrchardEditorInlineWidgets.prototype._onUpdateDocumentAnnotation = function(annotation) { var widget; if (annotation.type === "inline-widget") { widget = this.getInlineWidget(annotation.id); if (widget == null) { return; } return widget.setAttributes(annotation.data); } }; OrchardEditorInlineWidgets.prototype._onCodeMirrorInlineWidgetsChange = function(lineNo) { var annotation, documentWidgetIds, documentWidgets, editorWidget, editorWidgets, i, remainingWidgetId, _i, _j, _len, _len1, _results; editorWidgets = this.editor.codemirror.getInlineWidgetsOnLine(lineNo); documentWidgets = this.editor.document.getInlineWidgetsOnLine(lineNo); documentWidgetIds = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = documentWidgets.length; _i < _len; _i++) { annotation = documentWidgets[_i]; _results.push(annotation.id); } return _results; })(); debug("editor", "syncing codemirror inline widgets to doc", editorWidgets, documentWidgets, lineNo); for (_i = 0, _len = editorWidgets.length; _i < _len; _i++) { editorWidget = editorWidgets[_i]; if ((i = documentWidgetIds.indexOf(editorWidget.id)) > -1) { this.editor.document.moveInlineWidget(documentWidgetIds[i], editorWidget.from.ch, editorWidget.to.ch); documentWidgetIds.splice(i, 1); } } debug("editor", "removing remaining document ids", documentWidgetIds); _results = []; for (_j = 0, _len1 = documentWidgetIds.length; _j < _len1; _j++) { remainingWidgetId = documentWidgetIds[_j]; this._destroyInlineWidgetHandler(remainingWidgetId); _results.push(this.editor.document.removeInlineWidget(remainingWidgetId)); } return _results; }; return OrchardEditorInlineWidgets; })(Base); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define('orchard/undo',["orchard/base", "orchard/debug"], function(Base, debug) { var OrchardUndoManager; return OrchardUndoManager = (function(_super) { __extends(OrchardUndoManager, _super); function OrchardUndoManager(editor) { this.editor = editor; this._bindToEditorEvents(); this.undoStack = []; this.redoStack = []; } OrchardUndoManager.prototype._bindToEditorEvents = function() { var _this = this; this.editor.on("undo", function() { return _this.undo(); }); this.editor.on("redo", function() { return _this.redo(); }); this.editor.document.on("op:local", function(op) { return _this._onChange(op); }); return this.editor.document.on("op:remote", function(op) { return _this._onChange(op); }); }; OrchardUndoManager.prototype.undo = function() { var op, _this = this; op = this.undoStack.pop(); if (op == null) { return; } this.redoStack.push(op); this.ignoreNextOp = true; return this.editor.document.triggeringEvents(function() { return _this.editor.revertRawOp(op); }); }; OrchardUndoManager.prototype.redo = function() { var op, _this = this; op = this.redoStack.pop(); if (op == null) { return; } this.undoStack.push(op); this.ignoreNextOp = true; return this.editor.document.triggeringEvents(function() { return _this.editor.applyRawOp(op); }); }; OrchardUndoManager.prototype._onChange = function(op) { if (this.ignoreNextOp) { return delete this.ignoreNextOp; } else { this.undoStack.push(op); return this.redoStack = []; } }; return OrchardUndoManager; })(Base); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define('orchard/copyandpaste',["orchard/base", "orchard/debug"], function(Base, debug) { var OrchardCopyAndPasteManager; return OrchardCopyAndPasteManager = (function(_super) { __extends(OrchardCopyAndPasteManager, _super); function OrchardCopyAndPasteManager(editor) { this.editor = editor; this._bindToEvents(); } OrchardCopyAndPasteManager.prototype._bindToEvents = function() {}; OrchardCopyAndPasteManager.prototype.copy = function() { var content, selection; selection = this.editor.getSelection(); if (selection != null) { content = this.editor.document.getRawRange(selection.from, selection.to); return $.localStorage("orchard.clipboard", JSON.stringify(content)); } }; OrchardCopyAndPasteManager.prototype.paste = function(e) { var content, selection, _this = this; content = $.localStorage("orchard.clipboard"); if (content == null) { return; } e.preventDefault(); content = JSON.parse(content); selection = this.editor.getSelection(); if (selection == null) { return; } return this.editor.document.triggeringEvents(function() { _this.editor.document.recreateAnnotationIds(content); return _this.editor.document.insertRawRange(selection.from, content); }); }; return OrchardCopyAndPasteManager; })(Base); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define('orchard/widgets/widget',["orchard/base", "orchard/event", "orchard/debug"], function(Base, Events, debug) { var Widget; return Widget = (function(_super) { __extends(Widget, _super); Widget.include(Events); function Widget(editor, annotationId, data) { var _this = this; this.editor = editor; this.annotationId = annotationId; this.data = data; if (typeof document !== "undefined" && document !== null) { this.el = document.createElement("div"); this.el.setAttribute("class", "oe-widget"); this.el.innerHTML = this.template; this.el.onclick = function() { var _ref; if ((_ref = _this.editor.widgets) != null) { _ref.deselectWidgets(); } return _this.select(); }; this._bindToSelectionEvents(); } this._setDefaults(); this._createDataBindings(); } Widget.prototype.destroy = function() { var _ref; this.off(); return (_ref = this.el) != null ? _ref.remove() : void 0; }; Widget.prototype.getLineNumber = function() { return this.editor.widgets.getWidgetLineNo(this.annotationId); }; Widget.prototype.setAttributes = function(attributes) { var key, oldAttributes, value, _ref, _results; oldAttributes = this.data; this.data = attributes; _ref = this.data; for (key in _ref) { value = _ref[key]; if (oldAttributes[key] !== this.data[key]) { this.trigger("" + key + ":update", this.data[key], oldAttributes[key]); } delete oldAttributes[key]; } _results = []; for (key in oldAttributes) { value = oldAttributes[key]; _results.push(this.trigger("" + key + ":update", null, oldAttributes[key])); } return _results; }; Widget.prototype.getAttribute = function(attribute) { return this.data[attribute]; }; Widget.prototype.setAttribute = function(attribute, value) { var oldValue; oldValue = this.data[attribute]; this.data[attribute] = value; this.trigger("" + attribute + ":update", value, oldValue); return this.editor.document.setWidgetAttribute(this.annotationId, attribute, value); }; Widget.prototype.select = function() { var _ref; if (this.selected) { return; } if ((_ref = this.editor.widgets) != null) { _ref.deselectWidgets(); } this.trigger("selected"); this.selected = true; return this.markAsChanged(); }; Widget.prototype.deselect = function() { if (!this.selected) { return; } if (this.selected) { this.trigger("deselected"); } return this.selected = false; }; Widget.prototype.markAsChanged = function() { return this.editor.widgets.markWidgetElementAsChanged(this.annotationId); }; Widget.prototype._setDefaults = function() { var attribute, currentValue, value, _ref, _results; _ref = this.defaults || {}; _results = []; for (attribute in _ref) { value = _ref[attribute]; currentValue = this.getAttribute(attribute); if (currentValue == null) { _results.push(this.setAttribute(attribute, value)); } else { _results.push(void 0); } } return _results; }; Widget.prototype._createDataBindings = function() { var $boundEl, $el, attribute, boundEl, match, _, _i, _j, _len, _len1, _ref, _ref1, _ref2, _results; if (this.el != null) { $el = $(this.el); _ref = $el.find("[data-oe-binding]"); for (_i = 0, _len = _ref.length; _i < _len; _i++) { boundEl = _ref[_i]; $boundEl = $(boundEl); attribute = $boundEl.data("oe-binding"); if ($boundEl.prop("tagName") === "SELECT") { this._bindToSelectEl($boundEl, attribute); } else if ($boundEl.data("oe-richtext") != null) { this._createEditorEl($boundEl[0], attribute); } } return this.markAsChanged(); } else { _ref1 = this.template.match(/data-oe-binding=('|")(.*?)('|") data-oe-richtext/g); _results = []; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { match = _ref1[_j]; _ref2 = match.match(/data-oe-binding=('|")(.*?)('|")/), match = _ref2[0], _ = _ref2[1], attribute = _ref2[2]; _results.push(this._createEditorEl(null, attribute)); } return _results; } }; Widget.prototype._bindToSelectEl = function($el, attribute) { var updateDom, _this = this; if (this.editor.options.readonly) { $el.attr("disabled", "disabled"); } $el.selectpicker({ showContent: true }); $el.on("change", function() { var value; value = $el.val(); return _this.setAttribute(attribute, value); }); this.on("" + attribute + ":update", updateDom = function() { $el.val(_this.getAttribute(attribute)); return $el.selectpicker("refresh"); }); return updateDom(); }; Widget.prototype._createEditorEl = function(el, attribute) { var OrchardEditor, editor, _this = this; OrchardEditor = require("orchard/orchard"); this["" + attribute + "Editor"] = editor = new OrchardEditor(el, this.editor.widgets.getWidgetDataSubdoc(this.annotationId, [attribute]), { simple: true }, this.editor); this.editors || (this.editors = []); this.editors.push(editor); setTimeout(function() { editor.refresh(); return _this.markAsChanged(); }, 0); editor.on("selection:hitSide", function(direction) { return _this._moveCursorToNextEditor(editor, direction); }); return editor.on("change", function() { return _this.markAsChanged(); }); }; Widget.prototype._bindToSelectionEvents = function() { var _this = this; $(this.displayEl).on("click", function() { return _this.trigger("cursorEnter"); }); this.on("cursorEnter", function(direction) { _this._moveCursorToFirstEditor(direction); return _this.select(); }); return this.on("cursorLeave", function(direction) { _this._moveCursorToFirstEditor(direction); return _this.deselect(); }); }; Widget.prototype._moveCursorToNextEditor = function(editor, direction) { var index; index = this.editors.indexOf(editor); if (index < 0) { return this.trigger("cursorLeave", direction); } else if (direction === "top") { if (index === 0) { return this.trigger("cursorLeave", direction); } else { return this.editors[index - 1].focus(); } } else if (direction === "bottom") { if (index === this.editors.length - 1) { return this.trigger("cursorLeave", direction); } else { return this.editors[index + 1].focus(); } } }; Widget.prototype._moveCursorToFirstEditor = function(direction) { var _ref, _ref1; this.editors || (this.editors = []); if (direction === "top") { return (_ref = this.editors[0]) != null ? _ref.focus() : void 0; } else if (direction === "bottom") { return (_ref1 = this.editors[this.editors.length - 1]) != null ? _ref1.focus() : void 0; } }; return Widget; })(Base); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define('orchard/widgets/figure',["orchard/orchard/widgets", "orchard/widgets/widget", "orchard/debug"], function(OrchardEditorWidgets, Widget, debug) { var FigureWidget; FigureWidget = (function(_super) { var w; __extends(FigureWidget, _super); FigureWidget.prototype.defaults = { width: "60", caption: { lines: [ { text: "Caption", annotations: [] } ] } }; FigureWidget.prototype.template = "
\n
\n \n
\n
\n
\n
\n
\n
\n \n
\n
"; function FigureWidget(editor, annotationId, data) { var _ref, _ref1, _ref2, _ref3, _ref4, _this = this; this.editor = editor; this.annotationId = annotationId; this.data = data; FigureWidget.__super__.constructor.call(this, this.editor, this.annotationId, this.data); this.figureEl = (_ref = this.el) != null ? _ref.getElementsByClassName("oe-figure")[0] : void 0; this.imgEl = (_ref1 = this.el) != null ? _ref1.getElementsByClassName("oe-figure-image")[0] : void 0; this.imgWrapperEl = (_ref2 = this.el) != null ? _ref2.getElementsByClassName("oe-figure-image-wrapper")[0] : void 0; this.placeholderEl = (_ref3 = this.el) != null ? _ref3.getElementsByClassName("oe-figure-image-placeholder")[0] : void 0; this.captionEl = (_ref4 = this.el) != null ? _ref4.getElementsByClassName("oe-figure-caption-text")[0] : void 0; this.on("imageId:update", function() { return _this.loadImage(); }); this.on("image:load", function() { return _this.refreshImageSize(); }); this.on("image:clear", function() { return _this.refreshImageSize(); }); this.on("width:update", function() { return _this.refreshImageSize(); }); this.createFigureLabel(); this.on("dom:insert", function() { return _this.loadImage(); }); } FigureWidget.prototype.destroy = function() { FigureWidget.__super__.destroy.apply(this, arguments); return this.editor.refreshDocumentNumbering(); }; FigureWidget.prototype.updateDocumentNumbering = function(currentNumbering) { this.currentNumbering = currentNumbering; this.currentNumbering.figure++; this.numericLabel = "" + this.currentNumbering.chapter + "." + this.currentNumbering.figure; this.label = "Figure " + this.numericLabel; this.refreshFigureLabel(); return this.currentNumbering; }; FigureWidget.prototype.refreshImageSize = function() { var imageWidth, width; width = parseInt(this.getAttribute("width"), 10) / 100; if ((this.el != null) && (this.aspectRatio != null)) { imageWidth = this.figureWidth() * width; if (this._resizableInited) { this._destroyResizable(); } $(this.imgWrapperEl).width(imageWidth); $(this.imgWrapperEl).height(imageWidth / this.aspectRatio); this._initResizable(); } return this.markAsChanged(); }; FigureWidget.prototype.createFigureLabel = function() { var _ref, _this = this; if (typeof document !== "undefined" && document !== null) { this.figureLabel = document.createElement("span"); this.figureLabel.className = "oe-figure-number oe-non-editable"; this.refreshFigureLabel(); this._labelBookmark = (_ref = this.captionEditor.codemirror) != null ? _ref.addBookmark({ line: 0, ch: 0 }, this.figureLabel) : void 0; return this.captionEditor.on("change", function() { var _ref1, _ref2; if ((_ref1 = _this._labelBookmark) != null) { _ref1.clear(); } return _this._labelBookmark = (_ref2 = _this.captionEditor.codemirror) != null ? _ref2.addBookmark({ line: 0, ch: 0 }, _this.figureLabel) : void 0; }); } }; FigureWidget.prototype.refreshFigureLabel = function() { var _ref; return (_ref = this.figureLabel) != null ? _ref.innerHTML = this.label + " " : void 0; }; FigureWidget.prototype.loadImage = function() { var OrchardEditor, imageId, _ref, _ref1, _ref2, _this = this; imageId = this.getAttribute("imageId"); debug("figure", "loading image", imageId); if (imageId != null) { OrchardEditor = require("orchard/orchard"); return OrchardEditor.convertImageIdToUrl(imageId, function(error, url) { var _ref, _ref1, _ref2, _ref3; if ((_ref = _this.imgEl) != null) { _ref.onload = function() { debug("figure", "image loaded", url); _this.aspectRatio = _this.imgEl.width / _this.imgEl.height; return _this.trigger("image:load"); }; } if ((_ref1 = _this.imgEl) != null) { _ref1.setAttribute("src", url); } if ((_ref2 = _this.imgEl) != null) { _ref2.style.display = ""; } return (_ref3 = _this.placeholderEl) != null ? _ref3.style.display = "none" : void 0; }); } else { debug("figure", "no image found"); if ((_ref = this.imgEl) != null) { _ref.setAttribute("src", ""); } if ((_ref1 = this.imgEl) != null) { _ref1.style.display = "none"; } if ((_ref2 = this.placeholderEl) != null) { _ref2.style.display = ""; } this.aspectRatio = 1.6; return this.trigger("image:clear"); } }; FigureWidget.prototype.convertToTex = function(options, callback) { var width, _this = this; if (options == null) { options = {}; } if (callback == null) { callback = function(error, tex) {}; } options.convertImageIdToFilePath || (options.convertImageIdToFilePath = function(imageId, callback) { if (callback == null) { callback = function(error, path) {}; } return callback(null, "images/" + imageId + ".png"); }); width = parseInt(this.getAttribute("width") || "60", 10) / 100; return options.convertImageIdToFilePath(this.getAttribute("imageId"), function(error, path) { if (error != null) { return callback(error); } return callback(null, "\\begin{figure}\n \\centering\n \\includegraphics[width=" + width + "\\textwidth]{" + path + "}\n \\caption{" + (_this.captionEditor.getValue()) + "}\n \\label{" + _this.annotationId + "}\n\\end{figure}"); }); }; FigureWidget.prototype._destroyResizable = function() { this._resizableInited = false; return $(this.imgWrapperEl).resizable("destroy"); }; FigureWidget.prototype._initResizable = function() { var fivePercentGrid, fullWidth, _this = this; fullWidth = $(this.figureEl).innerWidth(); fivePercentGrid = fullWidth / 20; $(this.imgWrapperEl).resizable({ aspectRatio: this.aspectRatio, grid: [fivePercentGrid, fivePercentGrid / this.aspectRatio], minWidth: 4 * fivePercentGrid, maxWidth: fullWidth, minHeight: 4 * fivePercentGrid / this.aspectRatio, stop: function() { var ratio, width; ratio = $(_this.imgWrapperEl).outerWidth() / _this.figureWidth(); width = _this._roundToNearestFivePercent(ratio).toString(); return _this.setAttribute("width", width); } }); return this._resizableInited = true; }; FigureWidget.prototype._roundToNearestFivePercent = function(float) { return Math.round(float / 0.05) * 5; }; FigureWidget.prototype.figureWidth = function() { return $(this.figureEl).innerWidth(); }; return FigureWidget; })(Widget); OrchardEditorWidgets.WIDGET_TYPES["figure"] = FigureWidget; return FigureWidget; }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define('orchard/ui',["orchard/base", "orchard/event", "orchard/widgets/figure", "orchard/debug"], function(Base, Events, FigureWidget, debug) { var HEADING_CLASSES, OrchardUI; HEADING_CLASSES = ["chapter", "section", "subsection"]; OrchardUI = (function(_super) { __extends(OrchardUI, _super); OrchardUI.include(Events); function OrchardUI(editor) { this.bindToEditor(editor); } OrchardUI.prototype.bindToEditor = function(editor) { var widget, _i, _len, _ref, _ref1, _results, _this = this; if (this.editor != null) { this.editor.off("selection:change", this._onSelectionChange); this.editor.off("widget:new", this._onNewWidget); } this.editor = editor; this._onSelectionChange = function(selection) { return _this.onSelectionChange(selection); }; this.editor.on("selection:change", this._onSelectionChange); this._onNewWidget = function(widget) { return _this.onNewWidget(widget); }; this.editor.on("widget:new", this._onNewWidget); _ref1 = ((_ref = this.editor.widgets) != null ? _ref.getWidgets() : void 0) || []; _results = []; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { widget = _ref1[_i]; _results.push(this.onNewWidget(widget)); } return _results; }; OrchardUI.prototype.onSelectionChange = function(selection) { var annotation, annotations, className, line, _i, _len; line = selection.from.line; if (line === selection.to.line) { annotations = this.editor.getLineAnnotationsOnLine(line); className = null; for (_i = 0, _len = annotations.length; _i < _len; _i++) { annotation = annotations[_i]; if (HEADING_CLASSES.indexOf(annotation["class"]) > -1) { className = annotation["class"]; break; } } debug("toolbar", "selection line class", className); if (className != null) { return $("#line-class").val(className); } else { return $("#line-class").val("paragraph"); } } else { return debug("toolbar", "multi selection... don't know what to do yet!"); } }; OrchardUI.prototype.onLineClassChange = function(className) { var annotation, annotations, line, selection, _i, _len; if (this.editor.options.readonly) { return; } selection = this.editor.getSelection(); line = selection.from.line; if (line === selection.to.line) { annotations = this.editor.getLineAnnotationsOnLine(line); for (_i = 0, _len = annotations.length; _i < _len; _i++) { annotation = annotations[_i]; if (HEADING_CLASSES.indexOf(annotation["class"]) > -1) { this.editor.removeLineAnnotation(annotation.annotationId); } } debug("toolbar", "setting line class", className); if (className !== "paragraph") { return this.editor.addLineAnnotation(line, className); } } else { return debug("toolbar", "multi selection... don't know what to do yet!"); } }; OrchardUI.prototype.insertMath = function() { var selection, selectionPosition; if (this.editor.options.readonly) { return; } selectionPosition = this.editor.getSelection(); selection = this.editor.getSelectedText(); this.editor.replaceSelection("\\( " + selection + " \\)"); this.editor.setCursor({ line: selectionPosition.from.line, ch: selectionPosition.from.ch + 3 }); return this.editor.focus(); }; OrchardUI.prototype.insertWidget = function(type) { var selectionPosition; if (this.editor.options.readonly) { return; } selectionPosition = this.editor.getSelection(); this.editor.startOperation(); this.editor.replaceSelection("\n\n", "end"); this.editor.addWidget(selectionPosition.from.line + 1, type, {}); this.editor.endOperation(); return this.editor.setCursor({ line: selectionPosition.from.line + 2, ch: 0 }); }; OrchardUI.prototype.insertCitation = function() { var selectionPosition; if (this.editor.options.readonly) { return; } selectionPosition = this.editor.getSelection(); return this.editor.addInlineWidget(selectionPosition.from.line, selectionPosition.from.ch, "citation", {}); }; OrchardUI.prototype.insertReference = function() { var selectionPosition; if (this.editor.options.readonly) { return; } selectionPosition = this.editor.getSelection(); return this.editor.addInlineWidget(selectionPosition.from.line, selectionPosition.from.ch, "reference", {}); }; OrchardUI.prototype.onNewWidget = function(widget) { this.trigger("widget:new", widget); widget.on("selected", function() { return $(widget.el).addClass("oe-widget-selected"); }); return widget.on("deselected", function() { return $(widget.el).removeClass("oe-widget-selected"); }); }; return OrchardUI; })(Base); return OrchardUI; }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define('orchard/inline-widgets/inline-widget',["orchard/base", "orchard/event", "orchard/debug"], function(Base, Events, debug) { var InlineWidget, _ref; return InlineWidget = (function(_super) { __extends(InlineWidget, _super); function InlineWidget() { _ref = InlineWidget.__super__.constructor.apply(this, arguments); return _ref; } InlineWidget.include(Events); InlineWidget.prototype.getAttribute = function(attribute) { return this.data[attribute]; }; InlineWidget.prototype.setAttribute = function(attribute, value) { var oldValue; oldValue = this.data[attribute]; this.data[attribute] = value; this.trigger("" + attribute + ":update", value, oldValue); return this.editor.document.setWidgetAttribute(this.annotationId, attribute, value); }; InlineWidget.prototype.setAttributes = function(attributes) { var key, oldAttributes, value, _ref1, _results; oldAttributes = this.data; this.data = attributes; _ref1 = this.data; for (key in _ref1) { value = _ref1[key]; if (oldAttributes[key] !== this.data[key]) { this.trigger("" + key + ":update", this.data[key], oldAttributes[key]); } delete oldAttributes[key]; } _results = []; for (key in oldAttributes) { value = oldAttributes[key]; _results.push(this.trigger("" + key + ":update", null, oldAttributes[key])); } return _results; }; InlineWidget.prototype.markAsChanged = function() { return this.editor.inlineWidgets.markInlineWidgetElementAsChanged(this.annotationId); }; InlineWidget.prototype.refreshSelectEl = function() { if (this.selectEl == null) { return; } $(this.selectEl).selectpicker("refresh"); $(this.el).find(".caret").hide(); $(this.el).find(".filter-option").css({ position: "static" }); $(this.el).find(".dropdown-toggle").css({ width: "auto" }); return $(this.el).find(".bootstrap-select").css({ width: "auto" }); }; return InlineWidget; })(Base); }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define('orchard/inline-widgets/dropdown-widget',["orchard/inline-widgets/inline-widget", "orchard/debug"], function(InlineWidget, debug) { var DropdownWidget; return DropdownWidget = (function(_super) { __extends(DropdownWidget, _super); DropdownWidget.prototype.className = ""; function DropdownWidget(editor, annotationId, data) { var _ref, _this = this; this.editor = editor; this.annotationId = annotationId; this.data = data; this.el = typeof document !== "undefined" && document !== null ? document.createElement("span") : void 0; if (this.el != null) { this.el.innerHTML = this.template; this.el.setAttribute("class", this.className); this.selectEl = (_ref = this.el) != null ? _ref.getElementsByTagName("select")[0] : void 0; if (this.editor.options.readonly) { this.selectEl.setAttribute("disabled", "disabled"); } $(this.selectEl).selectpicker({ showContent: false }); this._bindToSelect(); } this._bindToEditor(); this.on("dom:insert", function() { return _this._onDomInsert(); }); } DropdownWidget.prototype.destory = function() { return this._unbindFromEditor(); }; DropdownWidget.prototype._onDomInsert = function() { return this._refresh(); }; DropdownWidget.prototype._refresh = function() { var bibliography, child, citationId, option, options, reference, references, _i, _j, _len, _len1, _ref; if (this.el == null) { return; } while (child = this.selectEl.children[0]) { child.remove(); } references = []; _ref = this.editor.bibliographies || []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { bibliography = _ref[_i]; references = references.concat(bibliography.getReferencesAsArray()); } debug("BIBLIOGRAPHIES", references); citationId = this.getAttribute("citationId"); option = $(""); optgroup.attr("label", label); for (_j = 0, _len1 = options.length; _j < _len1; _j++) { option = options[_j]; optgroup.append(option); } return $(_this.selectEl).append(optgroup); }; createGroup("Sections", sections); createGroup("Figures", figures); createGroup("Equations", equations); if (selectedHandler == null) { this.selectEl.selectedIndex = 0; } return this.refreshSelectEl(); }; return ReferenceInlineWidget; })(DropdownWidget); OrchardEditorInlineWidgets.INLINE_WIDGET_TYPES["reference"] = ReferenceInlineWidget; return ReferenceInlineWidget; }); }).call(this); (function() { var __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; define('orchard/inline-widgets/citation',["orchard/inline-widgets/dropdown-widget", "orchard/orchard/inline-widgets", "orchard/debug"], function(DropdownWidget, OrchardEditorInlineWidgets, debug) { var CitationInlineWidget, _ref; CitationInlineWidget = (function(_super) { __extends(CitationInlineWidget, _super); function CitationInlineWidget() { _ref = CitationInlineWidget.__super__.constructor.apply(this, arguments); return _ref; } CitationInlineWidget.prototype.template = ""; CitationInlineWidget.prototype.className = "oe-citation"; CitationInlineWidget.prototype.convertToTex = function(callback) { if (callback == null) { callback = function(error, tex) {}; } return callback(null, "\\cite{" + (this.getAttribute("citationId")) + "}"); }; CitationInlineWidget.prototype._bindToEditor = function() { var _this = this; this._onBibliographyRefreshHandler = function() { return _this._onBibliographyRefresh(); }; return this.editor.on("bibliographies:update", this._onBibliographyRefreshHandler); }; CitationInlineWidget.prototype._unbindFromEditor = function() { return this.editor.off("bibliographies:update", this._onBibliographyRefreshHandler); }; CitationInlineWidget.prototype._bindToSelect = function() { var _ref1, _this = this; if ((_ref1 = this.selectEl) != null) { _ref1.onchange = function() { var citationId; citationId = _this.selectEl.options[_this.selectEl.selectedIndex].getAttribute("value"); _this.setAttribute("citationId", citationId); return _this._refresh(); }; } return this.on("citationId:update", function() { return _this._refresh(); }); }; CitationInlineWidget.prototype._onBibliographyRefresh = function() { return this._refresh(); }; CitationInlineWidget.prototype._refresh = function() { var bibliography, child, citationId, option, options, reference, references, _i, _j, _len, _len1, _ref1; if (this.el == null) { return; } while (child = this.selectEl.children[0]) { child.remove(); } references = []; _ref1 = this.editor.bibliographies || []; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { bibliography = _ref1[_i]; references = references.concat(bibliography.getReferencesAsArray()); } debug("BIBLIOGRAPHIES", references); citationId = this.getAttribute("citationId"); option = $("