/learn-portlet/src/main/webapp/js2.0/curriculum/views/CertificateSelectStatementsView.js

https://github.com/ViLPy/JSCORM · JavaScript · 175 lines · 165 code · 10 blank · 0 comment · 10 complexity · 4616052d2b6aebed7c4a6f5ea75a1d85 MD5 · raw file

  1. SelectStatementCollectionService = new Backbone.Service({ url: path.root,
  2. sync: {
  3. 'read': {
  4. 'path': path.api.certificates,
  5. 'data': function (e, options) {
  6. return {
  7. action: 'GETSTATEMENTS',
  8. courseId: Utils.getCourseId(),
  9. page: options.currentPage,
  10. count: options.itemsOnPage,
  11. filter: options.filter,
  12. sortAscDirection: options.order
  13. }
  14. },
  15. 'method': 'get'
  16. }
  17. }
  18. });
  19. SelectStatementCollection = Backbone.Collection.extend({
  20. model: StatementModel,
  21. parse: function (response) {
  22. this.trigger('statementCollection:updated', { total: response.total, currentPage: response.currentPage});
  23. return response.records;
  24. }
  25. }).extend(SelectStatementCollectionService);
  26. var CertificateSelectStatementsRowView = Backbone.View.extend({
  27. events: {
  28. "click .js-toggle-button": "toggleThis"
  29. },
  30. tagName: 'tr',
  31. initialize: function (options) {
  32. function getLangDictionaryValue(value, lang) {
  33. var langDict = value,
  34. key;
  35. if (typeof lang !== "undefined" && typeof langDict[lang] !== "undefined") {
  36. return langDict[lang];
  37. }
  38. if (typeof langDict.und !== "undefined") {
  39. return langDict.und;
  40. }
  41. if (typeof langDict["en-US"] !== "undefined") {
  42. return langDict["en-US"];
  43. }
  44. for (key in langDict) {
  45. if (langDict.hasOwnProperty(key)) {
  46. return langDict[key];
  47. }
  48. }
  49. return "";
  50. }
  51. this.options = options;
  52. this.model.on('change', this.render, this);
  53. this.model.set('verbName',getLangDictionaryValue(this.model.get('verbName')));
  54. this.model.set('objName',getLangDictionaryValue(this.model.get('objName')));
  55. },
  56. render: function () {
  57. var template = Mustache.to_html(jQuery('#selectStatementsRowView').html(), this.model.toJSON());
  58. this.$el.html(template);
  59. return this;
  60. },
  61. toggleThis: function () {
  62. this.model.toggle();
  63. this.trigger('unsetIsSelectedAll');
  64. }
  65. });
  66. var CertificateSelectStatementsDialogView = Backbone.View.extend({
  67. SEARCH_TIMEOUT: 800,
  68. events: {
  69. 'click #addStatementsButton': 'addStatements',
  70. 'keyup #statementsSearch': 'filterStatements',
  71. 'click #sortStatements li': 'filterStatements',
  72. 'click #selectAllStatements': 'selectAllStatements'
  73. },
  74. initialize: function (options) {
  75. this.options = options;
  76. this.language = options.language;
  77. this.isSelectedAll = false;
  78. this.collection = new SelectStatementCollection();
  79. var that = this;
  80. this.collection.on('statementCollection:updated', function (details) {
  81. that.updatePagination(details, that);
  82. });
  83. this.collection.on('reset', this.renderElements, this);
  84. this.paginatorModel = new PageModel();
  85. this.paginatorModel.set({'itemsOnPage': 10});
  86. },
  87. filterStatements: function () {
  88. clearTimeout(this.inputTimeout);
  89. this.inputTimeout = setTimeout(this.applyFilter.bind(this), this.SEARCH_TIMEOUT);
  90. },
  91. applyFilter: function () {
  92. clearTimeout(this.inputTimeout);
  93. this.reloadFirstPage();
  94. },
  95. render: function () {
  96. var template = Mustache.to_html(jQuery('#selectStatementsDialogView').html(), this.language);
  97. this.$el.html(template);
  98. this.$('.dropdown').valamisDropDown();
  99. var that = this;
  100. this.paginator = new ValamisPaginator({
  101. el: this.$el.find("#statementListPaginator"),
  102. language: this.language,
  103. model: this.paginatorModel
  104. });
  105. this.paginator.on('pageChanged', function () {
  106. that.reload();
  107. });
  108. this.paginatorShowing = new ValamisPaginatorShowing({
  109. el: this.$el.find("#statementListPagingShowing"),
  110. language: this.language,
  111. model: this.paginator.model
  112. });
  113. this.reloadFirstPage();
  114. return this;
  115. },
  116. selectAllStatements: function () {
  117. this.isSelectedAll = !this.isSelectedAll;
  118. var that = this;
  119. this.collection.each(function (item) {
  120. if (item.get('selected') != that.isSelectedAll)
  121. item.toggle();
  122. });
  123. },
  124. unsetIsSelectedAll: function () {
  125. this.isSelectedAll = false;
  126. },
  127. renderElements: function () {
  128. if (this.collection.length > 0) {
  129. this.$('#noStatementsLabel').hide();
  130. this.collection.each(function (item) {
  131. var row = new CertificateSelectStatementsRowView({model: item});
  132. row.on('unsetIsSelectedAll', this.unsetIsSelectedAll, this);
  133. this.$('#statementsList').append(row.render().$el);
  134. }, this);
  135. } else {
  136. this.$('#noStatementsLabel').show();
  137. }
  138. },
  139. updatePagination: function (details, context) {
  140. this.paginator.updateItems(details.total);
  141. },
  142. fetchCollection: function (page) {
  143. this.$('#statementsList').empty();
  144. this.collection.fetch({
  145. reset: true,
  146. currentPage: page,
  147. itemsOnPage: this.paginator.itemsOnPage(),
  148. filter: this.$('#statementsSearch').val(),
  149. order: this.$('#sortStatements').data('value')
  150. });
  151. },
  152. reloadFirstPage: function () {
  153. this.fetchCollection(1);
  154. },
  155. reload: function () {
  156. this.fetchCollection(this.paginator.currentPage());
  157. },
  158. addStatements: function () {
  159. var selectedStatements = this.collection.filter(function (item) {
  160. return item.get('selected');
  161. });
  162. this.options.parentWindow.trigger('addStatements', selectedStatements);
  163. this.trigger('closeModal', this);
  164. }
  165. });