/** * @author Kaj Ström * @copyright DL Software Oy, 2014. */ /** * Object for managing AJAX calls for reservation details. * * Retrieved details are cached per page load. * * @type {object} */ var ReservationDetailEntities = { entities: [], /** * Retrieve a reservation detail entity. * @param {Number} id * @param {String} location * @param {Number} rsvDetId * @returns jQuery promise for the AJAX call. */ getReservationDetailEntity: function (id, location, rsvDetId) { var defer = $.Deferred(); var url = "index.php?func=rd_ajax&id=" + id + "&location=" + location + "&usebfc=1"; if (rsvDetId) { url += "&rsv_det_id=" + rsvDetId; } if (!_.has(this.entities, id)) { $.ajax({ url: url, async: true, dataType: "json", context: this, cache: false, success: function (data) { defer.resolve(data); this.entities[id] = data; }, error: function () { defer.reject("ajaxerror"); } }); } else { defer.resolve(this.entities[id]); } return defer.promise(); } }; /** * Reservation details. * @param {jQuery} $tr * @param {Number} id * @param {String} location * @param {Number} rsvDetId * @param {Boolean} rowAllowRsv * @constructor */ function ReservationListDetails($tr, id, location, rsvDetId, rowAllowRsv) { this.$detailsDiv = null; $tr.addClass("details-open"); this.$tr = $tr; this.id = id; this.rsvDetId = rsvDetId; this.location = location; this.rowAllowRsv = rowAllowRsv; this.colspan = $tr.find("td:visible").length; this.showDetails(); $("#er_"+this.id).hide(); /*Piilotetaan extra-rivi*/ } /** * Render a container for the reservation details view. */ ReservationListDetails.prototype.renderDetailContainer = function () { var self = this; if (this.$detailsDiv === null) { this.$detailsDiv = $(_.template($("#tpl-rsv-details").html(), { id: this.id, colspan: this.colspan })); this.$tr.after(this.$detailsDiv); this.$detailsDiv.fadeIn(600); //Event listeners this.closeFunction = function () { self.close(); }; this.$detailsDiv.find(".js-close").on("click", this.closeFunction); this.$tr.on("click", this.closeFunction); this.$tr.find("span.glyphicon-chevron-down") .removeClass("glyphicon-chevron-down") .addClass("glyphicon-chevron-up"); } }; /** * Render the loading view. */ ReservationListDetails.prototype.renderLoadingView = function () { this.$detailsDiv.find(".detail-contents").html($("#tpl-rsv-details-loading").html()); }; /** * Display reservation details. */ ReservationListDetails.prototype.showDetails = function () { var self = this; this.renderDetailContainer(); this.renderLoadingView(); var retrievingDetails = ReservationDetailEntities.getReservationDetailEntity(this.id, this.location, this.rsvDetId); $.when(retrievingDetails).done(function (details) { self.renderDetails(details); }); $.when(retrievingDetails).fail(function () { alert("An error occurred while retrieving the reservation information"); self.close(); }); }; /** * Render details view * @param {object} details */ ReservationListDetails.prototype.renderDetails = function (details) { details.obj_memo_text = unescape(details.obj_memo_text); var template = $("#tpl-rsv-details-view").html(); details.row_allow_rsv = this.rowAllowRsv; details.res_description = $("#er_"+this.id+" td.res_description").html(); /*Underscore template do html-entities*/ this.$detailsDiv.find(".detail-contents").html(_.template(template, details)); var html = this.$detailsDiv.find(".detail-contents").html(); /*Allow html-tags*/ html = ReplaceAll(html, '<', "<"); html = ReplaceAll(html, '>', ">"); this.$detailsDiv.find(".detail-contents").html(html); }; /** * Close the details view and remove event listeners. */ ReservationListDetails.prototype.close = function () { var self = this; this.$detailsDiv.fadeOut(600, function () { self.$detailsDiv.remove(); self.$tr.removeClass("details-open"); self.$tr.unbind("click", self.closeFunction); self.$tr.find("span.glyphicon-chevron-up") .removeClass("glyphicon-chevron-up") .addClass("glyphicon-chevron-down"); }); //Näytetään extra-rivi $('#er_'+this.id).show(); }; function DrawClickForDetails () { if ('' != '') { var tableWidth = $("#table_rsv_list tbody").width() + 'px'; $("#table_rsv_list tbody").find("tr:visible ").each(function () { if (!$(this).parent().hasClass('day')) { var firstTd = $(this).find("td:first"); if (firstTd.find("div").html() != undefined) { firstTd.find("div").css('width', tableWidth); }else{ var divHtml = '
'; firstTd.html(firstTd.html() + divHtml); } } }); } } $(document).ready(function () { /* DrawClickForDetails(); $(window).resize(function() { DrawClickForDetails(); }); */ $("#table_rsv_list tbody").find("tr:not(.detail-row)").click(function (e) { var $target = null; if (e.target.tagName === "TD") { var $target = $(e.target).closest("tr"); } if (e.target.tagName === "TR") { var $target = $(e.target); } if ($target != null && $target.hasClass('extra_row')) { $target = $('#' + $target.attr('parent')); } if ($target != null && !$target.hasClass("details-open")) { new ReservationListDetails($target, $target.attr("id"), $target.data("location"), $target.data("rsvdetid"), $target.data("allowrsv")); } }); });