-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessRockFinder2.js
147 lines (129 loc) · 3.76 KB
/
ProcessRockFinder2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var grid;
$(document).ready(function() {
// get current finder name
var $field = $('#wrap_Inputfield_code');
var $debug = $('#debuginfo');
var name = $field.data('name');
var hasTabulator = $('.RockTabulatorWrapper').length;
// load code from browser, but only when no name is set
// this makes sure that if a name is set, the code is always
// the same as in the corresponding file
var code;
if(!name) code = localStorage.getItem('RockFinder2_code');
// get ace editor
var editor;
// setup spinner
var $spinner = $('<i class="fa fa-spin fa-spinner" style="margin-left: 10px;"></i>');
// get code from textarea/ace
var getCode = function() {
if(typeof ace != "undefined") {
code = ace.edit('InputfieldAceExtended_code_editor').getSession().getValue();
}
else {
code = $('#Inputfield_code').val();
}
return code;
}
// ajax function
var sendAJAX = function() {
$spinner.hide().appendTo($field.closest('.Inputfield').find('>.InputfieldHeader')).fadeIn();
// save code to localStorage
if(!name) localStorage.setItem('RockFinder2_code', code);
var $tabulator = $('.RockTabulatorWrapper');
$tabulator.find('.loading').fadeIn();
// get data and log it to console
$.post(RockFinder2.conf.url, {
code: getCode(),
type: 'debug',
}).done(function(data) {
// check for errors
if(data.error) {
alert(data.error);
$debug.find('.tracy-inner').fadeOut();
return;
}
// update div
$debug.fadeOut(function() {
$debug.html(data.html);
// unfold tracy dump object
$debug.find('.tracy-dump-object').click();
}).fadeIn();
// update tabulator
if(hasTabulator) {
grid = RockTabulator.getGrid("rockfinder2_sandbox");
if(!grid.table) grid.initTable({data:data.finder.data});
else grid.table.setData(data.finder.data);
$tabulator.find('.loading').fadeOut();
}
}).fail(function(data) {
alert('Rquest failed, see console');
console.error(data);
}).always(function() {
$spinner.fadeOut();
});
}
// save code to file
var saveFile = function() {
$.post("#", {
code: getCode(),
action: 'save',
}).done(function(data) {
UIkit.notification({
message: data,
});
}).fail(function(data) {
alert('Rquest failed: ' + data);
});
}
// onload
$(window).load(function() {
// early exit on overview page
if(!$('#sandboxform').length) return;
if(typeof ace != 'undefined') {
editor = ace.edit('InputfieldAceExtended_code_editor');
}
// set code from localstorage
if(code) {
editor.setValue(code, 1);
}
// fire ajax request on first load
sendAJAX();
});
// keyboard shortcuts
$('#wrap_Inputfield_code').keydown(function (event) {
if (event.ctrlKey || event.metaKey || event.altKey) {
switch (event.keyCode) {
// ctrl/alt + enter
case 13:
event.preventDefault();
sendAJAX();
return false;
}
}
});
$(window).keydown(function (event) {
if (event.ctrlKey || event.metaKey || event.altKey) {
switch (event.keyCode) {
// ctrl/alt + s
case 83:
event.preventDefault();
saveFile();
return false;
}
}
});
// confirm deletion of finders
$(document).on('click', '.delFinder', function(event) {
var $link = $(event.target).closest('a');
var name = $link.data('name');
ProcessWire.confirm('Are you sure you want to delete Finder ' + name + ' ?', function() {
// yes, do not abort
window.location.href = $link.attr('href');
},
function() {
// on abort
return false;
});
return false;
});
});