easyappointments/assets/js/pages/services.min.js

1 line
8.2 KiB
JavaScript
Executable File

"use strict";App.Pages.Services=function(){function filter(keyword){var selectId=1<arguments.length&&void 0!==arguments[1]?arguments[1]:null,show=!!(2<arguments.length&&void 0!==arguments[2])&&arguments[2];App.Http.Services.search(keyword,filterLimit).then(function(response){filterResults=response,$filterServices.find(".results").empty(),response.forEach(function(service){$filterServices.find(".results").append(App.Pages.Services.getFilterHtml(service)).append($("<hr/>"))}),0===response.length?$filterServices.find(".results").append($("<em/>",{text:lang("no_records_found")})):response.length===filterLimit&&$("<button/>",{type:"button",class:"btn btn-outline-secondary w-100 load-more text-center",text:lang("load_more"),click:function click(){filterLimit+=20,App.Pages.Services.filter(keyword,selectId,show)}}).appendTo("#filter-services .results"),selectId&&App.Pages.Services.select(selectId,show)})}function select(id){var show=!!(1<arguments.length&&void 0!==arguments[1])&&arguments[1];if($filterServices.find(".selected").removeClass("selected"),$filterServices.find(".service-row[data-id=\""+id+"\"]").addClass("selected"),show){var service=filterResults.find(function(filterResult){return+filterResult.id===+id});App.Pages.Services.display(service),$("#edit-service, #delete-service").prop("disabled",!1)}}function updateAvailableServiceCategories(){App.Http.ServiceCategories.search("",999).then(function(response){$serviceCategoryId.empty(),$serviceCategoryId.append(new Option("","")).val(""),response.forEach(function(serviceCategory){$serviceCategoryId.append(new Option(serviceCategory.name,serviceCategory.id))})})}var $services=$("#services"),$id=$("#id"),$name=$("#name"),$duration=$("#duration"),$price=$("#price"),$currency=$("#currency"),$serviceCategoryId=$("#service-category-id"),$availabilitiesType=$("#availabilities-type"),$attendantsNumber=$("#attendants-number"),$isPrivate=$("#is-private"),$location=$("#location"),$description=$("#description"),$filterServices=$("#filter-services"),$color=$("#color"),filterResults={},filterLimit=20;return document.addEventListener("DOMContentLoaded",function initialize(){App.Pages.Services.resetForm(),App.Pages.Services.filter(""),App.Pages.Services.addEventListeners(),updateAvailableServiceCategories()}),{filter:filter,save:function save(service){App.Http.Services.save(service).then(function(response){App.Layouts.Backend.displayNotification(lang("service_saved")),App.Pages.Services.resetForm(),$filterServices.find(".key").val(""),App.Pages.Services.filter("",response.id,!0)})},remove:function remove(id){App.Http.Services.destroy(id).then(function(){App.Layouts.Backend.displayNotification(lang("service_deleted")),App.Pages.Services.resetForm(),App.Pages.Services.filter($filterServices.find(".key").val())})},validate:function validate(){$services.find(".is-invalid").removeClass("is-invalid"),$services.find(".form-message").removeClass("alert-danger").hide();try{var missingRequired=!1;if($services.find(".required").each(function(index,requiredField){$(requiredField).val()||($(requiredField).addClass("is-invalid"),missingRequired=!0)}),missingRequired)throw new Error(lang("fields_are_required"));if(+$duration.val()<vars("event_minimum_duration"))throw $duration.addClass("is-invalid"),new Error(lang("invalid_duration"));return!0}catch(error){return $services.find(".form-message").addClass("alert-danger").text(error.message).show(),!1}},getFilterHtml:function getFilterHtml(service){var name=service.name,info=service.duration+" min - "+service.price+" "+service.currency;return $("<div/>",{class:"service-row entry","data-id":service.id,html:[$("<strong/>",{text:name}),$("<br/>"),$("<small/>",{class:"text-muted",text:info}),$("<br/>")]})},resetForm:function resetForm(){$filterServices.find(".selected").removeClass("selected"),$filterServices.find("button").prop("disabled",!1),$filterServices.find(".results").css("color",""),$services.find(".record-details").find("input, select, textarea").val("").prop("disabled",!0),$services.find(".record-details .form-label span").prop("hidden",!0),$services.find(".record-details #is-private").prop("checked",!1),$services.find(".record-details h4 a").remove(),$services.find(".add-edit-delete-group").show(),$services.find(".save-cancel-group").hide(),$("#edit-service, #delete-service").prop("disabled",!0),$services.find(".record-details .is-invalid").removeClass("is-invalid"),$services.find(".record-details .form-message").hide(),App.Components.ColorSelection.disable($color)},display:function display(service){$id.val(service.id),$name.val(service.name),$duration.val(service.duration),$price.val(service.price),$currency.val(service.currency),$description.val(service.description),$location.val(service.location),$availabilitiesType.val(service.availabilities_type),$attendantsNumber.val(service.attendants_number),$isPrivate.prop("checked",service.is_private),App.Components.ColorSelection.setColor($color,service.color);var serviceCategoryId=null===service.id_service_categories?"":service.id_service_categories;$serviceCategoryId.val(serviceCategoryId)},select:select,addEventListeners:function addEventListeners(){$services.on("submit","#filter-services form",function(event){event.preventDefault();var key=$filterServices.find(".key").val();$filterServices.find(".selected").removeClass("selected"),App.Pages.Services.resetForm(),App.Pages.Services.filter(key)}),$services.on("click",".service-row",function(event){if($filterServices.find(".filter").prop("disabled"))return void $filterServices.find(".results").css("color","#AAA");var serviceId=$(event.currentTarget).attr("data-id"),service=filterResults.find(function(filterResult){return+filterResult.id===+serviceId}),dedicatedUrl=App.Utils.Url.siteUrl("?service="+encodeURIComponent(service.id)),$link=$("<a/>",{href:dedicatedUrl,target:"_blank",html:[$("<i/>",{class:"fas fa-link me-2"}),$("<span/>",{text:lang("booking_link")})]});$services.find(".record-details h4").find("a").remove().end().append($link),App.Pages.Services.display(service),$filterServices.find(".selected").removeClass("selected"),$(event.currentTarget).addClass("selected"),$("#edit-service, #delete-service").prop("disabled",!1)}),$services.on("click","#add-service",function(){App.Pages.Services.resetForm(),$services.find(".add-edit-delete-group").hide(),$services.find(".save-cancel-group").show(),$services.find(".record-details").find("input, select, textarea").prop("disabled",!1),$services.find(".record-details .form-label span").prop("hidden",!1),$filterServices.find("button").prop("disabled",!0),$filterServices.find(".results").css("color","#AAA"),App.Components.ColorSelection.enable($color),$name.val("Service"),$duration.val("30"),$price.val("0"),$currency.val(""),$serviceCategoryId.val(""),$availabilitiesType.val("flexible"),$attendantsNumber.val("1")}),$services.on("click","#cancel-service",function(){var id=$id.val();App.Pages.Services.resetForm(),""!==id&&App.Pages.Services.select(id,!0)}),$services.on("click","#save-service",function(){var service={name:$name.val(),duration:$duration.val(),price:$price.val(),currency:$currency.val(),description:$description.val(),location:$location.val(),color:App.Components.ColorSelection.getColor($color),availabilities_type:$availabilitiesType.val(),attendants_number:$attendantsNumber.val(),is_private:+$isPrivate.prop("checked"),id_service_categories:$serviceCategoryId.val()||void 0};""!==$id.val()&&(service.id=$id.val()),App.Pages.Services.validate()&&App.Pages.Services.save(service)}),$services.on("click","#edit-service",function(){$services.find(".add-edit-delete-group").hide(),$services.find(".save-cancel-group").show(),$services.find(".record-details").find("input, select, textarea").prop("disabled",!1),$services.find(".record-details .form-label span").prop("hidden",!1),$filterServices.find("button").prop("disabled",!0),$filterServices.find(".results").css("color","#AAA"),App.Components.ColorSelection.enable($color)}),$services.on("click","#delete-service",function(){var serviceId=$id.val(),buttons=[{text:lang("cancel"),click:function click(event,messageModal){messageModal.hide()}},{text:lang("delete"),click:function click(event,messageModal){App.Pages.Services.remove(serviceId),messageModal.hide()}}];App.Utils.Message.show(lang("delete_service"),lang("delete_record_prompt"),buttons)})}}}();