PageRenderTime 52ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/learnpress/assets/js/frontend/course-lesson.js

https://gitlab.com/gregtyka/lfmawordpress
JavaScript | 202 lines | 192 code | 7 blank | 3 comment | 30 complexity | fbea784a34645426bf2be31a08246661 MD5 | raw file
  1. ;(function ($) {
  2. "use strict";
  3. $.LP_Course_Item = function () {
  4. }
  5. $.LP_Course_Item.Model = Backbone.Model.extend({
  6. url : function () {
  7. return this.rootUrl
  8. },
  9. rootUrl : '',
  10. initialize: function (data) {
  11. this.rootUrl = data.rootUrl;
  12. },
  13. load : function (callback) {
  14. var that = this,
  15. _completed = function (response, success) {
  16. var $html = $(response || ''),
  17. $lesson = $html.find('#learn-press-course-lesson');
  18. if ($lesson.length == 0) {
  19. $lesson = $('<div id="learn-press-course-lesson" />');
  20. }
  21. if (LearnPress.Hook.applyFilters('learn_press_update_item_content', $lesson, that) !== false) {
  22. that.set('content', $lesson);
  23. $(document).trigger('learn_press_course_item_content_replaced', $lesson, that);
  24. $('.course-item.item-current')
  25. .removeClass('item-current');
  26. $('.course-item.course-item-' + that.get('id'))
  27. .addClass('item-current');
  28. }
  29. $.isFunction(callback) && callback.call(that, response);
  30. };
  31. $.ajax({
  32. url : this.url(),
  33. dataType: 'html',
  34. success : function (response) {
  35. _completed(response, true);
  36. },
  37. error : function () {
  38. _completed('', false)
  39. }
  40. })
  41. },
  42. complete : function (args) {
  43. var that = this;
  44. args = $.extend({
  45. data : null,
  46. success: null
  47. }, args || {});
  48. $.ajax({
  49. url : LearnPress_Settings.ajax,
  50. dataType: 'html',
  51. data : $.extend({
  52. action: 'learnpress_complete_lesson',
  53. id : this.get('id')
  54. }, args.data || {}),
  55. success : function (response) {
  56. response = LearnPress.parseJSON(response);
  57. $.isFunction(args.success) && args.success.call(that, $.extend(response, {id: that.get('id')}))
  58. }
  59. })
  60. }
  61. });
  62. $.LP_Course_Item.View = Backbone.View.extend({
  63. el : '#learn-press-course-lesson',
  64. events : {
  65. 'click .complete-lesson-button': '_completeLesson'
  66. },
  67. initialize : function () {
  68. _.bindAll(this, 'updateItem', '_completeLesson');
  69. this.model.on('change', this.updateItem, this);
  70. if (LearnPress.Hook.applyFilters('learn_press_before_load_item', this) !== false) {
  71. if (this.model.get('id') && this.$('input[name="learn-press-lesson-viewing"]').val() != this.model.get('id')) {
  72. if (this.model.get('content')) {
  73. this.updateItem();
  74. } else {
  75. this.model.load();
  76. }
  77. } else if (this.model.get('content')) {
  78. LearnPress.Hook.doAction('learn_press_item_content_loaded', this.model.get('content'), this);
  79. }
  80. }
  81. },
  82. updateItem : function () {
  83. var $content = this.model.get('content');
  84. this.$el.replaceWith($content);
  85. this.setElement($content);
  86. var url = LearnPress.Hook.applyFilters('learn_press_set_item_url', this.model.get('rootUrl'), this);
  87. if (url) {
  88. LearnPress.setUrl(url);
  89. }
  90. LearnPress.Hook.doAction('learn_press_item_content_loaded', $content, this);
  91. },
  92. _autoNextItem : function (item, delay) {
  93. var $link = this.$('.course-item-next a[data-id="' + item + '"]');
  94. if (!$link.length) {
  95. return;
  96. }
  97. var duration = 3,
  98. $span = $('<span>Auto next in ' + duration + 's</span>').insertAfter(this.$('.complete-lesson-button'));
  99. setInterval(function () {
  100. duration--;
  101. $span.html('Auto next in ' + duration + 's');
  102. if (duration == 0) {
  103. $link.trigger('click')
  104. }
  105. }, 1000);
  106. },
  107. _completeLesson: function (e) {
  108. var that = this;
  109. this.model.complete({
  110. data : $(e.target).data(),
  111. success: function (response) {
  112. response = LearnPress.Hook.applyFilters('learn_press_user_complete_lesson_response', response);
  113. if (response.next_item) {
  114. //that._autoNextItem(response.next_item, 3);
  115. }
  116. LearnPress.Hook.doAction('learn_press_user_completed_lesson', response, that);
  117. }
  118. });
  119. }
  120. });
  121. $.LP_Course_Item.Collection = Backbone.Collection.extend({
  122. model : $.LP_Course_Item.Model,
  123. current : 0,
  124. initialize: function () {
  125. var that = this;
  126. _.bindAll(this, 'initItems', 'loadItem');
  127. this.initItems();
  128. },
  129. initItems : function () {
  130. var that = this;
  131. $('.section-content .course-item').each(function () {
  132. var $li = $(this),
  133. $link = $li.find('a'),
  134. id = parseInt($link.attr('data-id')),
  135. args = {
  136. id : id,
  137. nonce : {
  138. complete: $link.attr('data-complete-nonce')
  139. },
  140. rootUrl: $link.attr('href'),
  141. type : $li.data('type')
  142. };
  143. if ($li.hasClass('item-current')) {
  144. that.current = id;
  145. args.content = $('#learn-press-course-lesson')
  146. }
  147. var model = new $.LP_Course_Item.Model(args);
  148. that.add(model);
  149. });
  150. },
  151. loadItem : function (item, link) {
  152. if ($.isNumeric(item)) {
  153. item = this.findWhere({id: item});
  154. } else if ($.type(item) == 'string') {
  155. item = this.findWhere({rootUrl: item});
  156. }
  157. if (LearnPress.Hook.applyFilters('learn_press_load_item_content', true, item, link) !== false) {
  158. if (item) {
  159. if (this.view) {
  160. this.view.undelegateEvents();
  161. //this.view.model.set('content', this.view.$el);
  162. $('.course-item.item-current')
  163. .removeClass('item-current');
  164. $('.course-item.course-item-' + item.get('id'))
  165. .addClass('item-current');
  166. }
  167. if (link) {
  168. item.set('rootUrl', link);
  169. item.rootUrl = link;
  170. }
  171. this.view = new $.LP_Course_Item.View({model: item});
  172. }
  173. }
  174. }
  175. });
  176. $.LP_Course_Item_List_View = Backbone.View.extend({
  177. model : $.LP_Course_Item.Collection,
  178. el : 'body',
  179. events : {
  180. 'click .section-content .course-item': '_loadItem',
  181. 'click .course-item-nav a' : '_loadItem'
  182. },
  183. initialize: function (args) {
  184. _.bindAll(this, '_loadItem');
  185. //this.model.loadItem(this.model.current);
  186. },
  187. _loadItem : function (e) {
  188. e.preventDefault();
  189. var $item = $(e.target),
  190. id = parseInt($item.attr('data-id')),
  191. link = $item.attr('href');
  192. this.model.loadItem(id ? id : link, link);
  193. }
  194. });
  195. })(jQuery);