PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/learn-portlet/src/main/webapp/js2.0/lesson-designer/VideoView.js

https://github.com/ViLPy/JSCORM
JavaScript | 169 lines | 156 code | 11 blank | 2 comment | 6 complexity | 43bd568daa26eb8be1c4667bccb07f6c MD5 | raw file
  1. var VideoModel = LessonContentModel.extend({
  2. defaults: {
  3. contentType: 'questionVideo',
  4. title: '',
  5. url: '',
  6. selected: false,
  7. icon: 'video'
  8. }
  9. });
  10. LiferayVideoModel = Backbone.Model.extend({
  11. defaults: {
  12. title: '',
  13. version: '',
  14. mimeType: ''
  15. }
  16. });
  17. LiferayVideoService = new Backbone.Service({
  18. url: path.root,
  19. sync: {
  20. 'read': {
  21. 'path': path.api.liferay,
  22. data: function (collection, options) {
  23. return {
  24. action: 'GETVIDEO',
  25. courseId: Utils.getCourseId(),
  26. page: options.currentPage,
  27. count: options.itemsOnPage
  28. }
  29. },
  30. 'method': 'get'
  31. }
  32. }
  33. });
  34. LiferayVideoGallery = Backbone.Collection.extend({
  35. model: LiferayVideoModel,
  36. parse: function (response) {
  37. this.trigger('videoCollection:updated', {
  38. total: response.total,
  39. currentPage: response.currentPage,
  40. listed: response.records.length
  41. });
  42. return response.records;
  43. }
  44. }).extend(LiferayVideoService);
  45. var VideoView = Backbone.View.extend({
  46. events: {
  47. 'click .videoType': 'changeVideoType'
  48. },
  49. render: function () {
  50. var mustacheAccumulator = {};
  51. _.extend(mustacheAccumulator, this.model.toJSON(), GLOBAL_translations);
  52. this.$el.html(Mustache.render(jQueryValamis('#videoTemplate').html(), mustacheAccumulator));
  53. this.$el.find('.js-url-edit').hide();
  54. this.videoCollection = new LiferayVideoGallery();
  55. this.videoCollection.on('reset', this.renderVideoGallery, this);
  56. this.videoCollection.on("videoCollection:updated", function (details) {
  57. that.updatePagination(details, that);
  58. }, this);
  59. var that = this;
  60. this.paginator = new ValamisPaginator({el: this.$('#videoPaginator'), language: GLOBAL_translations});
  61. this.paginator.setItemsPerPage(5);
  62. this.paginator.on('pageChanged', function () {
  63. that.videoCollection.fetch({
  64. reset: true,
  65. currentPage: that.paginator.currentPage(),
  66. itemsOnPage: that.paginator.itemsOnPage()
  67. });
  68. });
  69. this.videoCollection.fetch({reset: true, currentPage: 1, itemsOnPage: that.paginator.itemsOnPage()});
  70. return this;
  71. },
  72. updatePagination: function (details, context) {
  73. this.paginator.updateItems(details.total);
  74. },
  75. renderVideoGallery: function () {
  76. this.$('#dlvideo').html('');
  77. this.videoCollection.each(this.addVideo, this);
  78. },
  79. addVideo: function (item) {
  80. var view = new VideoElement({model: item});
  81. view.on('unselectAll', this.unselectAll, this);
  82. this.$('#dlvideo').append(view.render().$el);
  83. },
  84. unselectAll: function () {
  85. this.videoCollection.each(function (i) {
  86. i.set({selected: false});
  87. }, this);
  88. },
  89. changeVideoType: function () {
  90. this.$el.find('#dlVideoContainer').toggle(this.$('input#DL').prop('checked'));
  91. this.$el.find('#videoPaginator').toggle(this.$('input#DL').prop('checked'));
  92. this.$el.find('.js-url-edit').toggle(this.$('input#EMBED').prop('checked'));
  93. },
  94. submit: function () {
  95. var docLibrary = (this.$('input[name=videoType]:checked').val() == 'DL');
  96. var url = this.$('.js-url-edit').val();
  97. if (docLibrary) {
  98. var selectedVideo = this.videoCollection.find(function (item) {
  99. return item.get('selected');
  100. });
  101. }
  102. this.model.set({
  103. title: this.$('.js-title-edit').val() || this.model.get('lessonId') ? 'New video' : (selectedVideo) ? selectedVideo.get('title') : 'New video',
  104. mimeType: (selectedVideo) ? selectedVideo.get('mimeType') : '',
  105. url: url,
  106. uuid: (selectedVideo) ? selectedVideo.get('uuid') : '',
  107. groupID: (selectedVideo) ? selectedVideo.get('groupID') : '',
  108. fromDocLibrary: docLibrary
  109. });
  110. if(this.model.get('lessonId')) {
  111. this.model.save();
  112. contentManagerEvent.trigger('question:added', this.model);
  113. } else this.trigger('video:added', this.model);
  114. }
  115. });
  116. var VideoModal = Backbone.Modal.extend({
  117. template: function (data) {
  118. return Mustache.to_html(jQueryValamis('#lessonDesignerEmptyModalTemplate').html(),
  119. _.extend({header: GLOBAL_translations['AddVideoLabel']}, GLOBAL_translations))
  120. },
  121. submitEl: '.bbm-button',
  122. cancelEl: '.modal-close',
  123. className: 'val-modal',
  124. onRender: function () {
  125. this.view = new VideoView({
  126. model: this.model,
  127. el: this.$('.js-modal-content')
  128. });
  129. this.view.render();
  130. var self = this;
  131. this.view.on('video:added', function(model) { self.trigger('video:added', model) });
  132. },
  133. submit: function () {
  134. if (this.view)
  135. this.view.submit(this.model);
  136. }
  137. });
  138. VideoElement = Backbone.View.extend({
  139. events: {
  140. 'click .js-toggleButton': 'toggleThis'
  141. },
  142. tagName: 'tr',
  143. initialize: function () {
  144. this.model.on('change', this.render, this);
  145. },
  146. render: function () {
  147. var template = Mustache.to_html(jQueryValamis('#liferayVideoElementView').html(), this.model.toJSON());
  148. this.$el.html(template);
  149. return this;
  150. },
  151. toggleThis: function () {
  152. var selected = !this.model.get('selected');
  153. this.trigger('unselectAll', this);
  154. // this.$('#dlvideo').find('.js-toggleButton').removeClass('selected');
  155. // if (selected) this.$el.find('.js-toggleButton').addClass('selected');
  156. this.model.set({selected: selected});
  157. }
  158. });