Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //model for a single [Office - Share - Team] combination
- function OfficeShare() {
- var self = this;
- this.id = 0;
- this.percent = ko.observable();
- this.divId = "office-share-";
- this.officeId = "OfficeId-";
- this.teamId = "TeamId-";
- this.shareId = "ShareId-";
- this.selectedOfficeId = ko.observable();
- this.selectedTeamId = ko.observable();
- this.officeOptions = ko.observableArray([]);
- this.teamOptions = ko.observableArray([]);
- this.dynamicOptionsCaption = ko.observable("-- Choose a team --");
- this.hasTeams = ko.observable(false);
- this.selectedOfficeId.subscribe(function () {
- self.teamOptions([]);
- self.loadTeams();
- });
- this.loadTeams = function (request, callback) {
- self.request = request;
- pipeline.ajaxGetJson({
- url: '@Url.Content("~/api/Teams/Search")',
- data: { officeIds: [self.selectedOfficeId], returnTotalRecords: false },
- success: function (response, status, xhr) {
- if (response && response.records && response.records.length > 0) {
- self.dynamicOptionsCaption("-- Choose a team --");
- self.hasTeams(true);
- self.teamOptions(response.records);
- self.selectedTeamId(null);
- } else {
- self.dynamicOptionsCaption("-- No Teams Found --");
- self.selectedTeamId(null);
- self.hasTeams(false);
- }
- if (callback !== undefined) { callback(response); }
- }
- });
- };
- };
- //Parent Model containing all [Office - Share - Team] combinations chosen by the user
- function OfficeSharesViewModel() {
- var self = this;
- this.officeShares = ko.observableArray([]);
- this.countOfShares = ko.computed(function () {
- return self.officeShares().length;
- }, this);
- this.officeOptions = ko.observableArray([]);
- this.isInMultiOfficesMode = ko.observable(false);
- this.addOfficeShare = function (officeShare) {
- officeShare.divId = officeShare.divId + self.countOfShares();
- officeShare.officeId = officeShare.officeId + self.countOfShares();
- officeShare.teamId = officeShare.teamId + self.countOfShares();
- officeShare.shareId = officeShare.shareId + self.countOfShares();
- officeShare.officeOptions = self.officeOptions; //not correct way to assign value to observable array but working
- officeShare.id = self.countOfShares();
- self.officeShares.push(officeShare);
- };
- this.removeOfficeShare = function (officeShare) {
- if (self.countOfShares() > 1) {
- self.officeShares.remove(officeShare);
- } else {
- alert('The opportunity must have at least one office associated with it.');
- }
- };
- this.reduceOfficeSharesToOne = function () {
- while (self.countOfShares() > 1) {
- self.officeShares.remove(self.officeShares()[self.countOfShares() - 1]);
- }
- };
- this.sumPercents = ko.computed(function () {
- var total = 0;
- $.each(self.officeShares(), function (i, e) { total += Number(e.percent()); })
- return total;
- });
- this.sumPercents.subscribe(function () {
- if (self.sumPercents() > 100) {
- alert('The sum of the office shares percentages must be 100%');
- }
- });
- this.loadOfficesOptions = function () {
- pipeline.ajaxGetJson({
- url: '@Url.Content("~/api/Offices/Search")',
- data: { rows: 1000, returnTotalRecords: false },
- success: function (response, status, xhr) {
- self.officeOptions(response.records);
- }
- });
- };
- this.loadOfficeShares = function () {
- pipeline.ajaxGetJson({
- url: '@Url.Content("~/api/Opportunities/Search")',
- data: { opportunityIds: [$('#Id').val()], returnTotalRecords: false },
- success: function (response, status, xhr) {
- if (response && response.records && response.records['0'].officeShares) {
- if (response.records['0'].officeShares.length > 1) { OfficeSharesModel.isInMultiOfficesMode(true); $('#div-btn-add-office-share').show();}
- response.records['0'].officeShares.forEach(function (item) {
- var officeShare = new OfficeShare();
- officeShare.selectedOfficeId(item.officeId);
- officeShare.selectedTeamId(item.teamId);
- officeShare.loadTeams();
- officeShare.percent(item.share);
- self.addOfficeShare(officeShare);
- });
- }
- }
- });
- };
- }
- //initialize parent model / container
- var OfficeSharesModel = new OfficeSharesViewModel();
- $(document).ready(function () {
- //initialize Office-Team dropdowns
- ko.applyBindings(OfficeSharesModel, $('#office-shares').get(0));
- OfficeSharesModel.loadOfficesOptions();
- if ($('#Id').val()) {
- OfficeSharesModel.loadOfficeShares();
- }
- else {
- OfficeSharesModel.addOfficeShare(new OfficeShare());
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement