var DATE_LOCALE_FORMAT = "D.M.YY"; //10.2.16 var DATE_INPUT_FORMAT = "YYYY-MM-DD"; //2016-02-10 var TIME_FORMAT = "HH:mm"; // 17:30 var WEEKDAY_FORMAT = "dddd"; //Monday var MOMENT_LOCALE = "de"; var TIMEZONE_NAME = "Europe/Berlin"; //requires moment.js Array.prototype.asChipData = function () { return this.map((e, i) => { return { tag: e, id: i }; }) } Array.prototype.fromChipData = function () { return this.map(e => { return e.tag; }) } function Rule() { var options = {}; if (arguments[0]) options = arguments[0]; var default_args = { 'arten': [], 'dauer': 60, 'titel': [], 'name': "Standard" } for (var index in default_args) { if (typeof options[index] == "undefined") options[index] = default_args[index]; } this._duration = options.dauer; this._arten = options.arten; this._titel = options.titel; this._name = options.name; } Rule.prototype = { get Dauer() { return this._duration; }, set Dauer(value) { this._duration = value; }, get Arten() { return this._arten.asChipData(); }, set Arten(value) { this._arten = value.fromChipData(); }, get Titel() { return this._titel.asChipData(); }, set Titel(value) { this._titel = value.fromChipData(); }, get Name() { return this._name; }, set Name(value) { this._name = value; }, }; Rule.prototype.fits = function (art, title) { var artMatch = false; var nameMatch = false; if (this._arten.length == 0) artMatch = true; else { this._arten.forEach(function (el, i) { if (art.includes(el.toLowerCase())) artMatch = true; }); } if (this._titel.length == 0) nameMatch = true; else { this._titel.forEach(function (el, i) { if (name.includes(el.toLowerCase())) nameMatch = true; }); } return artMatch && nameMatch; } Rule.thaw = function (json) { return new Rule({ dauer: json._duration, name: json._name, arten: json._arten, titel: json._titel }); } Rule.defaults = function () { var rules = []; var input = [{ name: "EF #01", dauer: 60, arten: ['VS'], titel: ['Kinderkonzert'] }, { name: "EF #02", dauer: 120, arten: ['VS'], titel: ["Expeditionskonzert", "Sinfoniekonzert"] }, { name: "EF #03", dauer: 150, arten: ['Oa', 'GP'], titel: ['Expeditionskonzert'] }, { name: "EF #04", dauer: 60, arten: ['Oa', 'GP'], titel: ['Expeditionskonzert'] }, { name: "EF #05", dauer: 150, arten: ['Oa', 'OSP'] }, { name: "EF #06", dauer: 180, arten: ["VS", "BO", "OHP", "HP", "GP", "Prem", "WA"] }, { name: "Standard", dauer: 60 } ]; input.forEach(e => { rules.push(new Rule(e)); }); return rules; } var DURATION_RULES = Rule.defaults(); function Shift() { var options = {}; if (arguments[0]) options = arguments[0]; var default_args = { 'art': "", 'name': "DUMMY", 'datum': moment(), 'beschreibung': "", 'ort': "", } for (var index in default_args) { if (typeof options[index] == "undefined") options[index] = default_args[index]; } this.Datum = options.datum.format(DATE_INPUT_FORMAT); this.Beginn = options.datum.format(TIME_FORMAT); this.Art = options.art; this.Beschreibung = options.beschreibung; this.Name = options.name; this.Ort = options.ort; if (typeof options.ende != "undefined") { this.Ende = options.ende; } else { if (!options.dontSetDurationFromRules) { Shift.setDurationFromRules(this, DURATION_RULES); } } } Shift.prototype = { get Wochentag() { return this._date.clone().locale(MOMENT_LOCALE).format(WEEKDAY_FORMAT); }, set Wochentag(value) { throw "kann nicht gesetzt werden."; }, get FormattedDatum() { return this._date.clone().format(DATE_LOCALE_FORMAT); }, set FormattedDatum(value) { var dateMoment = moment(value, DATE_LOCALE_FORMAT); this._date = dateMoment; }, get Datum() { return this._date.clone().format(DATE_INPUT_FORMAT); }, set Datum(value) { var dateMoment = moment(value).startOf('day'); this._date = dateMoment.clone(); }, get Beginn() { return this._begin.clone().format(TIME_FORMAT); }, set Beginn(value) { var dateMoment = moment(this.Datum + " " + value, DATE_INPUT_FORMAT + " " + TIME_FORMAT); this._begin = dateMoment.clone(); }, get Ende() { var ende = this._begin.clone().add(this._duration).format(TIME_FORMAT); return ende; }, set Ende(value) { var dateMoment = moment(this.Datum + " " + value, DATE_INPUT_FORMAT + " " + TIME_FORMAT); if (this._begin > dateMoment) { dateMoment.add(1, 'd'); } this._duration = moment.duration(dateMoment.diff(this._begin)); }, get Dauer() { return this._duration; }, set Dauer(duration) { this._duration = duration; }, get Name() { return this._name; }, set Name(value) { this._name = value ? value.trim() : ""; }, get Ort() { return this._ort; }, set Ort(value) { this._ort = value ? value.trim() : ""; }, get VEventTitle() { return this.Name + " " + this.Art; }, set VEventTitle(value) { throw "kann nicht gesetzt werden."; }, get Art() { return this._kind; }, set Art(value) { this._kind = value ? value.trim() : ""; }, get Beschreibung() { return this._description; }, set Beschreibung(value) { this._description = value ? value.trim() : ""; } } Shift.setDurationFromRules = function (shift, rules) { 'use strict'; var isAllDayEvent = shift.Beginn == "00:00"; if (isAllDayEvent) { shift.Dauer = moment.duration(24, 'h').locale(MOMENT_LOCALE); return; } var art = shift.Art.toLowerCase(); var titel = shift.Name.toLowerCase(); var duration = 60; for (var rIndex in rules) { var rule = rules[rIndex]; if (rule.fits(art, titel)) { duration = rule.Dauer; break; } } shift.Dauer = moment.duration(duration, 'm').locale(MOMENT_LOCALE); } Shift.prototype.toVEvent = function () { var end = this._begin.clone().add(this._duration); return new VEvent({ startMoment: this._begin, endMoment: end, title: this.VEventTitle, description: this.Beschreibung, location: this.Ort }); }; Shift.thaw = function (jsonShift) { moment.locale(MOMENT_LOCALE); var begin = moment(jsonShift._begin); var shift = new Shift({ art: jsonShift._kind, name: jsonShift._name, datum: begin, beschreibung: jsonShift._description, ort: jsonShift._ort, }); shift.id = jsonShift.id; shift.Dauer = moment.duration(jsonShift._duration); return shift; }; Shift.prototype.updateBeginn = function(hour) { this._begin = this._begin.add('hours', hour); }