foreman/app/assets/javascripts/config_template.js @ ad01d4d2
c4ba83b0 | Ohad Levy | var $editor
|
|
5e2b847d | Amos Benari | $(document).on('ContentLoad', function(){onEditorLoad()});
|
|
c4ba83b0 | Ohad Levy | ||
5e2b847d | Amos Benari | $(document).on('click','#config_template_submit', function(){
|
|
if($('.diffMode').exists()){
|
|||
set_edit_mode( $(".template_text"));
|
|||
}
|
|||
})
|
|||
c4ba83b0 | Ohad Levy | ||
5e2b847d | Amos Benari | $(document).on('change', '.template_file', function(e){
|
|
if ($('.template_file').val() != '') upload_file(e);
|
|||
})
|
|||
c4ba83b0 | Ohad Levy | ||
5e2b847d | Amos Benari | $(document).on('change','#keybinding', function(){
|
|
set_keybinding()
|
|||
})
|
|||
c4ba83b0 | Ohad Levy | ||
5e2b847d | Amos Benari | $(document).keyup(function(e) {
|
|
if (e.keyCode == 27) { // esc
|
|||
exit_fullscreen();
|
|||
}
|
|||
c4ba83b0 | Ohad Levy | });
|
|
57a74ec9 | Amos Benari | ||
5e2b847d | Amos Benari | function onEditorLoad(){
|
|
var template_text = $(".template_text");
|
|||
if ($.browser && $.browser.msie && $.browser.version.slice(0,1) < 10) {
|
|||
$('.subnav').hide();
|
|||
if ($('.diffMode').exists()) {
|
|||
IE_diff_mode(template_text);
|
|||
}
|
|||
}else{
|
|||
if (template_text.exists()){
|
|||
create_editor(template_text)
|
|||
}
|
|||
if ($('.diffMode').exists()) {
|
|||
set_diff_mode(template_text);
|
|||
} else {
|
|||
set_edit_mode(template_text);
|
|||
}
|
|||
}
|
|||
}
|
|||
function set_keybinding(){
|
|||
var vim = require("ace/keyboard/vim").handler;
|
|||
var emacs = require("ace/keyboard/emacs").handler;
|
|||
var keybindings = [
|
|||
null, // Null = use "default" keymapping
|
|||
vim,
|
|||
emacs];
|
|||
$editor.setKeyboardHandler(keybindings[$("#keybinding")[0].selectedIndex]);
|
|||
}
|
|||
function upload_file(evt){
|
|||
if(window.File && window.FileList && window.FileReader)
|
|||
{
|
|||
ad01d4d2 | Walden Raines | if (!confirm(__("You are about to override the editor content, are you sure?"))) {
|
|
5e2b847d | Amos Benari | $('.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{
|
|||
// Browser can't read the file content,
|
|||
// the file will be uploaded to the server on form submit.
|
|||
// Set editor to read only mode
|
|||
$editor.setTheme("ace/theme/clouds");
|
|||
$editor.setReadOnly(true);
|
|||
}
|
|||
}
|
|||
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 | ||
5e2b847d | Amos Benari | function create_editor(item) {
|
|
c4ba83b0 | Ohad Levy | item.parent().prepend("<div id='editor1'></div>");
|
|
57a74ec9 | Amos Benari | ||
$("#editor1")
|
|||
.css("position","relative")
|
|||
c4ba83b0 | Ohad Levy | .height(item.height() || '360')
|
|
5e2b847d | Amos Benari | .width(item.width()+10)
|
|
.css('top', '-20px');
|
|||
c4ba83b0 | Ohad Levy | item.hide();
|
|
57a74ec9 | Amos Benari | ||
c4ba83b0 | Ohad Levy | $editor = ace.edit("editor1");
|
|
$editor.setShowPrintMargin(false);
|
|||
$editor.renderer.setShowGutter(false);
|
|||
}
|
|||
57a74ec9 | Amos Benari | ||
5e2b847d | Amos Benari | function set_fullscreen(){
|
|
$('#main').append($("#editor1"));
|
|||
$("#editor1")
|
|||
.height($(window).height()-80)
|
|||
.width($('#content').width())
|
|||
.addClass('container');
|
|||
$('#content').hide();
|
|||
$('.navbar').addClass('hidden');
|
|||
$('.logo-bar').addClass('hidden');
|
|||
$editor.resize();
|
|||
$('#main').append($('.exit-fullscreen'));
|
|||
bf4a13d3 | Walden Raines | $('.exit-fullscreen').removeClass('hidden');
|
|
5e2b847d | Amos Benari | $(window).scrollTop(0);
|
|
}
|
|||
function exit_fullscreen(){
|
|||
$(".template_text").show();
|
|||
$('#content').show();
|
|||
$('.navbar').removeClass('hidden');
|
|||
$('.logo-bar').removeClass('hidden');
|
|||
$(".template_text").parent().prepend($("#editor1"))
|
|||
$("#editor1")
|
|||
.height($(".template_text").height() || '360')
|
|||
.width($(".template_text").width()+10)
|
|||
$(".template_text").hide();
|
|||
$editor.resize();
|
|||
bf4a13d3 | Walden Raines | $('.exit-fullscreen').addClass('hidden');
|
|
5e2b847d | 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){
|
|||
5e2b847d | Amos Benari | if( $editor == undefined) return;
|
|
c4ba83b0 | Ohad Levy | $editor.setTheme("ace/theme/twilight");
|
|
$editor.setReadOnly(false);
|
|||
var session = $editor.getSession();
|
|||
0fe08ca9 | Amos Benari | session.setMode("ace/mode/ruby");
|
|
57a74ec9 | Amos Benari | ||
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 session = $editor.getSession();
|
|||
0fe08ca9 | Amos Benari | session.setMode("ace/mode/diff");
|
|
c4ba83b0 | Ohad Levy | var patch = JsDiff.createPatch(item.attr('data-file-name'), $('#old').text(), $('#new').text());
|
|
dfd511ba | Amos Benari | patch = patch.replace(/^(.*\n){0,4}/,'');
|
|
if (patch.length == 0)
|
|||
ad01d4d2 | Walden Raines | patch = __("No changes")
|
|
dfd511ba | Amos Benari | ||
c4ba83b0 | Ohad Levy | $(session).off('change');
|
|
session.setValue(patch);
|
|||
ceabd2b3 | Sam Kottler | }
|
|
function submit_code() {
|
|||
if($('.template_text').hasClass('diffMode')) {
|
|||
set_code();
|
|||
}
|
|||
c4ba83b0 | Ohad Levy | }
|
|
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){
|
|
ad01d4d2 | Walden Raines | if (!confirm(__("You are about to override the editor content with a previous version, are you sure?"))) return;
|
|
c4ba83b0 | Ohad Levy | ||
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');
|
|
ad01d4d2 | Walden Raines | $('#config_template_audit_comment').text(Jed.sprintf(__("Revert to revision from: %s"), time))
|
|
c4ba83b0 | Ohad Levy | }
|
|
})
|
|||
dfd511ba | Amos Benari | }
|