You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
381 lines
7.3 KiB
381 lines
7.3 KiB
4 years ago
|
var ModuleSnippets = {
|
||
|
|
||
|
initialized: false,
|
||
|
|
||
|
init: function () {
|
||
|
|
||
|
if (this.initialized)
|
||
|
return;
|
||
|
|
||
|
this.initialized = true;
|
||
|
|
||
|
this.build(); // Main functions
|
||
|
this.events(); // Events functions
|
||
|
},
|
||
|
|
||
|
|
||
|
// Main functions
|
||
|
build: function () {
|
||
|
this.executeBtn();
|
||
|
this.saveBtn();
|
||
|
this.clearBtn();
|
||
|
this.deleteBtn();
|
||
|
this.nameSelection();
|
||
|
},
|
||
|
|
||
|
|
||
|
// Events functions
|
||
|
events: function () {
|
||
|
this.MouseTrap();
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
MouseTrap: function () {
|
||
|
|
||
|
Mousetrap.bind(['ctrl+enter', 'command+enter'], function (event) {
|
||
|
if (event.preventDefault) {
|
||
|
event.preventDefault();
|
||
|
} else {
|
||
|
event.returnValue = false;
|
||
|
}
|
||
|
|
||
|
ModuleSnippets.executeRun();
|
||
|
|
||
|
return false;
|
||
|
});
|
||
|
|
||
|
Mousetrap.bind(['ctrl+s', 'command+s'], function (event) {
|
||
|
|
||
|
if (event.preventDefault) {
|
||
|
event.preventDefault();
|
||
|
} else {
|
||
|
event.returnValue = false;
|
||
|
}
|
||
|
|
||
|
ModuleSnippets.saveFragment();
|
||
|
|
||
|
return false;
|
||
|
});
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
saveFragment: function () {
|
||
|
|
||
|
let key_fragment = $('#fragmentKey').val();
|
||
|
|
||
|
if (key_fragment.length == '') {
|
||
|
jPrompt(new_text, '', new_title, function (name) {
|
||
|
if (name) {
|
||
|
ModuleSnippets.saveNew(name);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
else {
|
||
|
ModuleSnippets.saveExist(key_fragment);
|
||
|
}
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
saveNew: function (name) {
|
||
|
|
||
|
let code = $("#code_text").val();
|
||
|
|
||
|
$.ajax({
|
||
|
type: "POST",
|
||
|
url: "index.php?do=modules&action=modedit&mod=snippets&moduleaction=new&cp=" + session,
|
||
|
data: {
|
||
|
"fragment_name": name,
|
||
|
"fragment_code": code
|
||
|
},
|
||
|
dataType: "json",
|
||
|
beforeSend: function () {
|
||
|
$.alerts._overlay('show');
|
||
|
},
|
||
|
success: function (data) {
|
||
|
$.alerts._overlay('hide');
|
||
|
|
||
|
$.jGrowl(data['message'], {
|
||
|
header: data['header'],
|
||
|
theme: data['theme']
|
||
|
});
|
||
|
|
||
|
ModuleSnippets.changeSelect(data['fragments'], data['key']);
|
||
|
$('#fragmentKey').val(data['key']);
|
||
|
ModuleSnippets.showHideDeleteBtn();
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
saveExist: function (key) {
|
||
|
let code = $("#code_text").val();
|
||
|
|
||
|
$.ajax({
|
||
|
type: "POST",
|
||
|
url: "index.php?do=modules&action=modedit&mod=snippets&moduleaction=save&cp=" + session,
|
||
|
data: {
|
||
|
"fragment_key": key,
|
||
|
"fragment_code": code
|
||
|
},
|
||
|
dataType: "json",
|
||
|
beforeSend: function () {
|
||
|
$.alerts._overlay('show');
|
||
|
},
|
||
|
success: function (data) {
|
||
|
$.alerts._overlay('hide');
|
||
|
|
||
|
$.jGrowl(data['message'], {
|
||
|
header: data['header'],
|
||
|
theme: data['theme']
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
executeRun: function () {
|
||
|
let code = $("#code_text").val();
|
||
|
let profiling = $("#profilingCheck").is(':checked') ? 1 : 0;
|
||
|
|
||
|
var Dialog = $('<div id="ajax-dialog-result" style="display:none;" class="ajax-dialog"></div>').appendTo('body');
|
||
|
var dialogWidth = $(window).width() * 0.9;
|
||
|
var dialogHeight = $(window).height() * 0.8;
|
||
|
|
||
|
$.ajax({
|
||
|
type: "POST",
|
||
|
url: "index.php?do=modules&action=modedit&mod=snippets&moduleaction=execute&cp=" + session,
|
||
|
data: {
|
||
|
'fragment_code': code,
|
||
|
'fragment_profiling': profiling
|
||
|
},
|
||
|
beforeSend: function () {
|
||
|
$.alerts._overlay('show');
|
||
|
},
|
||
|
success: function (data) {
|
||
|
$.alerts._overlay('hide');
|
||
|
|
||
|
Dialog.html(data);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Dialog.dialog({
|
||
|
autoOpen: false,
|
||
|
modal: true,
|
||
|
dialogClass: 'fixed-dialog',
|
||
|
close: function (event, ui) {
|
||
|
$(this).dialog('destroy').remove();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
Dialog.dialog("option", "title", result_dialog);
|
||
|
Dialog.dialog("option", "width", dialogWidth);
|
||
|
Dialog.dialog("option", "height", dialogHeight);
|
||
|
Dialog.dialog("open");
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
executeBtn: function () {
|
||
|
$('.Execute').on('click', function (event) {
|
||
|
event.preventDefault();
|
||
|
|
||
|
ModuleSnippets.executeRun();
|
||
|
|
||
|
});
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
saveBtn: function () {
|
||
|
$('.SaveFragment').on('click', function (event) {
|
||
|
event.preventDefault();
|
||
|
|
||
|
ModuleSnippets.saveFragment();
|
||
|
});
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
clearBtn: function () {
|
||
|
$('.Clear').on('click', function (event) {
|
||
|
event.preventDefault();
|
||
|
$.alerts._overlay('show');
|
||
|
$('#fragmentKey').val('');
|
||
|
$('#fargmentNames option').prop('selected', false);
|
||
|
$('#fargmentNames option:first').prop('selected', true);
|
||
|
$("#code_text").text(ModuleSnippets.insertCodeMirror(''));
|
||
|
$('#fargmentNames').trigger('refresh');
|
||
|
$('a.Delete').addClass('hidden');
|
||
|
$.alerts._overlay('hide');
|
||
|
});
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
deleteBtn: function () {
|
||
|
$('.Delete').on('click', function (event) {
|
||
|
jConfirm(
|
||
|
del_confirm,
|
||
|
del_title,
|
||
|
function (succes) {
|
||
|
if (succes) {
|
||
|
$.alerts._overlay('show');
|
||
|
ModuleSnippets.deleteFragment();
|
||
|
}
|
||
|
}
|
||
|
);
|
||
|
});
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
deleteFragment: function () {
|
||
|
|
||
|
let key_fragment = $('#fragmentKey').val();
|
||
|
|
||
|
if (key_fragment != '') {
|
||
|
$.ajax({
|
||
|
type: "POST",
|
||
|
url: "index.php?do=modules&action=modedit&mod=snippets&moduleaction=delete&cp=" + session,
|
||
|
data: {
|
||
|
'fragment_key': key_fragment
|
||
|
},
|
||
|
dataType: "json",
|
||
|
beforeSend: function () {
|
||
|
$.alerts._overlay('show');
|
||
|
},
|
||
|
success: function(data) {
|
||
|
$.alerts._overlay('hide');
|
||
|
|
||
|
$.jGrowl(data['message'], {
|
||
|
header: data['header'],
|
||
|
theme: data['theme']
|
||
|
});
|
||
|
|
||
|
$('#fragmentKey').val('');
|
||
|
|
||
|
$("#code_text").text(ModuleSnippets.insertCodeMirror(''));
|
||
|
ModuleSnippets.showHideDeleteBtn();
|
||
|
ModuleSnippets.changeSelect(data['fragments']);
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
nameSelection: function () {
|
||
|
$('#fargmentNames').on('change', function (event) {
|
||
|
event.preventDefault();
|
||
|
|
||
|
let selected = $($(this), 'option:selected').val();
|
||
|
|
||
|
if (selected === '')
|
||
|
{
|
||
|
$('#fragmentKey').val('');
|
||
|
|
||
|
ModuleSnippets.showHideDeleteBtn();
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
$.ajax({
|
||
|
type: "POST",
|
||
|
url: "index.php?do=modules&action=modedit&mod=snippets&moduleaction=insert&cp=" + session,
|
||
|
data: {
|
||
|
'fragment_key': selected
|
||
|
},
|
||
|
dataType: "json",
|
||
|
beforeSend: function () {
|
||
|
$.alerts._overlay('show');
|
||
|
},
|
||
|
success: function (data) {
|
||
|
$.alerts._overlay('hide');
|
||
|
|
||
|
if (data.fragment_key == null)
|
||
|
{
|
||
|
$('#fragmentKey').val('');
|
||
|
|
||
|
ModuleSnippets.showHideDeleteBtn();
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
$.jGrowl(data['message'], {
|
||
|
header: data['header'],
|
||
|
theme: data['theme']
|
||
|
});
|
||
|
|
||
|
if (data['success'])
|
||
|
{
|
||
|
$("#code_text").text(ModuleSnippets.insertCodeMirror(data['fragment_code']));
|
||
|
$('#fragmentKey').val(data['fragment_key']);
|
||
|
}
|
||
|
// ToDo ELSE
|
||
|
|
||
|
ModuleSnippets.showHideDeleteBtn();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return false;
|
||
|
});
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
insertCodeMirror: function (data) {
|
||
|
var cm = $('.CodeMirror')[0].CodeMirror;
|
||
|
var doc = cm.getDoc();
|
||
|
var cursor = doc.getCursor();
|
||
|
var line = doc.getLine(cursor.line);
|
||
|
var pos = {
|
||
|
line: cursor.line,
|
||
|
ch: line.length - 1
|
||
|
};
|
||
|
|
||
|
doc.setValue('');
|
||
|
|
||
|
doc.replaceRange(data, pos);
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
showHideDeleteBtn: function () {
|
||
|
let key_fragment = $('#fragmentKey').val();
|
||
|
|
||
|
(key_fragment == '')
|
||
|
? $('a.Delete').addClass('hidden')
|
||
|
: $('a.Delete').removeClass('hidden');
|
||
|
|
||
|
return false;
|
||
|
},
|
||
|
|
||
|
|
||
|
//
|
||
|
changeSelect: function (data, key) {
|
||
|
let select = $('#fargmentNames');
|
||
|
|
||
|
select.html('');
|
||
|
|
||
|
select.append('<option value="">' + select_option + '</option>')
|
||
|
|
||
|
$.each(data, function () {
|
||
|
select.append('<option value="' + this.fragment_key + '">' + this.fragment_name + '</option>')
|
||
|
});
|
||
|
|
||
|
if (key !== undefined)
|
||
|
select.children('option[value=' + key + ']').prop('selected', true);
|
||
|
|
||
|
$('select').trigger('refresh');
|
||
|
}
|
||
|
};
|
||
|
|
||
|
jQuery(function ($) {
|
||
|
"use strict";
|
||
|
ModuleSnippets.init();
|
||
|
});
|