1 /** |
|
2 * MyAMS containers management |
|
3 */ |
|
4 (function($, globals) { |
|
5 |
|
6 var ams = globals.MyAMS; |
|
7 |
|
8 ams.container = { |
|
9 |
|
10 /** |
|
11 * Change container elements order |
|
12 * |
|
13 * This is a callback which may be used with TableDnD plug-in which allows you to |
|
14 * change order of table rows. |
|
15 * Rows order is stored in an hidden input which is defined in table's data attribute |
|
16 * called 'data-ams-input-name' |
|
17 */ |
|
18 changeOrder: function(table, names) { |
|
19 var input = $('input[name="' + $(this).data('ams-input-name') + '"]', $(this)); |
|
20 input.val(names.join(';')); |
|
21 }, |
|
22 |
|
23 /** |
|
24 * Delete an element from a container table |
|
25 * |
|
26 * @returns {Function} |
|
27 */ |
|
28 deleteElement: function() { |
|
29 return function() { |
|
30 var link = $(this); |
|
31 ams.skin && ams.skin.bigBox({ |
|
32 title: ams.i18n.WARNING, |
|
33 content: '<i class="text-danger fa fa-fw fa-bell"></i> ' + ams.i18n.DELETE_WARNING, |
|
34 status: 'info', |
|
35 buttons: ams.i18n.BTN_OK_CANCEL |
|
36 }, function(button) { |
|
37 if (button === ams.i18n.BTN_OK) { |
|
38 var tr = link.parents('tr').first(); |
|
39 var table = tr.parents('table').first(); |
|
40 var location = tr.data('ams-location') || table.data('ams-location') || ''; |
|
41 if (location) { |
|
42 location += '/'; |
|
43 } |
|
44 var deleteTarget = tr.data('ams-delete-target') || table.data('ams-delete-target') || 'delete-element.json'; |
|
45 var objectName = tr.data('ams-element-name'); |
|
46 ams.ajax && ams.ajax.post(location + deleteTarget, {'object_name': objectName}, function(result, status) { |
|
47 if (result.status === 'success') { |
|
48 if (table.hasClass('datatable')) { |
|
49 table.dataTable().fnDeleteRow(tr[0]); |
|
50 } else { |
|
51 tr.remove(); |
|
52 } |
|
53 if (result.handle_json) { |
|
54 ams.ajax.handleJSON(result); |
|
55 } |
|
56 } else { |
|
57 ams.ajax.handleJSON(result); |
|
58 } |
|
59 }); |
|
60 } |
|
61 }); |
|
62 }; |
|
63 }, |
|
64 |
|
65 /** |
|
66 * Switch element visibility |
|
67 */ |
|
68 switchElementVisibility: function() { |
|
69 return function() { |
|
70 var source = $(this); |
|
71 var cell = source.parents('td').first(); |
|
72 var row = source.parents('tr').first(); |
|
73 var table = row.parents('table'); |
|
74 $('i', source).attr('class', 'fa fa-fw fa-spinner fa-pulse'); |
|
75 ams.ajax && ams.ajax.post(table.data('ams-location') + '/' + |
|
76 (cell.data('ams-attribute-switcher') || table.data('ams-attribute-switcher')), |
|
77 {object_name: row.data('ams-element-name')}, |
|
78 function(result, status) { |
|
79 if (result.status === 'success') { |
|
80 if (result.visible) { |
|
81 $('i', source).attr('class', 'fa fa-fw fa-eye'); |
|
82 } else { |
|
83 $('i', source).attr('class', 'fa fa-fw fa-eye-slash text-danger'); |
|
84 } |
|
85 } else { |
|
86 ams.ajax.handleJSON(result); |
|
87 } |
|
88 }); |
|
89 } |
|
90 }, |
|
91 |
|
92 /** |
|
93 * Switch element attribute |
|
94 */ |
|
95 switchElementAttribute: function() { |
|
96 return function() { |
|
97 var source = $(this); |
|
98 var cell = source.parents('td').first(); |
|
99 var attribute = cell.data('ams-switcher-attribute-name'); |
|
100 var row = source.parents('tr').first(); |
|
101 var table = row.parents('table'); |
|
102 $('i', source).attr('class', 'fa fa-fw fa-spinner fa-pulse'); |
|
103 ams.ajax && ams.ajax.post(table.data('ams-location') + '/' + |
|
104 (cell.data('ams-attribute-switcher') || table.data('ams-attribute-switcher')), |
|
105 {object_name: row.data('ams-element-name')}, |
|
106 function(result, status) { |
|
107 if (result.status === 'success') { |
|
108 if (result[attribute] || result['on']) { |
|
109 $('i', source).attr('class', table.data('ams-' + attribute + '-icon-on') || 'fa fa-fw fa-check-square-o'); |
|
110 } else { |
|
111 $('i', source).attr('class', table.data('ams-' + attribute + '-icon-off') || 'fa fa-fw fa-check-square txt-color-silver opacity-75'); |
|
112 } |
|
113 } else { |
|
114 ams.ajax.handleJSON(result); |
|
115 } |
|
116 }); |
|
117 } |
|
118 }, |
|
119 |
|
120 /** |
|
121 * Export table to CSV |
|
122 */ |
|
123 exportTableToTSV: function() { |
|
124 return function() { |
|
125 var source = $(this); |
|
126 ams.ajax && ams.ajax.check(globals.saveAs, |
|
127 ams.baseURL + 'ext/js-filesaver' + ams.devext + '.js', |
|
128 function() { |
|
129 var table = $('table.datatable', source.parents('.ams-widget:first')); |
|
130 var datatable = table.dataTable(); |
|
131 var output = ''; |
|
132 $('th', table).each(function(index) { |
|
133 if (index > 0) { |
|
134 output += '\t'; |
|
135 } |
|
136 output += $(this).text(); |
|
137 }); |
|
138 output += '\n'; |
|
139 $(datatable.fnGetData()).each(function() { |
|
140 $(this).each(function(index) { |
|
141 if (index > 0) { |
|
142 output += '\t'; |
|
143 } |
|
144 output += this; |
|
145 }); |
|
146 output += '\n'; |
|
147 }); |
|
148 var blob = new Blob([output], {type: 'text/tsv; charset=utf-8'}); |
|
149 saveAs(blob, "export.tsv"); |
|
150 }); |
|
151 } |
|
152 } |
|
153 }; |
|
154 |
|
155 })(jQuery, this); |
|