if (!String.prototype.includes) { String.prototype.includes = function () { 'use strict'; return String.prototype.indexOf.apply(this, arguments) !== -1; }; } //extend FileReader if (!FileReader.prototype.readAsBinaryString) { FileReader.prototype.readAsBinaryString = function (fileData) { var binary = ""; var pt = this; var reader = new FileReader(); reader.onload = function (e) { var bytes = new Uint8Array(reader.result); var length = bytes.byteLength; for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]); } //pt.result - readonly so assign binary pt.content = binary; $(pt).trigger('onload'); } reader.readAsArrayBuffer(file); } } document.addEventListener('DOMContentLoaded', function() { M.AutoInit(); }); /* $(document).ready(function () { $('.modal').modal(); $('.tabs').tabs(); $('.fixed-action-btn').floatingActionButton(); }); */ //localStorage persistence var SHIFT_STORAGE_KEY = "dienstplan_chrosey"; var RULE_STORAGE_KEY = "regeln_chrosey"; var shiftStorage = { fetch: function () { 'use strict'; var parsed = JSON.parse(localStorage.getItem(SHIFT_STORAGE_KEY) || '[]'), shifts = []; parsed.forEach(function (el, index) { var shift = Shift.thaw(el); shift.id = index; shifts.push(shift); }); shiftStorage.uid = shifts.length; return shifts; }, save: function (shifts) { 'use strict'; var json = JSON.stringify(shifts) localStorage.setItem(SHIFT_STORAGE_KEY, json); }, count: function () { return JSON.parse(localStorage.getItem(SHIFT_STORAGE_KEY) || '[]').length; } }; var ruleStorage = { fetch: function () { 'use strict'; var rules = JSON.parse(localStorage.getItem(RULE_STORAGE_KEY)) || Rule.defaults(); ruleStorage.uid = rules.length; return rules; }, save: function (rules) { 'use strict'; var json = JSON.stringify(rules); localStorage.setItem(RULE_STORAGE_KEY, json); } }; var app = new Vue({ el: '#app', data: { shifts: shiftStorage.fetch(), rules: ruleStorage.fetch(), icsFile: null, blob: null, dp_sheet: '', deletedShift: '', remaining: shiftStorage.count(), selectedShift: '', selectedRule: '', saveto: 'dienstplan.ics', uploadFileName: "", config: { moment: { parse_formats: [ "ddd, DD/ MMM. 'YY HH:mm", "ddd, DD/ MMM. YYYY HH:mm" ], parse_language: 'en', display_language: 'de' }, toast_length: 3000 } }, watch: { shifts: { handler: function (shifts) { 'use strict'; shiftStorage.save(shifts); this.remaining = shifts.length; M.toast({html:"Änderungen gespeichert.", displayLength:this.config.toast_length}); }, deep: true }, rules: { handler: function (rules) { 'use strict'; ruleStorage.save(rules); M.toast({html:"Änderungen gespeichert.", displayLength:this.config.toast_length}); }, deep: true } }, methods: { onFileChange: function (event) { var files = event.target.files || event.dataTransfer.files; this.handleInputFile(files[0]); this.uploadFileName = files[0].name + " [" + Math.round(files[0].size / 1024) + " kB]"; M.toast({html:this.uploadFileName + " ausgewählt", displayLength:this.config.toast_length}); }, handleInputFile: function (file) { var reader = new FileReader(); var vm = this; reader.onload = (e) => { var data = !e ? reader.content : e.target.result; var workbook = XLSX.read(data, { type: 'binary', cellDates: true, }); if(workbook.Sheets["Dienstplan"] == null){ file.status = "error"; } else { vm.parseWorksheet(workbook.Sheets["Dienstplan"]); file.status = "success"; } }; reader.readAsBinaryString(file); M.toast({html:this.uploadFileName + " wird eingelesen",displayLength: this.config.toast_length}); }, parseWorksheet: function (dp) { var arr = XLSX.utils.sheet_to_row_object_array(dp, { range: 1 }); var vm = this; var day; arr.forEach(element => { moment.locale(vm.config.moment.parse_language); if (element.hasOwnProperty('Datum')) { day = moment(element.Datum); } if (element.hasOwnProperty('Dienst')) { var time = moment(element.Zeit); var date = day.hour(time.hour()).minute(time.minute()); var bemerkung = !element.Bemerkung ? "" : element.Bemerkung.trim(); var art = !element.Dienst ? "" : element.Dienst.trim(); var name = !element.__EMPTY ? "" : element.__EMPTY.trim(); console.log({day, element}); vm.addShift(new Shift(art, name, date, bemerkung)); } }); }, addShift: function (shift) { this.shifts.push(shift); }, removeShift: function (shift) { this.shifts.splice(this.shifts.indexOf(shift), 1); M.toast({html:shift.VEventTitle + " gelöscht",displayLength: this.config.toast_length}); }, cleanStorage: function () { this.shifts = []; M.toast({html:"Alle Einträge gelöscht",displayLength: this.config.toast_length}); }, createDownloadFile: function () { var vCal = new VCalendar("Dienstplan Kalender"); this.shifts.forEach(function (shift) { vCal.add(shift.toVEvent()); }); var calString = vCal.toString(); this.blob = new Blob([calString], { type: 'text/plain' }); if (this.icsFile !== null) { window.URL.revokeObjectURL(this.icsFile); } this.icsFile = window.URL.createObjectURL(this.blob); M.toast({html:this.saveto + " erstellt.",displayLength: this.config.toast_length}); }, downloadFile: function () { if (window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(this.blob, this.saveto); } }, selectShift: function (shift) { this.selectedShift = Shift.thaw(shift); this.keepShift = shift; M.updateTextFields(); $('#shiftModal').modal('open'); }, saveChanges: function (changedShift) { this.shifts.splice(this.shifts.indexOf(this.keepShift), 1, changedShift); $('#shiftModal').modal('close'); this.keepShift = ''; this.selectedShift = ''; }, discardChanges: function (changedShift) { $('#shiftModal').modal('close'); this.keepShift = ''; this.selectedShift = ''; }, selectRule: function (rule) { this.selectedRule = new Rule(rule.duration, rule.art, rule.title); this.keepRule = rule; $('#rule_arten').chips({ data: this.selectedRule.Arten }); $('#rule_titel').chips({ data: this.selectedRule.Titel }); $('#ruleModal').modal('open'); }, saveRule: function (changedRule) { changedRule.Arten = $('#rule_arten').chips('data'); changedRule.Titel = $('#rule_titel').chips('data'); this.rules.splice(this.rules.indexOf(this.keepRule), 1, changedRule); $('#ruleModal').modal('close'); this.keepRule = ''; this.selectedRule = ''; }, discardRule: function (changedRule) { this.rules.splice(this.rules.indexOf(this.keepRule), 1, this.keepRule); $('#ruleModal').modal('close'); this.keepRule = ''; this.selectedRule = ''; } }, directives: { 'edit-focus': function (el, value) { if (value) { el.focus(); } } } })