epic-s6ts
[kivitendo-erp.git] / js / kivi.Materialize.js
1 namespace("kivi.Materialize", function(ns) {
2   "use strict";
3
4   ns.init = function() {
5     ns.reinit_widgets();
6   };
7
8   ns.build_i18n = function() {
9     return {
10       months: [
11         kivi.t8('January'),
12         kivi.t8('February'),
13         kivi.t8('March'),
14         kivi.t8('April'),
15         kivi.t8('May'),
16         kivi.t8('June'),
17         kivi.t8('July'),
18         kivi.t8('August'),
19         kivi.t8('September'),
20         kivi.t8('October'),
21         kivi.t8('November'),
22         kivi.t8('December')
23       ],
24       monthsShort: [
25         kivi.t8('Jan'),
26         kivi.t8('Feb'),
27         kivi.t8('Mar'),
28         kivi.t8('Apr'),
29         kivi.t8('May'),
30         kivi.t8('Jun'),
31         kivi.t8('Jul'),
32         kivi.t8('Aug'),
33         kivi.t8('Sep'),
34         kivi.t8('Oct'),
35         kivi.t8('Nov'),
36         kivi.t8('Dec')
37       ],
38       weekdays: [
39         kivi.t8('Sunday'),
40         kivi.t8('Monday'),
41         kivi.t8('Tuesday'),
42         kivi.t8('Wednesday'),
43         kivi.t8('Thursday'),
44         kivi.t8('Friday'),
45         kivi.t8('Saturday')
46       ],
47       weekdaysShort: [
48         kivi.t8('Sun'),
49         kivi.t8('Mon'),
50         kivi.t8('Tue'),
51         kivi.t8('Wed'),
52         kivi.t8('Thu'),
53         kivi.t8('Fri'),
54         kivi.t8('Sat')
55       ],
56
57       // Buttons
58       today: kivi.t8('Today'),
59       done: kivi.t8('Ok'),
60       clear: kivi.t8('Clear'),
61       cancel: kivi.t8('Cancel'),
62
63       // Accessibility labels
64       labelMonthNext: kivi.t8('Next month'),
65       labelMonthPrev: kivi.t8('Previous month')
66     };
67   };
68
69   ns.flash = function(text) {
70     M.toast({html: text});
71   };
72
73   ns.reinit_widgets = function() {
74     $('.sidenav').sidenav();
75     $('select').formSelect();
76     $('.datepicker').datepicker({
77       firstDay: 1,
78       format: kivi.myconfig.dateformat,
79       showClearBtn: true,
80       i18n: ns.build_i18n()
81     });
82     $('.modal').modal();
83     $('.materialboxed').materialbox();
84     M.updateTextFields();
85   };
86
87   // alternative for kivi.popup_dialog.
88   // opens materialize modal instead.
89   //
90   // differences: M.modal can not load external content, so it needs to be fetched manually and inserted into the DOM.
91   ns.popup_dialog = function(params) {
92     params            = params        || { };
93     let id            = params.id     || 'jqueryui_popup_dialog';
94     let $div;
95     let custom_close  = params.dialog ? params.dialog.close : undefined;
96     let dialog_params = $.extend(
97       { // kivitendo default parameters.
98         // unlike classic layout, there is not fixed size, and M.modal is always... modal
99         onCloseStart: custom_close
100       },
101       // User supplied options:
102       params.dialog || { },
103       { // Options that must not be changed:
104         // close options already work
105       });
106
107     if (params.url) {
108       $.ajax({
109         url: params.url,
110         data: params.data,
111         success: function(data) {
112           params.html = data;
113           params.url = undefined;
114           params.data = undefined;
115           ns.popup_dialog(params);
116         },
117         error: function(x, status, error) { console.error(error); },
118         dataType: 'text',
119       });
120       return 1;
121     }
122
123     if (params.html) {
124       $div = $('<div>');
125       $div.attr('id', id);
126       $div.addClass("modal");
127       let $modal_content = $('<div>');
128       $modal_content.addClass('modal-content');
129       $modal_content.html(params.html);
130       $div.append($modal_content);
131       $('body').append($div);
132       kivi.reinit_widgets();
133       dialog_params.onCloseEnd = function() { $div.remove(); };
134
135       $div.modal(dialog_params);
136     } else if(params.id) {
137       $div = $('#' + params.id);
138     } else {
139       console.error("insufficient parameters to open dialog");
140       return 0;
141     }
142
143     $div.modal('open');
144
145     return true;
146   };
147
148   /**
149    * upload file to local storage for later sync
150    *
151    * should be used with P.M.file_upload(..., local=>1)
152    */
153   ns.LocalFileUpload = function(options) {
154     this.storage_token = options.storage_token; // used in localstorage to retrieve the file
155     this.dom_selector  = options.dom_selector;  // file inputs to listen on
156
157     this.init();
158   };
159
160   ns.LocalFileUpload.prototype = {
161     init: function() {
162       $(this.dom_selector).change(this.handle_file_upload);
163     },
164     handle_file_upload: function() {
165
166     },
167     load_files: function() {
168       return JSON.parse(localStorage.getImte(this.storage_token));
169     },
170     save_files: function() {
171       return JSON.parse(localStorage.getImte(this.storage_token));
172     },
173
174   };
175
176 });