epic-s6ts
[kivitendo-erp.git] / js / kivi.RecordTemplate.js
1 namespace('kivi.RecordTemplate', function(ns) {
2   'use strict';
3
4   ns.popup = function(template_type) {
5     $.get('controller.pl', {
6       action:        'RecordTemplate/show_dialog.js',
7       template_type: template_type,
8     }, kivi.eval_json_result);
9   };
10
11   ns.create = function() {
12     var new_name = $("#record_template_dialog_new_template_name").val();
13     if (new_name === '') {
14       alert(kivi.t8('Error: Name missing'));
15       return false;
16     }
17
18     kivi.RecordTemplate.save(undefined, new_name);
19   };
20
21   ns.save = function(id, name) {
22     var $type = $("#record_template_dialog_template_type");
23     var $form = $($type.data('form_selector'));
24
25     if (!$form) {
26       console.log("nothing found for form_selector " + $type.data("form_selector"));
27       return false;
28     }
29
30     if ((id !== undefined) && !confirm(kivi.t8('Are you sure you want to update the selected record template with the current values? This cannot be undone.')))
31       return false;
32
33     var data = $form.serializeArray().filter(function(val) { return val.name !== 'action'; });
34     data.push({ name: 'action',                            value: $type.data('save_action') });
35     data.push({ name: 'record_template_id',                value: id });
36     data.push({ name: 'record_template_new_template_name', value: name });
37
38     $.post($type.data('controller'), data, kivi.eval_json_result);
39   };
40
41   ns.load = function(id) {
42     var $type = $("#record_template_dialog_template_type");
43     var url   = encodeURIComponent($type.data('controller'))
44               + '?action=' + encodeURIComponent($type.data('load_action'))
45               + '&id='     + encodeURIComponent(id);
46
47     console.log(url);
48
49     window.location = url;
50   };
51
52   ns.rename = function(id) {
53     var current_name = $("#record_template_dialog_template_name_" + id).val();
54     var new_name     = prompt(kivi.t8("Please enter the new name:"), current_name);
55
56     if ((new_name === current_name) || !new_name || (new_name === ''))
57       return;
58
59     $.post('controller.pl', {
60       action: 'RecordTemplate/rename.js',
61       id: id,
62       template_name: new_name
63     }, kivi.eval_json_result);
64   };
65
66   ns.delete = function(id) {
67     if (!confirm(kivi.t8('Do you really want to delete this record template?')))
68       return;
69
70     $.post('controller.pl', {
71       action: 'RecordTemplate/delete.js',
72       id: id
73     }, kivi.eval_json_result);
74   };
75
76   ns.filter_templates = function() {
77     $.post('controller.pl', {
78       action: 'RecordTemplate/filter_templates',
79       template_filter: $("#template_filter").val(),
80       template_type: $("#record_template_dialog_template_type").val(),
81     }, kivi.eval_json_result);
82   };
83 });