Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jQuery.fn.Absense = function(option) {
- $(this).each(function(){
- new Absense($(this), option);
- });
- };
- var Absense = function(el, option){
- this._url = el.data("url");
- this._form = $("form", el);
- this._formWrapper = $(".block-person_absence-add", el);
- this._table = $("table", el);
- this._activeRequest = false;
- this.init(el);
- this.editTr = null;
- };
- Absense.prototype.setActiveRequest = function() {
- if (this._activeRequest) return false;
- this._activeRequest = true;
- if ($(".busy").size() <= 0) {
- $('body').append("<div class='busy'></div>");
- }
- $(".busy").show();
- return true;
- }
- Absense.prototype.unsetActiveRequest = function() {
- if (!this._activeRequest) return;
- this._activeRequest = false;
- $(".busy").hide();
- }
- Absense.prototype.init = function(el){
- var self = this,
- startDate = $('[name="startDate"]', this._form),
- endDate = $('[name="endDate"]', this._form);
- $('body').append(this._formWrapper);
- this._form.find(".close").click(function(){
- self.clearForm();
- self.hideForm();
- if (self.editTr) {
- self.editTr.show();
- self.editTr = null;
- }
- return false;
- });
- $(".button-add", el).click(function(){
- if (self._formWrapper.data("visible") != "true") {
- self.showForm();
- }
- return false;
- });
- startDate.datetimepicker({
- addSliderAccess: true,
- sliderAccessArgs: { touchonly: false },
- dateFormat: "yy-mm-dd",
- timeFormat: 'hh:mm',
- showButtonPanel: true,
- minDate: Date.today(),
- onClose: function(dateText, inst) {
- if (!self.editTr) {
- if (endDate.val() != '') {
- var testStartDate = new Date(dateText);
- var testEndDate = new Date(endDate.val());
- if (testStartDate > testEndDate) {
- endDate.val(dateText);
- }
- } else if (startDate.val() != '') {
- var start = Date.parse(startDate.val());
- start.addDays(1);
- endDate.val(start.toString('yyyy-MM-dd hh:mm'));
- }
- }
- },
- onSelect: function (selectedDateTime){
- if (!self.editTr) {
- var start = Date.parse(startDate.val());
- start.addDays(1);
- endDate.val(start.toString('yyyy-MM-dd hh:mm'));
- }
- }
- });
- endDate.datetimepicker({
- addSliderAccess: true,
- sliderAccessArgs: { touchonly: false },
- dateFormat: "yy-mm-dd",
- minDate: Date.today(),
- showButtonPanel: true,
- timeFormat: 'hh:mm'
- });
- this.startDatePicker = startDate.datetimepicker();
- this.endDatePicker = endDate.datetimepicker();
- $(".edit", el).live("click" ,function(e){
- //if (!self.editTr) {
- var tr = $(this).parents("tr"),
- td = tr.children();
- self.showForm();
- self.setFormValue("startDate", td.eq(0).text());
- self.setFormValue("endDate", td.eq(1).text());
- self.setFormValue("reason", td.eq(2).text());
- self.setFormValue("comment", td.eq(3).text());
- self._form.append($("<input type='hidden' name='absenceUUID' />").val(tr.data("absenceuuid")));
- tr.hide();
- self.editTr = tr;
- //}
- return false;
- });
- $(".remove", el).live("click" ,function(e){
- self.remove($(this).parents("tr"));
- return false;
- });
- this._form.on("submit", function(){
- self.save();
- return false;
- });
- };
- Absense.prototype.remove = function(el){
- if (!this.setActiveRequest()) return false;
- var self= this;
- $.ajax({
- url: this._url,
- dataType: "JSON",
- data: {
- action: 'remove',
- absenceUUID: el.data("absenceuuid")
- },
- success: function(responce){
- self.unsetActiveRequest();
- if (responce.status == 'success') {
- self.editTr = null;
- el.remove();
- if (self._table.find("tr").length < 4) {
- self._table.find(".empty-list").show();
- }
- } else if (responce.status == 'validation_error') {
- self.displayError(responce)
- }
- },
- error: function(){
- self.unsetActiveRequest();
- }
- })
- }
- Absense.prototype.save = function(){
- if (!this.setActiveRequest()) return false;
- this._activeRequest = true;
- var self = this,
- data = {
- action: "add"
- };
- data.startDate = Date.parse(this.getFormValue('startDate').replace(/-/g, "/")).getTime();
- data.endDate = Date.parse(this.getFormValue('endDate').replace(/-/g, "/")).getTime();
- data.reason = this.getFormValue('reason');
- data.comment = this.getFormValue('comment');
- if (this._form.find("[name='absenceUUID']").length > 0) {
- data.action = "edit";
- data.absenceUUID = this._form.find("[name='absenceUUID']").val();
- }
- $.ajax({
- url: this._url,
- data: data,
- dataType: "JSON",
- success: function(responce){
- self.unsetActiveRequest();
- if (responce.status == 'success') {
- self._table.show();
- self.addRow(responce);
- self.clearForm();
- self.hideForm();
- } else if (responce.status == 'validation_error') {
- self.displayError(responce);
- }
- },
- error: function(){
- self.unsetActiveRequest();
- }
- })
- }
- Absense.prototype.addRow = function(data){
- var row = this._table.find(".example-row").clone(),
- properties = ["startDate","endDate","reason","comment"];
- for (p in properties) {
- if (properties.hasOwnProperty(p))
- row.find("." + properties[p]).text(data[properties[p]]);
- }
- row.data("absenceuuid",data.absenceUUID);
- this._table.append(row.removeAttr("class"));
- this._table.find(".empty-list").hide();
- }
- Absense.prototype.displayError = function(resp){
- $('.error', this._form).remove();
- for (var i in resp.errors ) {
- if (resp.errors.hasOwnProperty(i)) {
- var field = $("[name='" + i + "']",this._form);
- if (field.length > 0) {
- field.after("<p class='error'>" + resp.errors[i] + "</p>");
- } else {
- this._form.prepend("<p class='error'>" + resp.errors[i] + "</p>");
- }
- }
- }
- }
- Absense.prototype.clearForm = function(name, value){
- this._form.find("input").val("");
- this._form.find("textarea").text("").val("");
- this._form.find("[name='absenceUUID']").remove();
- $('.error', this._form).remove();
- }
- Absense.prototype.showForm = function(){
- this._formWrapper.show();
- var self = this,
- h = this._formWrapper.outerHeight(),
- shift = $(window).height()/2 + h/2 + 50;
- this._formWrapper.css({
- top: (- h - 50) + "px"
- })
- this._formWrapper.data("visible", "true");
- this._formWrapper.animate({top: "+=" + (shift) + "px"});
- }
- Absense.prototype.hideForm = function(){
- var self = this,
- shift = $(window).height()/2 + this._formWrapper.outerHeight()/2 + 50;
- this._formWrapper.data("visible", "hide");
- this._formWrapper.animate({top: "-=" + shift + "px"}, function(){
- self._formWrapper.hide();
- });
- }
- Absense.prototype.setFormValue = function(name, value){
- this._form.find("[name='" + name + "']").val(value);
- }
- Absense.prototype.getFormValue = function(name){
- return this._form.find("[name='" + name + "']").val();
- }
Add Comment
Please, Sign In to add comment