/* ===========================================================
* 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 = "
" +
// '
loading...' +
// '
';
// $(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 = " Add ";
$('#' + this.options.createItemButtonPlaceholder).html(html);
}
html = "";
if (!this.$dataset.data || this.$dataset.data.length < 1) {
html += ("No resources was found in the team");
}
else {
html += "" +
"" +
"| Name | " +
"Expenditure Category | " +
" | " +
"
" +
"{0}" +
"" +
"
";
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 = "";
if (viewItemHrefTemplate) {
var viewItemHref = viewItemHrefTemplate.replace("{0}", currentResource.Id);
currentItemHtml += "| " + currentResource.Name + " | ";
}
else {
currentItemHtml += "" + currentResource.Name + " | ";
}
currentItemHtml +=
"" + currentResource.ExpenditureCategory + " | " +
"";
if (editItemHrefTemplate) {
var editItemHref = editItemHrefTemplate.replace("{0}", currentResource.Id);
currentItemHtml += "" +
"";
}
if (deleteItemHrefTemplate) {
var deleteItemHref = deleteItemHrefTemplate.replace("{0}", currentResource.Id);
currentItemHtml += "" +
"";
}
currentItemHtml += " |
";
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));