PageRenderTime 296ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/types/library/toolset/toolset-common/utility/help-videos/res/js/toolset-help-videos.js

https://gitlab.com/Fraternal-Group/fraternal
JavaScript | 373 lines | 317 code | 55 blank | 1 comment | 18 complexity | fcaf86a944e99066a5370e45ebb554a9 MD5 | raw file
  1. var WP_Toolset = WPV_Toolset || {};
  2. WP_Toolset.HelpVideos = {};
  3. if( typeof _ !== 'undefined' && _.templateSettings )
  4. {
  5. _.templateSettings = {
  6. escape: /\{\{([^\}]+?)\}\}(?!\})/g,
  7. evaluate: /<#([\s\S]+?)#>/g,
  8. interpolate: /\{\{\{([\s\S]+?)\}\}\}/g
  9. };
  10. }
  11. WP_Toolset.HelpVideosFactory = function ($) {
  12. var self = this,
  13. videos = WP_ToolsetVideoSettings.video_instances,
  14. current = WP_ToolsetVideoSettings.current,
  15. seen = WP_ToolsetVideoSettings.seen,
  16. collection,
  17. append_done = false,
  18. triggered_manually = false,
  19. collection_view = null;
  20. self.init = function () {
  21. return self.show_video(current);
  22. };
  23. self.get_seen = function(){
  24. return seen;
  25. }
  26. self.populate_collection = function(){
  27. collection = new WP_Toolset.HelpVideosCollection();
  28. _.each(videos, function(v){
  29. collection.add( new WP_Toolset.HelpVideo( v ) );
  30. });
  31. return collection;
  32. };
  33. self.get_videos = function(){
  34. return collection;
  35. };
  36. self.create_on_the_fly = function(element,append_to){
  37. if( append_done === false ){
  38. var el = self.create_element(element);
  39. jQuery(append_to).append(el);
  40. append_done = true;
  41. }
  42. };
  43. self.handle_list = function(){
  44. var models = self.populate_collection();
  45. collection_view = new WP_Toolset.HelpVideosListView({model:models});
  46. jQuery( '.js-toolset-videos-wrapper' ).append( jQuery(WP_ToolsetVideoSettings.VIDEOS_LIST_TITLE), collection_view.$el );
  47. };
  48. self.show_video = function( video ){
  49. if ( videos.hasOwnProperty(video) ) {
  50. if( adminpage !== WP_ToolsetVideoSettings.detached_page && videos[video].hasOwnProperty('append_to') && videos[video].append_to !== '' ){
  51. self.create_on_the_fly( videos[video].element ? videos[video].element : WP_ToolsetVideoSettings.GENERIC_ELEMENT, videos[video].append_to );
  52. }
  53. var model = WP_Toolset.HelpVideos.hasOwnProperty(video) ? WP_Toolset.HelpVideos[video] : new WP_Toolset.HelpVideo(videos[video]),
  54. view = new WP_Toolset.HelpVideoView({
  55. el: videos[video].element ? videos[video].element : WP_ToolsetVideoSettings.GENERIC_ELEMENT,
  56. model: model
  57. });
  58. if( adminpage !== WP_ToolsetVideoSettings.detached_page && self.get_seen() === 'seen' && triggered_manually === false ){
  59. view.$el.hide();
  60. view.manual_trigger();
  61. triggered_manually = true;
  62. }
  63. jQuery('.js-toolset-videos-wrapper').width( model.get('width') ).height( model.get('height') );
  64. WP_Toolset.HelpVideos[video] = model;
  65. return WP_Toolset.HelpVideos[video];
  66. }
  67. return null;
  68. };
  69. self.show_new_video = function( model ){
  70. self.remove_list();
  71. jQuery('.js-toolset-videos-wrapper').append( self.create_element( model.get('element') ) );
  72. self.show_video( model.get('name') );
  73. };
  74. self.create_element = function( selector ){
  75. var sel = selector.substring(1);
  76. return jQuery('<div class="'+sel+'" id="'+sel+'"></div>')
  77. };
  78. self.remove_list = function(){
  79. jQuery('.js-videos-list-title').remove();
  80. collection_view.remove();
  81. };
  82. self.init();
  83. };
  84. WP_Toolset.HelpVideo = Backbone.Model.extend({
  85. defaults: {
  86. title:'',
  87. name: '',
  88. url: '',
  89. element: '',
  90. screens: [],
  91. width:'600px',
  92. height:'400px'
  93. }
  94. });
  95. WP_Toolset.HelpVideosCollection = Backbone.Collection.extend({
  96. model:WP_Toolset.HelpVideo,
  97. current:null
  98. });
  99. WP_Toolset.HelpVideoView = Backbone.View.extend({
  100. DELAY:200,
  101. initialize: function (options) {
  102. var self = this;
  103. self.template_selector = '#toolset-video-template';
  104. self.template = _.template(jQuery(self.template_selector).html());
  105. self.deatch_url = WP_ToolsetVideoSettings.detach_url;
  106. self.render(options).el;
  107. },
  108. render: function (options) {
  109. var self = this;
  110. self.$el.html(self.template(self.model.toJSON()));
  111. self.wrap = jQuery('.js-toolset-box-container', self.$el);
  112. self.handle_detach();
  113. self.wrap.loaderOverlay('show', {
  114. class:'loader-overlay-high-z',
  115. css : {
  116. "opacity" : "0.65",
  117. height : self.model.get('height')
  118. }
  119. }
  120. );
  121. self.hidden_wrap = jQuery('.js-video-player-box', self.$el);
  122. self.remove_button = jQuery('.js-remove-video', self.$el);
  123. self.handle_video();
  124. self.remove_video();
  125. return self;
  126. },
  127. handle_detach:function(){
  128. var self = this, $button = jQuery('.js-detach-video', self.$el);
  129. if( adminpage === WP_ToolsetVideoSettings.detached_page ){
  130. $button.hide();
  131. } else {
  132. $button.on('click', function(event){
  133. event.stopImmediatePropagation();
  134. event.preventDefault();
  135. self.remove_button.trigger('click');
  136. window.open( self.deatch_url );
  137. });
  138. }
  139. },
  140. handle_video:function(){
  141. var self = this;
  142. var video = jQuery('.js-video-player');
  143. self.player = new MediaElementPlayer( video, {
  144. alwaysShowHours: false,
  145. width:self.model.get('width'),
  146. height:self.model.get('height'),
  147. success: function (mediaElement, domObject) {
  148. mediaElement.addEventListener('loadeddata', function(e) {
  149. mediaElement.pause();
  150. self.hidden_wrap.fadeIn(self.DELAY, function(event){
  151. self.setPlay( mediaElement );
  152. });
  153. }, false);
  154. mediaElement.addEventListener('ended', function(e) {
  155. self.setPlay( mediaElement, true );
  156. }, false);
  157. mediaElement.addEventListener('play', function(e) {
  158. jQuery('.mejs-mediaelement').loaderOverlay('hide');
  159. }, false);
  160. },
  161. // fires when a problem is detected
  162. error: function () {
  163. }
  164. } );
  165. },
  166. setPlay:function( mediaElement, after_ended ){
  167. var play = jQuery('<i class="fa fa-play-circle js-toolset-play-video"></i>'),
  168. $title = jQuery('.js-video-box-title-open').eq(0).detach().clone();
  169. jQuery('.mejs-mediaelement').loaderOverlay('show', {
  170. class:'loader-overlay-high-z',
  171. css : {
  172. "opacity" : "0.7",
  173. 'height': jQuery('.mejs-mediaelement').height() - 30 + 'px'
  174. }
  175. });
  176. jQuery('.js-video-box-title-open').remove();
  177. jQuery('.toolset-box-container .loader-overlay').append($title);
  178. jQuery('.toolset-box-container .preloader').css({
  179. 'background':'none'
  180. }).append(play);
  181. jQuery('.js-toolset-play-video').on('click', function(event){
  182. event.stopImmediatePropagation();
  183. event.preventDefault();
  184. jQuery('.mejs-mediaelement').loaderOverlay('hide',{onRemove:function(){
  185. mediaElement.play();
  186. }});
  187. });
  188. this.wrap.loaderOverlay('hide',{onRemove:function(){
  189. }, fadeOutSpeed:200});
  190. },
  191. remove_video:function(){
  192. var self = this;
  193. self.remove_button.on('click', function(event){
  194. event.stopImmediatePropagation();
  195. event.preventDefault();
  196. self.$el.hide(400, function(){
  197. if( adminpage === WP_ToolsetVideoSettings.detached_page ){
  198. WP_Toolset.HelpVideos.main.handle_list();
  199. } else {
  200. self.insert_title_view();
  201. }
  202. self.remove();
  203. });
  204. });
  205. },
  206. manual_trigger:function(){
  207. var self = this;
  208. self.remove_button.trigger('click');
  209. },
  210. insert_title_view:function(){
  211. var self = this,
  212. index = self.$el.index(),
  213. parent = self.$el.parent(),
  214. element = self.model.get('element') ? self.model.get('element').substring(1) : WP_ToolsetVideoSettings.GENERIC_ELEMENT,
  215. new_me = new WP_Toolset.HelpVideoListView({
  216. model:self.model,
  217. template_selector:'#toolset-video-header-template',
  218. tagName:'div',
  219. classes:element,
  220. id:element
  221. });
  222. parent.insertAtIndex(index, new_me.$el);
  223. return new_me;
  224. }
  225. });
  226. WP_Toolset.HelpVideoListView = Backbone.View.extend({
  227. initialize:function(options){
  228. var self = this;
  229. self.tagName = options.tagName;
  230. self.$el.addClass(options.classes + ' video-title-alone');
  231. if( options.hasOwnProperty('id') ){
  232. self.$el.prop('id', options.id);
  233. }
  234. self.template_selector = options.template_selector;
  235. self.template = _.template(jQuery(self.template_selector).html());
  236. self.render( options ).el
  237. return self;
  238. },
  239. render:function( options ){
  240. var self = this;
  241. self.$el.html(self.template(self.model.toJSON()));
  242. self.show();
  243. return self;
  244. },
  245. show:function(){
  246. var self = this;
  247. self.$el.on('click', function(event){
  248. event.stopImmediatePropagation();
  249. event.preventDefault();
  250. if( adminpage === WP_ToolsetVideoSettings.detached_page ) {
  251. WP_Toolset.HelpVideos.main.show_new_video(self.model);
  252. } else {
  253. self.insert_video_view();
  254. }
  255. });
  256. },
  257. insert_video_view:function(){
  258. var self = this,
  259. index = self.$el.index(),
  260. parent = self.$el.parent();
  261. self.$el.empty();
  262. parent.insertAtIndex( index, self.$el.clone() );
  263. self.remove();
  264. WP_Toolset.HelpVideos.main.show_video( self.model.get('name') );
  265. }
  266. });
  267. WP_Toolset.HelpVideosListView = Backbone.View.extend({
  268. el:'.js-videos-list',
  269. tagName: 'ul',
  270. initialize:function(options){
  271. var self = this;
  272. self.$el = self.create_element();
  273. self.el = self.$el[0];
  274. self.render(options).el;
  275. return self;
  276. },
  277. render:function( option ){
  278. var self = this,
  279. options = _.extend({}, option);
  280. self.$el.empty();
  281. self.fragment = document.createDocumentFragment();
  282. self.appendModelElement( options );
  283. self.$el.append( self.fragment );
  284. return self;
  285. },
  286. appendModelElement:function( option ){
  287. var self = this, view, el, options = option;
  288. self.model.each(function(model){
  289. try{
  290. options = {
  291. model:model
  292. }
  293. view = new WP_Toolset.HelpVideoListView({
  294. model:model,
  295. template_selector:'#toolset-video-list-template',
  296. tagName:'li',
  297. classes:'js-video-list toolset-video-list'
  298. });
  299. el = view.el;
  300. self.fragment.appendChild( el );
  301. }
  302. catch( e )
  303. {
  304. console.error( e.message );
  305. }
  306. }, self)
  307. return this;
  308. },
  309. create_element:function(){
  310. return jQuery('<ul class="js-videos-list toolset-videos-list"></ul>');
  311. }
  312. });
  313. (function ($) {
  314. $(function () {
  315. WP_Toolset.HelpVideos.main = {};
  316. WP_Toolset.HelpVideosFactory.call(WP_Toolset.HelpVideos.main, $);
  317. });
  318. }(jQuery));