Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
494
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. //model for a single [Office - Share - Team] combination
  2. function OfficeShare() {
  3. var self = this;
  4. this.id = 0;
  5. this.percent = ko.observable();
  6. this.divId = "office-share-";
  7. this.officeId = "OfficeId-";
  8. this.teamId = "TeamId-";
  9. this.shareId = "ShareId-";
  10. this.selectedOfficeId = ko.observable();
  11. this.selectedTeamId = ko.observable();
  12. this.officeOptions = ko.observableArray([]);
  13. this.teamOptions = ko.observableArray([]);
  14. this.dynamicOptionsCaption = ko.observable("-- Choose a team --");
  15. this.hasTeams = ko.observable(false);
  16. this.selectedOfficeId.subscribe(function () {
  17. self.teamOptions([]);
  18. self.loadTeams();
  19. });
  20. this.loadTeams = function (request, callback) {
  21. self.request = request;
  22.  
  23. pipeline.ajaxGetJson({
  24. url: '@Url.Content("~/api/Teams/Search")',
  25. data: { officeIds: [self.selectedOfficeId], returnTotalRecords: false },
  26. success: function (response, status, xhr) {
  27. if (response && response.records && response.records.length > 0) {
  28. self.dynamicOptionsCaption("-- Choose a team --");
  29. self.hasTeams(true);
  30. self.teamOptions(response.records);
  31. self.selectedTeamId(null);
  32. } else {
  33. self.dynamicOptionsCaption("-- No Teams Found --");
  34. self.selectedTeamId(null);
  35. self.hasTeams(false);
  36. }
  37. if (callback !== undefined) { callback(response); }
  38. }
  39. });
  40. };
  41. };
  42.  
  43. //Parent Model containing all [Office - Share - Team] combinations chosen by the user
  44. function OfficeSharesViewModel() {
  45. var self = this;
  46. this.officeShares = ko.observableArray([]);
  47. this.countOfShares = ko.computed(function () {
  48. return self.officeShares().length;
  49. }, this);
  50. this.officeOptions = ko.observableArray([]);
  51. this.isInMultiOfficesMode = ko.observable(false);
  52. this.addOfficeShare = function (officeShare) {
  53. officeShare.divId = officeShare.divId + self.countOfShares();
  54. officeShare.officeId = officeShare.officeId + self.countOfShares();
  55. officeShare.teamId = officeShare.teamId + self.countOfShares();
  56. officeShare.shareId = officeShare.shareId + self.countOfShares();
  57. officeShare.officeOptions = self.officeOptions; //not correct way to assign value to observable array but working
  58. officeShare.id = self.countOfShares();
  59. self.officeShares.push(officeShare);
  60. };
  61. this.removeOfficeShare = function (officeShare) {
  62. if (self.countOfShares() > 1) {
  63. self.officeShares.remove(officeShare);
  64. } else {
  65. alert('The opportunity must have at least one office associated with it.');
  66. }
  67. };
  68. this.reduceOfficeSharesToOne = function () {
  69. while (self.countOfShares() > 1) {
  70. self.officeShares.remove(self.officeShares()[self.countOfShares() - 1]);
  71. }
  72. };
  73. this.sumPercents = ko.computed(function () {
  74. var total = 0;
  75. $.each(self.officeShares(), function (i, e) { total += Number(e.percent()); })
  76. return total;
  77. });
  78. this.sumPercents.subscribe(function () {
  79. if (self.sumPercents() > 100) {
  80. alert('The sum of the office shares percentages must be 100%');
  81. }
  82. });
  83. this.loadOfficesOptions = function () {
  84. pipeline.ajaxGetJson({
  85. url: '@Url.Content("~/api/Offices/Search")',
  86. data: { rows: 1000, returnTotalRecords: false },
  87. success: function (response, status, xhr) {
  88. self.officeOptions(response.records);
  89. }
  90. });
  91. };
  92. this.loadOfficeShares = function () {
  93. pipeline.ajaxGetJson({
  94. url: '@Url.Content("~/api/Opportunities/Search")',
  95. data: { opportunityIds: [$('#Id').val()], returnTotalRecords: false },
  96. success: function (response, status, xhr) {
  97. if (response && response.records && response.records['0'].officeShares) {
  98. if (response.records['0'].officeShares.length > 1) { OfficeSharesModel.isInMultiOfficesMode(true); $('#div-btn-add-office-share').show();}
  99. response.records['0'].officeShares.forEach(function (item) {
  100. var officeShare = new OfficeShare();
  101. officeShare.selectedOfficeId(item.officeId);
  102. officeShare.selectedTeamId(item.teamId);
  103. officeShare.loadTeams();
  104. officeShare.percent(item.share);
  105. self.addOfficeShare(officeShare);
  106. });
  107. }
  108. }
  109. });
  110. };
  111. }
  112. //initialize parent model / container
  113. var OfficeSharesModel = new OfficeSharesViewModel();
  114.  
  115.  
  116. $(document).ready(function () {
  117.  
  118. //initialize Office-Team dropdowns
  119. ko.applyBindings(OfficeSharesModel, $('#office-shares').get(0));
  120.  
  121. OfficeSharesModel.loadOfficesOptions();
  122. if ($('#Id').val()) {
  123. OfficeSharesModel.loadOfficeShares();
  124. }
  125. else {
  126. OfficeSharesModel.addOfficeShare(new OfficeShare());
  127. };
  128. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement