easyappointments/assets/js/utils/working_plan.min.js

1 line
14 KiB
JavaScript
Executable File

"use strict";function _typeof(o){"@babel/helpers - typeof";return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(o){return typeof o}:function(o){return o&&"function"==typeof Symbol&&o.constructor===Symbol&&o!==Symbol.prototype?"symbol":typeof o},_typeof(o)}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor))throw new TypeError("Cannot call a class as a function")}function _defineProperties(target,props){for(var descriptor,i=0;i<props.length;i++)descriptor=props[i],descriptor.enumerable=descriptor.enumerable||!1,descriptor.configurable=!0,"value"in descriptor&&(descriptor.writable=!0),Object.defineProperty(target,_toPropertyKey(descriptor.key),descriptor)}function _createClass(Constructor,protoProps,staticProps){return protoProps&&_defineProperties(Constructor.prototype,protoProps),staticProps&&_defineProperties(Constructor,staticProps),Object.defineProperty(Constructor,"prototype",{writable:!1}),Constructor}function _defineProperty(obj,key,value){return key=_toPropertyKey(key),key in obj?Object.defineProperty(obj,key,{value:value,enumerable:!0,configurable:!0,writable:!0}):obj[key]=value,obj}function _toPropertyKey(t){var i=_toPrimitive(t,"string");return"symbol"==_typeof(i)?i:i+""}function _toPrimitive(t,r){if("object"!=_typeof(t)||!t)return t;var e=t[Symbol.toPrimitive];if(void 0!==e){var i=e.call(t,r||"default");if("object"!=_typeof(i))return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}App.Utils.WorkingPlan=function(){var moment=window.moment,WorkingPlan=function(){function WorkingPlan(){_classCallCheck(this,WorkingPlan),_defineProperty(this,"enableCancel",!1),_defineProperty(this,"enableSubmit",!1)}return _createClass(WorkingPlan,[{key:"setup",value:function setup(workingPlan){var weekDayId=App.Utils.Date.getWeekdayId(vars("first_weekday")),workingPlanSorted=App.Utils.Date.sortWeekDictionary(workingPlan,weekDayId);$(".working-plan tbody").empty(),$(".breaks tbody").empty();var timeFormat="regular"===vars("time_format")?"h:mm a":"HH:mm";$.each(workingPlanSorted,function(index,workingDay){var day=this.convertValueToDay(index),dayDisplayName=App.Utils.String.upperCaseFirstLetter(day);$("<tr/>",{html:[$("<td/>",{html:[$("<div/>",{class:"checkbox form-check",html:[$("<input/>",{class:"form-check-input",type:"checkbox",id:index}),$("<label/>",{class:"form-check-label",text:dayDisplayName,for:index})]})]}),$("<td/>",{html:[$("<input/>",{id:index+"-start",class:"work-start form-control form-control-sm"})]}),$("<td/>",{html:[$("<input/>",{id:index+"-end",class:"work-end form-control form-control-sm"})]})]}).appendTo(".working-plan tbody"),workingDay?($("#"+index).prop("checked",!0),$("#"+index+"-start").val(moment(workingDay.start,"HH:mm").format(timeFormat).toLowerCase()),$("#"+index+"-end").val(moment(workingDay.end,"HH:mm").format(timeFormat).toLowerCase()),workingDay.breaks.sort(function(break1,break2){return break1.start.localeCompare(break2.start)}),workingDay.breaks.forEach(function(workingDayBreak){$("<tr/>",{html:[$("<td/>",{class:"break-day editable",text:dayDisplayName}),$("<td/>",{class:"break-start editable",text:moment(workingDayBreak.start,"HH:mm").format(timeFormat).toLowerCase()}),$("<td/>",{class:"break-end editable",text:moment(workingDayBreak.end,"HH:mm").format(timeFormat).toLowerCase()}),$("<td/>",{html:[$("<button/>",{type:"button",class:"btn btn-outline-secondary btn-sm edit-break",title:lang("edit"),html:[$("<span/>",{class:"fas fa-edit"})]}),$("<button/>",{type:"button",class:"btn btn-outline-secondary btn-sm delete-break",title:lang("delete"),html:[$("<span/>",{class:"fas fa-trash-alt"})]}),$("<button/>",{type:"button",class:"btn btn-outline-secondary btn-sm save-break d-none",title:lang("save"),html:[$("<span/>",{class:"fas fa-check-circle"})]}),$("<button/>",{type:"button",class:"btn btn-outline-secondary btn-sm cancel-break d-none",title:lang("cancel"),html:[$("<span/>",{class:"fas fa-ban"})]})]})]}).appendTo(".breaks tbody")})):($("#"+index).prop("checked",!1),$("#"+index+"-start").prop("disabled",!0),$("#"+index+"-end").prop("disabled",!0))}.bind(this)),this.editableDayCell($(".breaks .break-day")),this.editableTimeCell($(".breaks").find(".break-start, .break-end"))}},{key:"setupWorkingPlanExceptions",value:function setupWorkingPlanExceptions(workingPlanExceptions){for(var date in workingPlanExceptions){var workingPlanException=workingPlanExceptions[date];this.renderWorkingPlanExceptionRow(date,workingPlanException).appendTo(".working-plan-exceptions tbody")}}},{key:"editableDayCell",value:function editableDayCell($selector){var weekDays={};weekDays[lang("sunday")]=lang("sunday"),weekDays[lang("monday")]=lang("monday"),weekDays[lang("tuesday")]=lang("tuesday"),weekDays[lang("wednesday")]=lang("wednesday"),weekDays[lang("thursday")]=lang("thursday"),weekDays[lang("friday")]=lang("friday"),weekDays[lang("saturday")]=lang("saturday"),$selector.editable(function(value){return value},{type:"select",data:weekDays,event:"edit",width:"100px",height:"30px",submit:"<button type=\"button\" class=\"d-none submit-editable\">Submit</button>",cancel:"<button type=\"button\" class=\"d-none cancel-editable\">Cancel</button>",onblur:"ignore",onreset:function(){if(!this.enableCancel)return!1}.bind(this),onsubmit:function(){if(!this.enableSubmit)return!1}.bind(this)})}},{key:"editableTimeCell",value:function editableTimeCell($selector){$selector.editable(function(value){return value},{event:"edit",width:"100px",height:"30px",submit:$("<button/>",{type:"button",class:"d-none submit-editable",text:lang("save")}).get(0).outerHTML,cancel:$("<button/>",{type:"button",class:"d-none cancel-editable",text:lang("cancel")}).get(0).outerHTML,onblur:"ignore",onreset:function(){if(!this.enableCancel)return!1}.bind(this),onsubmit:function(){if(!this.enableSubmit)return!1}.bind(this)})}},{key:"renderWorkingPlanExceptionRow",value:function renderWorkingPlanExceptionRow(date,workingPlanException){var timeFormat="regular"===vars("time_format")?"h:mm a":"HH:mm",start=null===workingPlanException||void 0===workingPlanException?void 0:workingPlanException.start,end=null===workingPlanException||void 0===workingPlanException?void 0:workingPlanException.end;return $("<tr/>",{data:{date:date,workingPlanException:workingPlanException},html:[$("<td/>",{class:"working-plan-exception-date",text:App.Utils.Date.format(date,vars("date_format"),vars("time_format"),!1)}),$("<td/>",{class:"working-plan-exception--start",text:start?moment(start,"HH:mm").format(timeFormat).toLowerCase():"-"}),$("<td/>",{class:"working-plan-exception--end",text:end?moment(end,"HH:mm").format(timeFormat).toLowerCase():"-"}),$("<td/>",{html:[$("<button/>",{type:"button",class:"btn btn-outline-secondary btn-sm edit-working-plan-exception",title:lang("edit"),html:[$("<span/>",{class:"fas fa-edit"})]}),$("<button/>",{type:"button",class:"btn btn-outline-secondary btn-sm delete-working-plan-exception",title:lang("delete"),html:[$("<span/>",{class:"fas fa-trash-alt"})]})]})]})}},{key:"addEventListeners",value:function addEventListeners(){var _this=this;$(".working-plan tbody").on("click","input:checkbox",function(event){var id=$(event.currentTarget).attr("id");!0===$(event.currentTarget).prop("checked")?($("#"+id+"-start").prop("disabled",!1).val("9:00 AM"),$("#"+id+"-end").prop("disabled",!1).val("6:00 PM")):($("#"+id+"-start").prop("disabled",!0).val(""),$("#"+id+"-end").prop("disabled",!0).val(""))}),$(".add-break").on("click",function(){var timeFormat="regular"===vars("time_format")?"h:mm a":"HH:mm",$newBreak=$("<tr/>",{html:[$("<td/>",{class:"break-day editable",text:lang("sunday")}),$("<td/>",{class:"break-start editable",text:moment("12:00","HH:mm").format(timeFormat).toLowerCase()}),$("<td/>",{class:"break-end editable",text:moment("14:00","HH:mm").format(timeFormat).toLowerCase()}),$("<td/>",{html:[$("<button/>",{type:"button",class:"btn btn-outline-secondary btn-sm edit-break",title:lang("edit"),html:[$("<span/>",{class:"fas fa-edit"})]}),$("<button/>",{type:"button",class:"btn btn-outline-secondary btn-sm delete-break",title:lang("delete"),html:[$("<span/>",{class:"fas fa-trash-alt"})]}),$("<button/>",{type:"button",class:"btn btn-outline-secondary btn-sm save-break d-none",title:lang("save"),html:[$("<span/>",{class:"fas fa-check-circle"})]}),$("<button/>",{type:"button",class:"btn btn-outline-secondary btn-sm cancel-break d-none",title:lang("cancel"),html:[$("<span/>",{class:"fas fa-ban"})]})]})]}).appendTo(".breaks tbody");_this.editableDayCell($newBreak.find(".break-day")),_this.editableTimeCell($newBreak.find(".break-start, .break-end")),$newBreak.find(".edit-break").trigger("click")}),$(document).on("click",".edit-break",function(event){var $previousEdits=$(event.currentTarget).closest("table").find(".editable");$previousEdits.each(function(index,editable){editable.reset&&editable.reset()});var $tr=$(event.currentTarget).closest("tr");$tr.children().trigger("edit"),App.Utils.UI.initializeTimePicker($tr.find(".break-start input, .break-end input")),$tr.find(".break-day select").focus(),$tr.find(".edit-break, .delete-break").addClass("d-none"),$tr.find(".save-break, .cancel-break").removeClass("d-none"),$tr.find("select,input:text").addClass("form-control form-control-sm")}),$(document).on("click",".delete-break",function(event){$(event.currentTarget).closest("tr").remove()}),$(document).on("click",".cancel-break",function(event){var element=event.target,$modifiedRow=$(element).closest("tr");_this.enableCancel=!0,$modifiedRow.find(".cancel-editable").trigger("click"),_this.enableCancel=!1,$modifiedRow.find(".edit-break, .delete-break").removeClass("d-none"),$modifiedRow.find(".save-break, .cancel-break").addClass("d-none")}),$(document).on("click",".save-break",function(event){var element=event.target,$modifiedRow=$(element).closest("tr"),startMoment=moment($modifiedRow.find(".break-start input").val(),"HH:mm"),endMoment=moment($modifiedRow.find(".break-end input").val(),"HH:mm");startMoment.isAfter(endMoment)&&$modifiedRow.find(".break-end input").val(startMoment.add(1,"hour").format("regular"===vars("time_format")?"h:mm a":"HH:mm").toLowerCase()),_this.enableSubmit=!0,$modifiedRow.find(".editable .submit-editable").trigger("click"),_this.enableSubmit=!1,$modifiedRow.find(".save-break, .cancel-break").addClass("d-none"),$modifiedRow.find(".edit-break, .delete-break").removeClass("d-none")}),$(document).on("click",".add-working-plan-exception",function(){App.Components.WorkingPlanExceptionsModal.add().done(function(date,workingPlanException){var $tr=null;$(".working-plan-exceptions tbody tr").each(function(index,tr){if(date===$(tr).data("date"))return $tr=$(tr),!1});var $newTr=_this.renderWorkingPlanExceptionRow(date,workingPlanException);$tr?$tr.replaceWith($newTr):$newTr.appendTo(".working-plan-exceptions tbody")})}),$(document).on("click",".edit-working-plan-exception",function(event){var $tr=$(event.target).closest("tr"),date=$tr.data("date"),workingPlanException=$tr.data("workingPlanException");App.Components.WorkingPlanExceptionsModal.edit(date,workingPlanException).done(function(date,workingPlanException){$tr.replaceWith(_this.renderWorkingPlanExceptionRow(date,workingPlanException))})}),$(document).on("click",".delete-working-plan-exception",function(event){$(event.currentTarget).closest("tr").remove()})}},{key:"get",value:function get(){var _this2=this,workingPlan={},timeFormat="regular"===vars("time_format")?"h:mm a":"HH:mm";return $(".working-plan input:checkbox").each(function(index,checkbox){var id=$(checkbox).attr("id");!0===$(checkbox).prop("checked")?(workingPlan[id]={start:moment($("#"+id+"-start").val(),timeFormat).format("HH:mm"),end:moment($("#"+id+"-end").val(),timeFormat).format("HH:mm"),breaks:[]},$(".breaks tr").each(function(index,tr){var day=_this2.convertDayToValue($(tr).find(".break-day").text());if(day===id){var start=$(tr).find(".break-start").text(),end=$(tr).find(".break-end").text();workingPlan[id].breaks.push({start:moment(start,"regular"===vars("time_format")?"h:mm a":"HH:mm").format("HH:mm"),end:moment(end,"regular"===vars("time_format")?"h:mm a":"HH:mm").format("HH:mm")})}}),workingPlan[id].breaks.sort(function(break1,break2){return break1.start.localeCompare(break2.start)})):workingPlan[id]=null}),workingPlan}},{key:"getWorkingPlanExceptions",value:function getWorkingPlanExceptions(){var workingPlanExceptions={};return $(".working-plan-exceptions tbody tr").each(function(index,tr){var $tr=$(tr),date=$tr.data("date");workingPlanExceptions[date]=$tr.data("workingPlanException")}),workingPlanExceptions}},{key:"timepickers",value:function timepickers(disabled){disabled=disabled||!1,!1===disabled&&App.Utils.UI.initializeTimePicker($(".working-plan input:text"),{onChange:function onChange(selectedDates,dateStr,instance){var startMoment=moment(selectedDates[0]),$workEnd=$(instance.input).closest("tr").find(".work-end"),endMoment=moment(App.Utils.UI.getDateTimePickerValue($workEnd));startMoment>endMoment&&App.Utils.UI.setDateTimePickerValue($workEnd,startMoment.add(1,"hour").toDate())}})}},{key:"reset",value:function reset(){}},{key:"convertValueToDay",value:function convertValueToDay(value){return"sunday"===value?lang("sunday"):"monday"===value?lang("monday"):"tuesday"===value?lang("tuesday"):"wednesday"===value?lang("wednesday"):"thursday"===value?lang("thursday"):"friday"===value?lang("friday"):"saturday"===value?lang("saturday"):void 0}},{key:"convertDayToValue",value:function convertDayToValue(day){return day===lang("sunday")?"sunday":day===lang("monday")?"monday":day===lang("tuesday")?"tuesday":day===lang("wednesday")?"wednesday":day===lang("thursday")?"thursday":day===lang("friday")?"friday":day===lang("saturday")?"saturday":void 0}}])}();return WorkingPlan}();