foreman/public/javascripts/config_template.js @ dbeead7b
c4ba83b0 | Ohad Levy | var $editor
|
|
$(function() {
|
|||
var template_text = $(".template_text");
|
|||
19553511 | Amos Benari | if ($.browser.msie && $.browser.version.slice(0,1) < 10) {
|
|
a0629495 | Amos Benari | $('.subnav').hide();
|
|
if ($('.diffMode').size() >0) {
|
|||
IE_diff_mode(template_text);
|
|||
}
|
|||
}else{
|
|||
if (template_text.size() >0 ) { create_editor(template_text) };
|
|||
if ($('.diffMode').size() >0) {
|
|||
set_diff_mode(template_text);
|
|||
} else {
|
|||
set_edit_mode(template_text);
|
|||
}
|
|||
c4ba83b0 | Ohad Levy | }
|
|
19553511 | Amos Benari | $(".template_file").on("change", function(evt){
|
|
if ($(".template_file").val() == "") return;
|
|||
if(window.File && window.FileList && window.FileReader)
|
|||
{
|
|||
var answer = confirm("You are about to override the editor content, Are You Sure?")
|
|||
if (!answer) { $('.template_file').val(""); return;}
|
|||
var files = evt.target.files; // files is a FileList object
|
|||
for (var i = 0, f; f = files[i]; i++) {
|
|||
var reader = new FileReader();
|
|||
// Closure to capture the file information.
|
|||
reader.onloadend = function(evt) {
|
|||
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
|
|||
$('#new').text(( evt.target.result));
|
|||
set_edit_mode($('.template_text'));
|
|||
}
|
|||
};
|
|||
// Read in the file as text.
|
|||
reader.readAsText(f);
|
|||
$('.template_file').val("");
|
|||
}
|
|||
}else{
|
|||
//Set editor in read only mode
|
|||
$editor.setTheme("ace/theme/clouds");
|
|||
$editor.setReadOnly(true);
|
|||
}
|
|||
c4ba83b0 | Ohad Levy | ||
})
|
|||
$("#keybinding").on("change", function() {
|
|||
var vim = require("ace/keyboard/keybinding/vim").Vim;
|
|||
var emacs = require("ace/keyboard/keybinding/emacs").Emacs;
|
|||
var keybindings = {
|
|||
19553511 | Amos Benari | Default: null, // Null = use "default" keymapping
|
|
Vim: vim,
|
|||
Emacs: emacs};
|
|||
c4ba83b0 | Ohad Levy | ||
$editor.setKeyboardHandler(keybindings[$("#keybinding").val()]);
|
|||
})
|
|||
});
|
|||
57a74ec9 | Amos Benari | ||
function snippet_changed(item){
|
|||
var checked = !!$(item).attr('checked');
|
|||
$('#kind_selector').toggle(!checked);
|
|||
$('#snippet_message').toggle(checked);
|
|||
$('#association').toggle(!checked);
|
|||
}
|
|||
c4ba83b0 | Ohad Levy | function create_editor(item) {
|
|
item.parent().prepend("<div id='editor1'></div>");
|
|||
57a74ec9 | Amos Benari | ||
$("#editor1")
|
|||
.css("position","relative")
|
|||
c4ba83b0 | Ohad Levy | .height(item.height() || '360')
|
|
.width(item.width());
|
|||
item.hide();
|
|||
57a74ec9 | Amos Benari | ||
c4ba83b0 | Ohad Levy | $editor = ace.edit("editor1");
|
|
$editor.setShowPrintMargin(false);
|
|||
$editor.renderer.setShowGutter(false);
|
|||
}
|
|||
57a74ec9 | Amos Benari | ||
c4ba83b0 | Ohad Levy | ||
function set_preview(){
|
|||
if($('.template_text').hasClass('diffMode')) return;
|
|||
$('.template_text').addClass('diffMode');
|
|||
$('#new').html( $editor.getSession().getValue());
|
|||
set_diff_mode($('.template_text'))
|
|||
}
|
|||
function set_code(){
|
|||
$('.template_text').removeClass('diffMode');
|
|||
set_edit_mode($('.template_text'));
|
|||
}
|
|||
function set_edit_mode(item){
|
|||
$editor.setTheme("ace/theme/twilight");
|
|||
$editor.setReadOnly(false);
|
|||
57a74ec9 | Amos Benari | var RubyMode = require("ace/mode/ruby").Mode;
|
|
c4ba83b0 | Ohad Levy | var session = $editor.getSession();
|
|
57a74ec9 | Amos Benari | session.setMode(new RubyMode());
|
|
c4ba83b0 | Ohad Levy | session.setValue($('#new').text());
|
|
57a74ec9 | Amos Benari | session.on('change', function(){
|
|
c4ba83b0 | Ohad Levy | item.text(session.getValue());
|
|
57a74ec9 | Amos Benari | });
|
|
c4ba83b0 | Ohad Levy | }
|
|
57a74ec9 | Amos Benari | ||
c4ba83b0 | Ohad Levy | function set_diff_mode(item){
|
|
$editor.setTheme("ace/theme/clouds");
|
|||
$editor.setReadOnly(true);
|
|||
var DiffMode = require("ace/mode/diff").Mode;
|
|||
var session = $editor.getSession();
|
|||
session.setMode(new DiffMode());
|
|||
var patch = JsDiff.createPatch(item.attr('data-file-name'), $('#old').text(), $('#new').text());
|
|||
$(session).off('change');
|
|||
session.setValue(patch);
|
|||
}
|
|||
57a74ec9 | Amos Benari | ||
a0629495 | Amos Benari | function IE_diff_mode(item){
|
|
718be0aa | Amos Benari | var patch = JsDiff.createPatch(item.attr('data-file-name'), $('#old').contents().text() , $('#new').contents().text());
|
|
a0629495 | Amos Benari | item.val(patch);
|
|
item.attr('readOnly', true);
|
|||
}
|
|||
c4ba83b0 | Ohad Levy | function revert_template(item){
|
|
var answer = confirm("You are about to override the editor content with a previous version, Are You Sure?")
|
|||
if (!answer) return;
|
|||
var version = $(item).attr('data-version');
|
|||
var url = $(item).attr('data-url');
|
|||
$.ajax({
|
|||
19553511 | Amos Benari | type: 'get',
|
|
url: url,
|
|||
data:'version=' + version,
|
|||
complete: function(res) {
|
|||
$('#primary_tab').click();
|
|||
718be0aa | Amos Benari | if ($.browser.msie && $.browser.version.slice(0,1) < 10){
|
|
dbeead7b | Amos Benari | $('.template_text').val(res.responseText);
|
|
718be0aa | Amos Benari | } else {
|
|
$('#new').text(res.responseText);
|
|||
set_edit_mode($('.template_text'));
|
|||
}
|
|||
19553511 | Amos Benari | var time = $(item).closest('div.row').find('h6 span').attr('data-original-title');
|
|
$('#config_template_audit_comment').text("Revert to revision from: " + time)
|
|||
c4ba83b0 | Ohad Levy | }
|
|
})
|
|||
}
|