Created
May 28, 2014 04:52
-
-
Save amb26/f31d2c39ef809d15b032 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fluid.defaults("souq.codeMirror", { | |
gradeNames: ["fluid.viewComponent", "autoInit"], | |
codeMirrorOpts: ["lineNumbers", "mode", "gutters"], | |
// we relay all raw CodeMirror events with additional argument "that" in 0th place, rest shifted to the right | |
codeMirrorEvents: ["onChange", "onCursorActivity", "onViewportChange", "onGutterClick", "onScroll", "onUpdate"], | |
events: { | |
onCreateCodeMirror: null, | |
onChange: null, | |
onCursorActivity: null, | |
onViewportChange: null, | |
onGutterClick: null, | |
onScroll: null, | |
onUpdate: null | |
}, | |
listeners: { | |
onCreate: "souq.codeMirror.create" | |
}, | |
invokers: { | |
getContent: "souq.codeMirror.getContent({that}.editor)", | |
setContent: "souq.codeMirror.setContent({that}.editor, {arguments}.0)", | |
isEmpty: "souq.codeMirror.isEmpty!({that}.editor)" | |
} | |
}); | |
fluid.defaults("souq.lintingCodeMirror", { | |
gradeNames: ["souq.codeMirror", "autoInit"], | |
events: { | |
onUpdateLinting: null, | |
// an event derived from "onUpdateLinting" which fires (that, true/false, etc.) depending on whether the editor | |
// contents were linted as valid or not (currently detected by having any lint markers) | |
onValidatedContentChange: null | |
}, | |
listeners: { | |
onCreateCodeMirror: "souq.codeMirror.onCreateLinting", | |
"onUpdateLinting.onValidatedContentChange": "souq.codeMirror.onUpdateLinting" | |
}, | |
invokers: { | |
showLintMarkers: "souq.codeMirror.showLintMarkers!({that}, {arguments}.0, {arguments}.1)", | |
}, | |
// options to be passed raw to the codeMirror linting helper - can accept funcs such as getAnnotation, formatAnnotation etc. | |
lint: { | |
tooltips: true, | |
async: false | |
} | |
}); | |
souq.codeMirror.makeEventListener = function (that, event) { | |
return function () { | |
var args = fluid.makeArray(arguments); | |
args.unshift(that); | |
return event.fire.apply(null, args); | |
}; | |
}; | |
souq.codeMirror.create = function (that) { | |
var opts = fluid.filterKeys($.extend({}, that.options), that.options.codeMirrorOpts); | |
var events = that.options.codeMirrorEvents; | |
for (var i = 0; i < events.length; ++ i) { | |
var event = events[i]; | |
opts[event] = souq.codeMirror.makeEventListener(that, that.events[event]); | |
} | |
that.events.onCreateCodeMirror.fire(that, opts); | |
that.editor = CodeMirror.fromTextArea(that.container[0], opts); | |
that.wrapper = that.editor.getWrapperElement(); | |
}; | |
souq.codeMirror.onCreateLinting = function (that, opts) { | |
var lint = fluid.copy(that.options.lint); | |
lint.onUpdateLinting = souq.codeMirror.makeEventListener(that, that.events.onUpdateLinting); | |
opts.lint = lint; | |
}; | |
souq.codeMirror.getContent = function (editor) { | |
return editor.getDoc().getValue(); | |
}; | |
souq.codeMirror.setContent = function (editor, content) { | |
var doc = editor.getDoc(); | |
doc.setValue(content); | |
}; | |
souq.codeMirror.isEmpty = function (editor) { | |
// TODO: If the editor is created with some content, we should get at this directly (via the textarea, etc.) | |
if (!editor) { | |
return true; | |
} | |
var doc = editor.getDoc(); | |
if (doc.lineCount() > 1) { | |
return false; | |
} | |
var first = doc.getLine(0); | |
return $.trim(first).length === 0; | |
}; | |
souq.codeMirror.showLintMarkers = function (that, visibility, selfDispatch) { | |
if (!that.editor) { | |
// Since CodeMirror does not participate in the GINGER WORLD we can't deal with this kind of race in a civilized | |
// manner - the linting may apply during the construction process of the editor during which we can't find its | |
// parent element | |
if (!selfDispatch) { | |
setTimeout(function() { | |
that.showLintMarkers(visibility, true); | |
}, 1); | |
} | |
} else { | |
var wrapper = that.editor.getWrapperElement(); | |
var markers = $(".CodeMirror-lint-marker-error", wrapper); | |
markers.toggle(visibility); | |
} | |
}; | |
souq.codeMirror.onUpdateLinting = function (that, annotationsNotSorted, annotations) { | |
that.events.onValidatedContentChange.fire(that, annotationsNotSorted.length === 0, annotationsNotSorted, annotations); | |
that.showLintMarkers(!that.isEmpty()); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment