268 lines
12 KiB
JavaScript
268 lines
12 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";
|
|
}
|
|
},
|
|
renderTable: function () {
|
|
var html;
|
|
|
|
if (this.options.allowCreateItem && this.options.createItemButtonPlaceholder && this.options.allowEditItem) {
|
|
var createItemHref = this.options.createItemUrl + "?teamId=" + this.options.teamId;
|
|
html = "<button class='btn btn-primary' style='margin-top:5px;margin-left:5px;' onclick=\"onAddItemClick(this, '" + this.options.editItemModalId + "')\" data-addItemUrl=\"" + createItemHref + "\"><i class='fa fa-plus'></i> Add Resource</button>";
|
|
$('#' + 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>");
|
|
$('#teamNonProjectTimeBtn').hide();
|
|
$('#teamNonProjectTimeBtn').data('isHidden', true);
|
|
}
|
|
else {
|
|
if (!$('#teamNonProjectTimeBtn').hasClass("collapsed")) {
|
|
$('#teamNonProjectTimeBtn').show();
|
|
$('#teamNonProjectTimeBtn').data('isHidden', false);
|
|
}
|
|
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;
|
|
var changeItemExpCatHrefTemplate = null;
|
|
|
|
if (this.options.viewItemUrl && (this.options.viewItemUrl.length > 0))
|
|
viewItemHrefTemplate = this.options.viewItemUrl + "?resourceId={0}&teamId=" + this.options.teamId;
|
|
|
|
if (this.options.allowEditItem &&
|
|
this.options.editItemUrl && (this.options.editItemUrl.length > 0) &&
|
|
this.options.editItemModalId && (this.options.editItemModalId.length > 0))
|
|
editItemHrefTemplate = this.options.editItemUrl + "?resourceId={0}";
|
|
|
|
if (this.options.deleteItemUrl && (this.options.deleteItemUrl.length > 0) && this.options.allowEditItem)
|
|
deleteItemHrefTemplate = this.options.deleteItemUrl + "?resourceId={0}&teamId=" + this.options.teamId;
|
|
|
|
if (this.options.allowChangeExpCat &&
|
|
this.options.changeExpCatItemUrl && (this.options.changeExpCatItemUrl.length > 0) &&
|
|
this.options.changeExpCatModalId && (this.options.changeExpCatModalId.length > 0))
|
|
changeItemExpCatHrefTemplate = this.options.changeExpCatItemUrl + "?resourceId={0}";
|
|
|
|
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=\"onEditItemClick(this, '" + this.options.editItemModalId + "')\" data-buttonid=\"" + this.id + "\" \
|
|
data-editUrl=\"" + editItemHref + "\" data-resourceId=\"" + currentResource.Id + "\" href=\"#\" " +
|
|
"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\" style=\"margin-right:4px;\">" +
|
|
"<i class=\"fa fa-trash-o\"></i></a></nobr>";
|
|
}
|
|
|
|
if (changeItemExpCatHrefTemplate) {
|
|
var expChangeForResourceAllowed =
|
|
(!currentResource.ResourceScenarioAllocationsInfo || !currentResource.ResourceScenarioAllocationsInfo.HasAllocations) &&
|
|
(!currentResource.ResourceActualsInfo || !currentResource.ResourceActualsInfo.HasAllocations) &&
|
|
(!currentResource.ResourceMixAllocationsInfo || !currentResource.ResourceMixAllocationsInfo.HasAllocations);
|
|
|
|
if (expChangeForResourceAllowed) {
|
|
var changeItemExpCatHref = changeItemExpCatHrefTemplate.replace("{0}", currentResource.Id);
|
|
currentItemHtml += "<nobr><a onclick=\"onChangeItemExpCatClick(this, '" + this.options.changeExpCatModalId + "')\" data-buttonid=\"" + this.id + "\" \
|
|
data-editUrl=\"" + changeItemExpCatHref + "\" data-resourceId=\"" + currentResource.Id + "\" href=\"#\" " +
|
|
"data-placement=\"left\" class=\"btn btn-xs popover-warning popover-dark\" title=\"Change Expenditure Category\" style=\"margin-right:4px;\">" +
|
|
"<i class=\"fa fa-arrow-up\"></i></a>";
|
|
}
|
|
}
|
|
|
|
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,
|
|
allowEditItem: false,
|
|
allowDeleteItem: false,
|
|
allowChangeExpCat: false,
|
|
createItemUrl: null,
|
|
viewItemUrl: null,
|
|
editItemUrl: null,
|
|
deleteItemUrl: null,
|
|
data: null,
|
|
initCallback: null,
|
|
editItemModalId: null,
|
|
changeExpCatModalId: null
|
|
};
|
|
|
|
$.fn.teamResourcesBrowser.Constructor = TeamResourcesBrowser;
|
|
|
|
}(jQuery));
|
|
|
|
function onEditItemClick(element, editItemModalId) {
|
|
var buttonid = $(element).data("buttonid");
|
|
var resourceId = $(element).data("resourceid");
|
|
if (!CheckLock(buttonid, 'PeopleResource', resourceId)) {
|
|
console.error("CheckLock - false. PeopleResource: " + resourceId);
|
|
return false;
|
|
}
|
|
|
|
var editUrl = $(element).data("editurl");
|
|
$('#editPeopleResourceForm').load(editUrl, function () {
|
|
$.validator.unobtrusive.parse("#editPeopleResourceForm form");
|
|
$('#' + editItemModalId).on('show.bs.modal', function (e) {
|
|
if ($(e.target).attr('id') === editItemModalId) {
|
|
if (initPeopleResourceEditForm && (typeof initPeopleResourceEditForm === 'function')) {
|
|
initPeopleResourceEditForm();
|
|
}
|
|
}
|
|
});
|
|
$('#' + editItemModalId).modal('show');
|
|
});
|
|
}
|
|
|
|
function onAddItemClick(element, editItemModalId) {
|
|
var url = $(element).data("additemurl");
|
|
$('#editPeopleResourceForm').load(url, function () {
|
|
$.validator.unobtrusive.parse("#editPeopleResourceForm form");
|
|
$('#' + editItemModalId).on('show.bs.modal', function (e) {
|
|
if ($(e.target).attr('id') === editItemModalId) {
|
|
if (initPeopleResourceEditForm && (typeof initPeopleResourceEditForm === 'function')) {
|
|
initPeopleResourceEditForm();
|
|
}
|
|
}
|
|
});
|
|
$('#editPeopleResourceForm form').data("url", url);
|
|
$('#' + editItemModalId).modal('show');
|
|
});
|
|
}
|
|
|
|
function onChangeItemExpCatClick(element, changeExpCatModalId) {
|
|
var buttonid = $(element).data("buttonid");
|
|
var resourceId = $(element).data("resourceid");
|
|
if (!CheckLock(buttonid, 'PeopleResource', resourceId)) {
|
|
console.error("CheckLock - false. PeopleResource: " + resourceId);
|
|
return false;
|
|
}
|
|
|
|
var editUrl = $(element).data("editurl");
|
|
$('#peopleResourceChangeExpCatForm').load(editUrl, function () {
|
|
$.validator.unobtrusive.parse("#peopleResourceChangeExpCatForm form");
|
|
|
|
$('#' + changeExpCatModalId).on('show.bs.modal', function (e) {
|
|
if ($(e.target).attr('id') === changeExpCatModalId) {
|
|
if (initPeopleResourceChangeExpCatForm && (typeof initPeopleResourceChangeExpCatForm === 'function')) {
|
|
initPeopleResourceChangeExpCatForm();
|
|
}
|
|
}
|
|
});
|
|
$('#' + changeExpCatModalId).modal('show');
|
|
});
|
|
}
|