Geen omschrijving

manage.case.templates.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. function add_case_template() {
  2. let url = '/manage/case-templates/add/modal' + case_param();
  3. $('#modal_case_template_json').load(url, function (response, status, xhr) {
  4. if (status !== "success") {
  5. ajax_notify_error(xhr, url);
  6. return false;
  7. }
  8. let editor = ace.edit("editor_detail",
  9. {
  10. autoScrollEditorIntoView: true,
  11. minLines: 30,
  12. });
  13. editor.setTheme("ace/theme/tomorrow");
  14. editor.session.setMode("ace/mode/json");
  15. editor.renderer.setShowGutter(true);
  16. editor.setOption("showLineNumbers", true);
  17. editor.setOption("showPrintMargin", false);
  18. editor.setOption("displayIndentGuides", true);
  19. editor.setOption("maxLines", "Infinity");
  20. editor.session.setUseWrapMode(true);
  21. editor.setOption("indentedSoftWrap", true);
  22. editor.renderer.setScrollMargin(8, 5)
  23. editor.setOptions({
  24. enableBasicAutocompletion: [{
  25. getCompletions: (editor, session, pos, prefix, callback) => {
  26. callback(null, [
  27. {value: 'name', score: 1, meta: 'name of the template'},
  28. {value: 'display', score: 1, meta: 'display name of the template'},
  29. {value: 'description', score: 1, meta: 'description of the template'},
  30. {value: 'author', score: 1, meta: 'author of the template'},
  31. {value: 'title_prefix', score: 1, meta: 'prefix of instantiated cases'},
  32. {value: 'summary', score: 1, meta: 'summary of the case'},
  33. {value: 'tags', score: 1, meta: 'tags of the case or the tasks'},
  34. {value: 'tasks', score: 1, meta: 'tasks of the case'},
  35. {value: 'note_groups', score: 1, meta: 'groups of notes'},
  36. {value: 'title', score: 1, meta: 'title of the task or the note group or the note'},
  37. {value: 'content', score: 1, meta: 'content of the note'},
  38. ]);
  39. },
  40. }],
  41. enableLiveAutocompletion: true,
  42. enableSnippets: true
  43. });
  44. $('#submit_new_case_template').on("click", function () {
  45. let data_sent = Object();
  46. data_sent['case_template_json'] = editor.getSession().getValue();
  47. data_sent['csrf_token'] = $("#csrf_token").val();
  48. post_request_api('/manage/case-templates/add', JSON.stringify(data_sent), false, function() {
  49. window.swal({
  50. title: "Adding...",
  51. text: "Please wait",
  52. icon: "/static/assets/img/loader.gif",
  53. button: false,
  54. allowOutsideClick: false
  55. });
  56. })
  57. .done((data) => {
  58. if (notify_auto_api(data)) {
  59. refresh_case_template_table();
  60. $('#modal_case_template').modal('hide');
  61. }
  62. })
  63. .fail((error) => {
  64. let data = error.responseJSON;
  65. $('#submit_new_case_template').text('Save');
  66. $('#alert_case_template_edit').text(data.message);
  67. if (data.data && data.data.length > 0) {
  68. let output='<li>'+ sanitizeHTML(data.data) +'</li>';
  69. $('#case_template_err_details_list').append(output);
  70. $('#alert_case_template_details').show();
  71. }
  72. $('#alert_case_template_edit').show();
  73. })
  74. .always((data) => {
  75. window.swal.close();
  76. });
  77. return false;
  78. })
  79. });
  80. $('#modal_case_template').modal({ show: true });
  81. }
  82. $('#case_templates_table').dataTable( {
  83. "ajax": {
  84. "url": `/manage/case-templates/list${case_param()}`,
  85. "contentType": "application/json",
  86. "type": "GET",
  87. "data": function ( d ) {
  88. if (d.status == 'success') {
  89. return JSON.stringify( d.data );
  90. } else {
  91. return JSON.stringify([]);
  92. }
  93. }
  94. },
  95. "order": [[ 0, "desc" ]],
  96. "autoWidth": false,
  97. "columns": [
  98. {
  99. "data": "id",
  100. "render": function ( data, type, row ) {
  101. return '<a href="#" onclick="case_template_detail(\'' + row['id'] + '\');">' + sanitizeHTML(data) +'</a>';
  102. }
  103. },
  104. {
  105. "data": "display_name",
  106. "render": function ( data, type, row ) {
  107. return '<a href="#" onclick="case_template_detail(\'' + row['id'] + '\');">' + sanitizeHTML(data) +'</a>';
  108. }
  109. },
  110. {
  111. "data": "description"
  112. },
  113. {
  114. "data": "added_by"
  115. },
  116. {
  117. "data": "created_at"
  118. },
  119. {
  120. "data": "updated_at"
  121. }
  122. ]
  123. }
  124. );
  125. function refresh_case_template_table() {
  126. $('#case_templates_table').DataTable().ajax.reload();
  127. notify_success("Refreshed");
  128. }
  129. function delete_case_template(id) {
  130. swal({
  131. title: "Are you sure ?",
  132. text: "You won't be able to revert this !",
  133. icon: "warning",
  134. buttons: true,
  135. dangerMode: true,
  136. confirmButtonColor: '#3085d6',
  137. cancelButtonColor: '#d33',
  138. confirmButtonText: 'Yes, delete it!'
  139. })
  140. .then((willDelete) => {
  141. if (willDelete) {
  142. post_request_api(`/manage/case-templates/delete/${id}`)
  143. .done((data) => {
  144. if(notify_auto_api(data)) {
  145. window.location.href = '/manage/case-templates' + case_param();
  146. }
  147. });
  148. } else {
  149. swal("Pfew, that was close");
  150. }
  151. });
  152. }
  153. function case_template_detail(ctempl_id) {
  154. let url = '/manage/case-templates/' + ctempl_id + '/modal' + case_param();
  155. $('#modal_case_template_json').load(url, function (response, status, xhr) {
  156. if (status !== "success") {
  157. ajax_notify_error(xhr, url);
  158. return false;
  159. }
  160. let editor = ace.edit("editor_detail",
  161. {
  162. autoScrollEditorIntoView: true,
  163. minLines: 30,
  164. });
  165. editor.setTheme("ace/theme/tomorrow");
  166. editor.session.setMode("ace/mode/json");
  167. editor.renderer.setShowGutter(true);
  168. editor.setOption("showLineNumbers", true);
  169. editor.setOption("showPrintMargin", false);
  170. editor.setOption("displayIndentGuides", true);
  171. editor.setOption("maxLines", "Infinity");
  172. editor.session.setUseWrapMode(true);
  173. editor.setOption("indentedSoftWrap", true);
  174. editor.renderer.setScrollMargin(8, 5)
  175. editor.setOptions({
  176. enableBasicAutocompletion: [{
  177. getCompletions: (editor, session, pos, prefix, callback) => {
  178. callback(null, [
  179. {value: 'name', score: 1, meta: 'name of the template'},
  180. {value: 'display_name', score: 1, meta: 'display name of the template'},
  181. {value: 'description', score: 1, meta: 'description of the template'},
  182. {value: 'author', score: 1, meta: 'author of the template'},
  183. {value: 'title_prefix', score: 1, meta: 'prefix of instantiated cases'},
  184. {value: 'summary', score: 1, meta: 'summary of the case'},
  185. {value: 'tags', score: 1, meta: 'tags of the case or the tasks'},
  186. {value: 'tasks', score: 1, meta: 'tasks of the case'},
  187. {value: 'note_groups', score: 1, meta: 'groups of notes'},
  188. {value: 'title', score: 1, meta: 'title of the task or the note group or the note'},
  189. {value: 'content', score: 1, meta: 'content of the note'},
  190. ]);
  191. },
  192. }],
  193. enableLiveAutocompletion: true,
  194. enableSnippets: true
  195. });
  196. $('#submit_new_case_template').on("click", function () {
  197. update_case_template(ctempl_id, editor, false, false);
  198. });
  199. $('#submit_delete_case_template').on("click", function () {
  200. delete_case_template(ctempl_id);
  201. });
  202. });
  203. $('#modal_case_template').modal({ show: true });
  204. }
  205. function update_case_template(ctempl_id, editor, partial, complete){
  206. event.preventDefault();
  207. let data_sent = Object();
  208. data_sent['case_template_json'] = editor.getSession().getValue();
  209. data_sent['csrf_token'] = $("#csrf_token").val();
  210. $('#alert_case_template_edit').empty();
  211. $('#alert_case_template_details').hide();
  212. $('#case_template_err_details_list').empty();
  213. post_request_api(`/manage/case-templates/update/${ctempl_id}`, JSON.stringify(data_sent), false, function() {
  214. window.swal({
  215. title: "Updating...",
  216. text: "Please wait",
  217. icon: "/static/assets/img/loader.gif",
  218. button: false,
  219. allowOutsideClick: false
  220. });
  221. })
  222. .done((data) => {
  223. notify_auto_api(data);
  224. })
  225. .fail((error) => {
  226. let data = error.responseJSON;
  227. $('#submit_new_case_template').text('Update');
  228. $('#alert_case_template_edit').text(data.message);
  229. if (data.data && data.data.length > 0) {
  230. let output='<li>'+ sanitizeHTML(data.data) +'</li>';
  231. $('#case_template_err_details_list').append(output);
  232. $('#alert_case_template_details').show();
  233. }
  234. $('#alert_case_template_edit').show();
  235. })
  236. .always((data) => {
  237. window.swal.close();
  238. });
  239. return false;
  240. }
  241. function fire_upload_case_template() {
  242. let url = '/manage/case-templates/upload/modal' + case_param();
  243. $('#modal_upload_case_template_json').load(url, function (response, status, xhr) {
  244. if (status !== "success") {
  245. ajax_notify_error(xhr, url);
  246. return false;
  247. }
  248. });
  249. $('#modal_upload_case_template').modal({ show: true });
  250. }
  251. function upload_case_template() {
  252. if ($("#input_upload_case_template").val() !== "")
  253. {
  254. var file = $("#input_upload_case_template").get(0).files[0];
  255. var reader = new FileReader();
  256. reader.onload = function (e) {
  257. fileData = e.target.result
  258. var data = new Object();
  259. data['csrf_token'] = $('#csrf_token').val();
  260. data['case_template_json'] = fileData;
  261. post_request_api('/manage/case-templates/add', JSON.stringify(data), false, function() {
  262. window.swal({
  263. title: "Adding...",
  264. text: "Please wait",
  265. icon: "/static/assets/img/loader.gif",
  266. button: false,
  267. allowOutsideClick: false
  268. });
  269. })
  270. .done((data) => {
  271. notify_auto_api(data);
  272. jsdata = data;
  273. if (jsdata.status == "success") {
  274. refresh_case_template_table();
  275. $('#modal_upload_case_template').modal('hide');
  276. }
  277. })
  278. .fail((error) => {
  279. let data = error.responseJSON;
  280. $('#alert_upload_case_template').text(data.message);
  281. if (data.data && data.data.length > 0) {
  282. let output='<li>'+ sanitizeHTML(data.data) +'</li>';
  283. $('#upload_case_template_err_details_list').append(output);
  284. $('#alert_upload_case_template_details').show();
  285. }
  286. $('#alert_upload_case_template').show();
  287. })
  288. .always((data) => {
  289. $("#input_upload_case_template").val("");
  290. window.swal.close();
  291. });
  292. };
  293. reader.readAsText(file);
  294. }
  295. return false;
  296. }
  297. function downloadCaseTemplateDefinition() {
  298. event.preventDefault();
  299. let editor = ace.edit("editor_detail");
  300. let data = editor.getSession().getValue();
  301. let filename = "case_template.json";
  302. download_file(filename, 'text/json' , data);
  303. }