debian-mirror-gitlab/app/assets/javascripts/blob_edit/edit_blob.js

84 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-09-13 17:45:13 +05:30
(function() {
var bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
this.EditBlob = (function() {
function EditBlob(assets_path, ace_mode) {
if (ace_mode == null) {
ace_mode = null;
}
this.editModeLinkClickHandler = bind(this.editModeLinkClickHandler, this);
ace.config.set("modePath", assets_path + "/ace");
ace.config.loadModule("ace/ext/searchbox");
this.editor = ace.edit("editor");
this.editor.focus();
if (ace_mode) {
this.editor.getSession().setMode("ace/mode/" + ace_mode);
}
$('form').submit((function(_this) {
return function() {
return $("#file-content").val(_this.editor.getValue());
};
2016-09-29 09:46:39 +05:30
// Before a form submission, move the content from the Ace editor into the
// submitted textarea
2016-09-13 17:45:13 +05:30
})(this));
this.initModePanesAndLinks();
2016-11-03 12:29:30 +05:30
this.initSoftWrap();
new gl.BlobLicenseSelectors({
2016-09-13 17:45:13 +05:30
editor: this.editor
});
new BlobGitignoreSelectors({
editor: this.editor
});
2016-11-03 12:29:30 +05:30
new gl.BlobCiYamlSelectors({
2016-09-13 17:45:13 +05:30
editor: this.editor
});
}
EditBlob.prototype.initModePanesAndLinks = function() {
this.$editModePanes = $(".js-edit-mode-pane");
this.$editModeLinks = $(".js-edit-mode a");
return this.$editModeLinks.click(this.editModeLinkClickHandler);
};
EditBlob.prototype.editModeLinkClickHandler = function(event) {
var currentLink, currentPane, paneId;
event.preventDefault();
currentLink = $(event.target);
paneId = currentLink.attr("href");
currentPane = this.$editModePanes.filter(paneId);
this.$editModeLinks.parent().removeClass("active hover");
currentLink.parent().addClass("active hover");
this.$editModePanes.hide();
currentPane.fadeIn(200);
if (paneId === "#preview") {
2016-11-03 12:29:30 +05:30
this.$toggleButton.hide();
2016-09-13 17:45:13 +05:30
return $.post(currentLink.data("preview-url"), {
content: this.editor.getValue()
}, function(response) {
currentPane.empty().append(response);
return currentPane.syntaxHighlight();
});
} else {
2016-11-03 12:29:30 +05:30
this.$toggleButton.show();
2016-09-13 17:45:13 +05:30
return this.editor.focus();
}
};
2016-11-03 12:29:30 +05:30
EditBlob.prototype.initSoftWrap = function() {
this.isSoftWrapped = false;
this.$toggleButton = $('.soft-wrap-toggle');
this.$toggleButton.on('click', this.toggleSoftWrap.bind(this));
};
EditBlob.prototype.toggleSoftWrap = function(e) {
this.isSoftWrapped = !this.isSoftWrapped;
this.$toggleButton.toggleClass('soft-wrap-active', this.isSoftWrapped);
this.editor.getSession().setUseWrapMode(this.isSoftWrapped);
};
2016-09-13 17:45:13 +05:30
return EditBlob;
})();
}).call(this);