Guest User

Untitled

a guest
Oct 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. jQuery.fn.Absense = function(option) {
  2.     $(this).each(function(){
  3.         new Absense($(this), option);
  4.     });
  5. };
  6.  
  7. var Absense = function(el, option){
  8.     this._url = el.data("url");
  9.     this._form = $("form", el);
  10.     this._formWrapper = $(".block-person_absence-add", el);
  11.     this._table = $("table", el);
  12.     this._activeRequest = false;
  13.     this.init(el);
  14.     this.editTr = null;
  15. };
  16.  
  17. Absense.prototype.setActiveRequest = function() {
  18.     if (this._activeRequest) return false;
  19.     this._activeRequest = true;
  20.     if ($(".busy").size() <= 0) {
  21.         $('body').append("<div class='busy'></div>");
  22.     }
  23.     $(".busy").show();
  24.     return true;
  25. }
  26.  
  27. Absense.prototype.unsetActiveRequest = function() {
  28.     if (!this._activeRequest) return;
  29.     this._activeRequest = false;
  30.     $(".busy").hide();
  31. }
  32.  
  33. Absense.prototype.init = function(el){
  34.     var self = this,
  35.         startDate = $('[name="startDate"]', this._form),
  36.         endDate = $('[name="endDate"]', this._form);
  37.  
  38.     $('body').append(this._formWrapper);
  39.  
  40.     this._form.find(".close").click(function(){
  41.         self.clearForm();
  42.         self.hideForm();
  43.         if (self.editTr) {
  44.             self.editTr.show();
  45.             self.editTr = null;
  46.         }
  47.         return false;
  48.     });
  49.  
  50.     $(".button-add", el).click(function(){
  51.         if (self._formWrapper.data("visible") != "true") {
  52.             self.showForm();
  53.         }
  54.         return false;
  55.     });
  56.  
  57.     startDate.datetimepicker({
  58.         addSliderAccess: true,
  59.         sliderAccessArgs: { touchonly: false },
  60.         dateFormat: "yy-mm-dd",
  61.         timeFormat: 'hh:mm',
  62.         showButtonPanel: true,
  63.         minDate: Date.today(),
  64.         onClose: function(dateText, inst) {
  65.             if (!self.editTr) {
  66.                 if (endDate.val() != '') {
  67.                     var testStartDate = new Date(dateText);
  68.                     var testEndDate = new Date(endDate.val());
  69.                     if (testStartDate > testEndDate) {
  70.                         endDate.val(dateText);
  71.                     }
  72.                 } else if (startDate.val() != '') {
  73.                     var start = Date.parse(startDate.val());
  74.                     start.addDays(1);
  75.                     endDate.val(start.toString('yyyy-MM-dd hh:mm'));
  76.                 }
  77.             }
  78.         },
  79.         onSelect: function (selectedDateTime){
  80.             if (!self.editTr) {
  81.                 var start = Date.parse(startDate.val());
  82.                 start.addDays(1);
  83.                 endDate.val(start.toString('yyyy-MM-dd hh:mm'));
  84.             }
  85.         }
  86.     });
  87.  
  88.     endDate.datetimepicker({
  89.         addSliderAccess: true,
  90.         sliderAccessArgs: { touchonly: false },
  91.         dateFormat: "yy-mm-dd",
  92.         minDate: Date.today(),
  93.         showButtonPanel: true,
  94.         timeFormat: 'hh:mm'
  95.     });
  96.  
  97.     this.startDatePicker = startDate.datetimepicker();
  98.     this.endDatePicker = endDate.datetimepicker();
  99.  
  100.     $(".edit", el).live("click" ,function(e){
  101.         //if (!self.editTr) {
  102.         var tr = $(this).parents("tr"),
  103.             td = tr.children();
  104.  
  105.         self.showForm();
  106.         self.setFormValue("startDate",  td.eq(0).text());
  107.         self.setFormValue("endDate",  td.eq(1).text());
  108.         self.setFormValue("reason",  td.eq(2).text());
  109.         self.setFormValue("comment",  td.eq(3).text());
  110.         self._form.append($("<input type='hidden' name='absenceUUID' />").val(tr.data("absenceuuid")));
  111.  
  112.         tr.hide();
  113.  
  114.         self.editTr = tr;
  115.         //}
  116.  
  117.         return false;
  118.     });
  119.  
  120.     $(".remove", el).live("click" ,function(e){
  121.         self.remove($(this).parents("tr"));
  122.         return false;
  123.     });
  124.  
  125.     this._form.on("submit", function(){
  126.         self.save();
  127.         return false;
  128.     });
  129.  
  130. };
  131.  
  132. Absense.prototype.remove = function(el){
  133.  
  134.     if (!this.setActiveRequest()) return false;
  135.  
  136.     var self= this;
  137.  
  138.     $.ajax({
  139.         url: this._url,
  140.         dataType: "JSON",
  141.         data: {
  142.             action: 'remove',
  143.             absenceUUID: el.data("absenceuuid")
  144.         },
  145.         success: function(responce){
  146.             self.unsetActiveRequest();
  147.             if (responce.status == 'success') {
  148.                 self.editTr = null;
  149.                 el.remove();
  150.                 if (self._table.find("tr").length < 4) {
  151.                     self._table.find(".empty-list").show();
  152.                 }
  153.             } else if (responce.status == 'validation_error') {
  154.                 self.displayError(responce)
  155.             }
  156.         },
  157.         error: function(){
  158.             self.unsetActiveRequest();
  159.         }
  160.     })
  161. }
  162.  
  163. Absense.prototype.save = function(){
  164.  
  165.     if (!this.setActiveRequest()) return false;
  166.  
  167.     this._activeRequest = true;
  168.  
  169.     var self = this,
  170.         data = {
  171.             action: "add"
  172.         };
  173.  
  174.     data.startDate = Date.parse(this.getFormValue('startDate').replace(/-/g, "/")).getTime();
  175.     data.endDate = Date.parse(this.getFormValue('endDate').replace(/-/g, "/")).getTime();
  176.     data.reason = this.getFormValue('reason');
  177.     data.comment = this.getFormValue('comment');
  178.  
  179.     if (this._form.find("[name='absenceUUID']").length > 0) {
  180.         data.action = "edit";
  181.         data.absenceUUID = this._form.find("[name='absenceUUID']").val();
  182.     }
  183.  
  184.     $.ajax({
  185.         url: this._url,
  186.         data: data,
  187.         dataType: "JSON",
  188.         success: function(responce){
  189.             self.unsetActiveRequest();
  190.             if (responce.status == 'success') {
  191.                 self._table.show();
  192.                 self.addRow(responce);
  193.                 self.clearForm();
  194.                 self.hideForm();
  195.             } else if (responce.status == 'validation_error') {
  196.                 self.displayError(responce);
  197.             }
  198.         },
  199.         error: function(){
  200.             self.unsetActiveRequest();
  201.         }
  202.     })
  203. }
  204.  
  205. Absense.prototype.addRow = function(data){
  206.     var row = this._table.find(".example-row").clone(),
  207.         properties = ["startDate","endDate","reason","comment"];
  208.  
  209.     for (p in properties) {
  210.         if (properties.hasOwnProperty(p))
  211.             row.find("." + properties[p]).text(data[properties[p]]);
  212.     }
  213.  
  214.     row.data("absenceuuid",data.absenceUUID);
  215.  
  216.     this._table.append(row.removeAttr("class"));
  217.  
  218.     this._table.find(".empty-list").hide();
  219.  
  220. }
  221.  
  222. Absense.prototype.displayError = function(resp){
  223.     $('.error', this._form).remove();
  224.     for (var i in resp.errors ) {
  225.         if (resp.errors.hasOwnProperty(i)) {
  226.             var field  = $("[name='" + i + "']",this._form);
  227.             if (field.length > 0) {
  228.                 field.after("<p class='error'>" + resp.errors[i] + "</p>");
  229.             } else {
  230.                 this._form.prepend("<p class='error'>" + resp.errors[i] + "</p>");
  231.             }
  232.         }
  233.     }
  234.  
  235. }
  236.  
  237. Absense.prototype.clearForm = function(name, value){
  238.     this._form.find("input").val("");
  239.     this._form.find("textarea").text("").val("");
  240.     this._form.find("[name='absenceUUID']").remove();
  241.     $('.error', this._form).remove();
  242. }
  243.  
  244. Absense.prototype.showForm = function(){
  245.     this._formWrapper.show();
  246.     var self = this,
  247.         h = this._formWrapper.outerHeight(),
  248.         shift = $(window).height()/2 + h/2 + 50;
  249.     this._formWrapper.css({
  250.         top: (- h - 50) + "px"
  251.     })
  252.     this._formWrapper.data("visible", "true");
  253.     this._formWrapper.animate({top: "+=" + (shift) + "px"});
  254. }
  255.  
  256. Absense.prototype.hideForm = function(){
  257.     var self = this,
  258.         shift = $(window).height()/2 + this._formWrapper.outerHeight()/2 + 50;
  259.     this._formWrapper.data("visible", "hide");
  260.     this._formWrapper.animate({top: "-=" + shift + "px"}, function(){
  261.         self._formWrapper.hide();
  262.     });
  263. }
  264.  
  265. Absense.prototype.setFormValue = function(name, value){
  266.     this._form.find("[name='" + name + "']").val(value);
  267. }
  268.  
  269. Absense.prototype.getFormValue = function(name){
  270.     return this._form.find("[name='" + name + "']").val();
  271. }
Add Comment
Please, Sign In to add comment