Github user robertkowalski commented on a diff in the pull request:
https://github.com/apache/couchdb-fauxton/pull/391#discussion_r28864820
--- Diff: app/addons/components/react-components.react.jsx ---
@@ -63,7 +64,188 @@ function (app, FauxtonAPI, React, Components,
beautifyHelper) {
});
var CodeEditor = React.createClass({
+ getDefaultProps: function () {
+ return {
+ id : 'code-editor',
+ mode : 'javascript',
+ theme : 'idle_fingers',
+ fontSize : 13,
+ code : '',
+ showGutter : true,
+ highlightActiveLine : true,
+ showPrintMargin : false,
+ autoScrollEditorIntoView: true,
+ setHeightWithJS: true,
+ isFullPageEditor: false,
+ };
+ },
+
+ hasChanged: function () {
+ return !_.isEqual(this.props.code, this.getValue());
+ },
+
+ setupAce: function (props) {
+ this.editor = ace.edit(this.getDOMNode(this.refs.ace));
+ // Automatically scrolling cursor into view after selection
+ // change this will be disabled in the next version
+ // set editor.$blockScrolling = Infinity to disable this message
+ this.editor.$blockScrolling = Infinity;
+
+ this.editor.setShowPrintMargin(props.showPrintMargin);
+ this.editor.autoScrollEditorIntoView =
props.autoScrollEditorIntoView;
+ this.setEditorValue(props.code);
+ this.setHeightToLineCount();
+ this.removeIncorrectAnnotations();
+ this.editor.getSession().setMode("ace/mode/" + props.mode);
+ this.editor.setTheme("ace/theme/" + props.theme);
+ this.editor.setFontSize(this.props.fontSize);
+ },
+
+ setupEvents: function () {
+ $(window).on('beforeunload.editor_' + this.editorId,
_.bind(this.quitWarningMsg));
+ FauxtonAPI.beforeUnload('editor_' + this.editorId,
_.bind(this.quitWarningMsg, this));
+ },
+
+ quitWarningMsg: function () {
+ if (this.hasChanged()) {
+ return 'Your changes have not been saved. Click cancel to return
to the document.';
+ }
+ },
+
+ removeEvents: function () {
+ $(window).off('beforeunload.editor_' + this.editorId);
+ $(window).off('resize.editor', this.onPageResize);
+ FauxtonAPI.removeBeforeUnload('editor_' + this.editorId);
+ },
+
+ setHeightToLineCount: function () {
+ if (!this.props.setHeightWithJS) {
+ return;
+ }
+
+ var lines = this.editor.getSession().getDocument().getLength();
+
+ if (this.props.isFullPageEditor) {
+ var maxLines = this.getMaxAvailableLinesOnPage();
+ lines = lines < maxLines ? lines : maxLines;
+ }
+ this.editor.setOptions({
+ maxLines: lines
+ });
+ },
+
+ // List of JSHINT errors to ignore
+ // Gets around problem of anonymous functions not being a valid
statement
+ excludedViewErrors: [
+ "Missing name in function declaration.",
+ "['{a}'] is better written in dot notation."
+ ],
+
+ isIgnorableError: function (msg) {
+ return _.contains(this.excludedViewErrors, msg);
+ },
+
+ removeIncorrectAnnotations: function () {
+ var editor = this.editor,
+ isIgnorableError = this.isIgnorableError;
+
+ this.editor.getSession().on("changeAnnotation", function () {
+ var annotations = editor.getSession().getAnnotations();
+
+ var newAnnotations = _.reduce(annotations, function (annotations,
error) {
+ if (!isIgnorableError(error.raw)) {
+ annotations.push(error);
+ }
+ return annotations;
+ }, []);
+
+ if (annotations.length !== newAnnotations.length) {
+ editor.getSession().setAnnotations(newAnnotations);
+ }
+ });
+ },
+
+ componentDidMount: function () {
+ this.setupAce(this.props);
+ this.setupEvents();
+ },
+
+ componentWillUnmount: function () {
+ this.removeEvents();
+ this.editor.destroy();
+ },
+
+ componentWillReceiveProps: function (nextProps) {
+ this.setupAce(nextProps);
+ },
+
+ editSaved: function () {
+ return this.hasChanged();
+ },
+
+ getTitleFragment: function () {
+ if (!this.props.docs) {
+ return <strong>{this.props.title}</strong>;
+ }
+
+ return (
+ <label>
+ <strong>{this.props.title}</strong>
+ <a
+ className="help-link"
+ data-bypass="true"
+ href={this.props.docs}
+ target="_blank"
+ >
+ <i className="icon-question-sign"></i>
+ </a>
+ </label>
+ );
+ },
+
+ getAnnotations: function () {
+ return this.editor.getSession().getAnnotations();
+ },
+
+ hadValidCode: function () {
+ var errors = this.getAnnotations();
+ // By default CouchDB view functions don't pass lint
+ return _.every(errors, function (error) {
+ return this.isIgnorableError(error.raw);
+ }, this);
+ },
+
+ setEditorValue: function (code, lineNumber) {
+ lineNumber = lineNumber ? lineNumber : -1;
+ this.editor.setValue(code, lineNumber);
+ },
+
+ getValue: function () {
+ return this.editor.getValue();
+ },
+
+ getEditor: function () {
+ return this;
+ },
+
render: function () {
+ return (
+ <div className="control-group">
+ {this.getTitleFragment()}
+ <div ref="ace" className="js-editor" id={this.props.id}></div>
+ <Beautify code={this.props.code}
beautifiedCode={this.setEditorValue} />
+ </div>
+ );
+ }
+
+ });
+
+ /*var CodeEditor = React.createClass({
--- End diff --
i think we can remove that guy
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---