EnVisageOnline/Main-RMO/Source/EnVisage/Scripts/Plugins/TeamResourcesBrowser.js

181 lines
6.1 KiB
JavaScript

/* ===========================================================
* TeamResourcesBrowser.js v0.0.1
* ===========================================================
* Copyright 2015 Prevu
* ========================================================== */
(function ($) {
"use strict"; // jshint ;_;
/* TeamResourcesBrowser CLASS DEFINITION
* ====================== */
var TeamResourcesBrowser = function (element, options) {
this.init(element, options);
};
TeamResourcesBrowser.prototype = {
constructor: TeamResourcesBrowser,
init: function (element, options) {
var plugin = this;
this.options = options;
this.$container = $(element);
this.$dataset = {};
this.$dataset.data = options.data ? options.data : null;
this.$controls = {};
//this.renderInitial();
this.validateIncomingParams();
if (this.$dataset.data) {
this.renderTable();
}
if (this.options.initCallback && (typeof this.options.initCallback === 'function')) {
this.options.initCallback(this.$dataset);
}
},
validateIncomingParams: function () {
if (!this.options.teamId) {
throw "Team Id must be set";
}
if (this.options.allowCreateItem && (!this.options.createItemUrl || (this.options.createItemUrl.length < 1))) {
throw "Create new item mode was set to ON, but create item URL was not specified";
}
},
//renderInitial: function () {
// if (this.options.loadingImageUrl && (this.options.loadingImageUrl.length > 0)) {
// var html = "<div style='text-align: center;'>" +
// '<span class="control-label" style="margin-left: 10px;"><img src="' + this.options.loadingImageUrl +
// '" /> loading...</span>' +
// '</div>';
// $(this.$container).html(html);
// }
//},
renderTable: function () {
var html;
if (this.options.allowCreateItem && this.options.createItemButtonPlaceholder) {
var createItemHref = this.options.createItemUrl + "?teamId=" + this.options.teamId;
html = "<a class='btn btn-xs btn-primary pull-right no-expand' href='" + createItemHref + "' style='margin-right:10px;'><i class='fa fa-plus'></i> Add</a> ";
$('#' + this.options.createItemButtonPlaceholder).html(html);
}
html = "";
if (!this.$dataset.data || this.$dataset.data.length < 1) {
html += ("<span>No resources was found in the team</span>");
}
else {
html += "<table cellpadding='0' cellspacing='0' border='0' class='table table-striped resourceTable'>" +
"<thead><tr>" +
"<th>Name</th>" +
"<th>Expenditure Category</th>" +
"<th></th>" +
"</tr></thead>" +
"<tbody>{0}</tbody>" +
"<tfoot></tfoot>" +
"</table>";
var tableBodyHtml = "";
var viewItemHrefTemplate = null;
var editItemHrefTemplate = null;
var deleteItemHrefTemplate = null;
if (this.options.viewItemUrl && (this.options.viewItemUrl.length > 0))
viewItemHrefTemplate = this.options.viewItemUrl + "?resourceId={0}&teamId=" + this.options.teamId;
if (this.options.editItemUrl && (this.options.editItemUrl.length > 0))
editItemHrefTemplate = this.options.editItemUrl + "?resourceId={0}&teamId=" + this.options.teamId;
if (this.options.deleteItemUrl && (this.options.deleteItemUrl.length > 0))
deleteItemHrefTemplate = this.options.deleteItemUrl + "?resourceId={0}&teamId=" + this.options.teamId;
for (var index = 0; index < this.$dataset.data.length; index++) {
var currentResource = this.$dataset.data[index];
var currentItemHtml = "<tr>";
if (viewItemHrefTemplate) {
var viewItemHref = viewItemHrefTemplate.replace("{0}", currentResource.Id);
currentItemHtml += "<td><a href='" + viewItemHref + "'>" + currentResource.Name + "</a></td>";
}
else {
currentItemHtml += "<td>" + currentResource.Name + "</td>";
}
currentItemHtml +=
"<td>" + currentResource.ExpenditureCategory + "</td>" +
"<td>";
if (editItemHrefTemplate) {
var editItemHref = editItemHrefTemplate.replace("{0}", currentResource.Id);
currentItemHtml += "<nobr><a onclick=\"return CheckLock(this.id, 'PeopleResource', '" +
currentResource.Id + "');\" href=\"" + editItemHref + "\" " +
"data-placement=\"left\" class=\"btn btn-primary btn-xs popover-warning popover-dark\" title=\"Edit\" style=\"margin-right:4px;\">" +
"<i class=\"fa fa-pencil\"></i></a>";
}
if (deleteItemHrefTemplate) {
var deleteItemHref = deleteItemHrefTemplate.replace("{0}", currentResource.Id);
currentItemHtml += "<a onclick=\"return CheckLock(this.id, 'PeopleResource', '" +
currentResource.Id + "');\" href=\"" + deleteItemHref + "\" " +
"data-placement=\"left\" class=\"btn btn-danger btn-xs popover-warning popover-dark\" title=\"Delete\">" +
"<i class=\"fa fa-trash-o\"></i></a></nobr>";
}
currentItemHtml += "</td></tr>";
tableBodyHtml += currentItemHtml;
}
}
html = html.replace("{0}", tableBodyHtml);
this.$container.html(html);
},
setData: function (data) {
if (data && data.resources) {
this.$dataset.data = data.resources;
}
else {
this.$dataset.data = null;
}
this.renderTable();
},
destroy: function () {
var e = $.Event('destroy');
this.$container.trigger(e);
if (e.isDefaultPrevented()) return;
this.$container.removeData('teamResourcesBrowser');
}
};
/* TEAM RESOURCES BROWSER PLUGIN DEFINITION
* ======================= */
$.fn.teamResourcesBrowser = function (option, args) {
return this.each(function () {
var $this = $(this),
data = $this.data('teamResourcesBrowser'),
options = $.extend({}, $.fn.teamResourcesBrowser.defaults, $this.data(), typeof option == 'object' && option);
if (!data) $this.data('teamResourcesBrowser', (data = new TeamResourcesBrowser(this, options)));
if (typeof option == 'string')
data[option].apply(data, [].concat(args));
});
};
$.fn.teamResourcesBrowser.defaults = {
teamId: null,
createItemButtonPlaceholder: null,
allowCreateItem: false,
createItemUrl: null,
viewItemUrl: null,
editItemUrl: null,
deleteItemUrl: null,
data: null,
initCallback: null,
};
$.fn.teamResourcesBrowser.Constructor = TeamResourcesBrowser;
}(jQuery));