/learn-portlet/src/main/webapp/js2.0/widgets/LiferaySiteSelectDialog.js

https://github.com/ViLPy/JSCORM · JavaScript · 154 lines · 141 code · 13 blank · 0 comment · 5 complexity · 436b31b6c32316b0d9765d6ffe550f60 MD5 · raw file

  1. LiferaySiteModel = Backbone.Model.extend({
  2. defaults: {
  3. siteID: '',
  4. title: '',
  5. url: '',
  6. description: ''
  7. }
  8. });
  9. LiferaySiteCollectionService = new Backbone.Service({ url: '/',
  10. sync: {
  11. 'read': {
  12. path: path.api.courses,
  13. 'data': function (collection, options) {
  14. var filter = options.filter || '';
  15. var sort = 'true';
  16. if (options.sort)
  17. sort = options.sort;
  18. return {
  19. companyID: jQuery('#curriculumCompanyID').val(),
  20. filter: filter,
  21. sortAscDirection: sort,
  22. page: options.currentPage,
  23. count: options.itemsOnPage
  24. }
  25. },
  26. 'method': 'get'
  27. }
  28. }
  29. });
  30. LiferaySiteCollection = Backbone.Collection.extend({
  31. model: LiferaySiteModel,
  32. parse: function (response) {
  33. this.trigger('siteCollection:updated', { total: response.total, currentPage: response.currentPage, listed: response.records.length });
  34. return response.records;
  35. }
  36. }).extend(LiferaySiteCollectionService);
  37. LiferaySiteListElement = Backbone.View.extend({
  38. events: {
  39. "click .js-toggle-site-button": "toggleThis"
  40. },
  41. tagName: 'tr',
  42. initialize: function (options) {
  43. this.singleSelect = options.singleSelect;
  44. },
  45. render: function () {
  46. var template = Mustache.to_html(jQuery('#liferaySiteElementView').html(), {title: this.model.get('title')});
  47. this.$el.html(template);
  48. return this;
  49. },
  50. toggleThis: function () {
  51. if (this.singleSelect)
  52. this.trigger('lfSiteSelected', this.model);
  53. var alreadySelected = this.model.get('selected');
  54. if (alreadySelected) {
  55. this.model.set({selected: false });
  56. this.$('.js-toggle-site-button').removeClass('primary');
  57. this.$('.js-toggle-site-button').addClass('neutral');
  58. }
  59. else {
  60. this.model.set({selected: true });
  61. this.$('.js-toggle-site-button').removeClass('neutral');
  62. this.$('.js-toggle-site-button').addClass('primary');
  63. }
  64. },
  65. isSelected: function () {
  66. return this.model.get('selected');
  67. }
  68. });
  69. LiferaySitesContainer = Backbone.View.extend({
  70. events: {
  71. 'keyup #siteSearch': 'filterCourses',
  72. 'click .dropdown-menu > li': 'filterCourses',
  73. 'click .js-addCourses': 'addCourses'
  74. },
  75. initialize: function (options) {
  76. this.language = options.language;
  77. this.singleSelect = options.singleSelect;
  78. this.collection = new LiferaySiteCollection();
  79. this.collection.on('reset', this.addSites, this);
  80. var template = Mustache.to_html(jQuery('#liferaySiteDialogView').html(), _.extend({singleSelect: this.singleSelect}, this.language));
  81. this.$el.html(template);
  82. this.$el.find('.dropdown').valamisDropDown();
  83. var that = this;
  84. this.collection.on("siteCollection:updated", function (details) {
  85. that.updatePagination(details, that);
  86. }, this);
  87. this.pageModel = new PageModel();
  88. this.pageModel.set({'itemsOnPage': 10});
  89. this.paginator = new ValamisPaginator({
  90. el: this.$('#siteListPaginator'),
  91. language: this.language,
  92. model: this.pageModel
  93. });
  94. this.paginatorShowing = new ValamisPaginatorShowing({
  95. el: this.$('#siteListPagingShowing'),
  96. language: this.language,
  97. model: this.paginator.model
  98. });
  99. this.paginator.on('pageChanged', function () {
  100. that.collection.fetch({reset: true, currentPage: that.paginator.currentPage(), itemsOnPage: that.paginator.itemsOnPage()});
  101. });
  102. },
  103. updatePagination: function (details, context) {
  104. this.paginator.updateItems(details.total);
  105. },
  106. fetchSites: function () {
  107. this.collection.fetch({
  108. reset: true,
  109. currentPage: this.paginator.currentPage(),
  110. itemsOnPage: this.paginator.itemsOnPage(),
  111. filter: this.$('#siteSearch').val(),
  112. sort: this.$('#sortSite').data('value')
  113. });
  114. },
  115. addSites: function() {
  116. this.$('#siteList').empty();
  117. this.collection.each(this.addSite, this);
  118. },
  119. addSite: function (site) {
  120. var view = new LiferaySiteListElement({model: site, singleSelect: this.singleSelect});
  121. view.on('lfSiteSelected', function (item) {
  122. this.trigger('lfSiteSelected', item);
  123. }, this);
  124. this.$('#siteList').append(view.render().$el);
  125. },
  126. render: function () {
  127. this.fetchSites();
  128. return this;
  129. },
  130. addCourses: function () {
  131. this.trigger('addSelectedLfSite', this.collection);
  132. },
  133. filterCourses: function () {
  134. clearTimeout(this.inputTimeout);
  135. this.inputTimeout = setTimeout(this.applyFilter.bind(this), 800);
  136. },
  137. applyFilter: function () {
  138. clearTimeout(this.inputTimeout);
  139. this.fetchSites();
  140. }
  141. });