jetpack-modules.views.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. this.jetpackModules = this.jetpackModules || {};
  2. window.jetpackModules.views = (function( window, $, _, Backbone, wp ) {
  3. 'use strict';
  4. var views = {};
  5. views.List_Table = Backbone.View.extend({
  6. template : wp.template( 'Jetpack_Modules_List_Table_Template' ),
  7. /**
  8. * If we can, use replaceState to change the URL and indicate the new filtering.
  9. * This will be handy with redirecting back to the same state after activating/deactivating.
  10. */
  11. updateUrl : function() {
  12. if ( ! window.history.replaceState ) {
  13. return;
  14. }
  15. var url = window.location.href.split('?')[0] + '?page=jetpack_modules',
  16. m_tag = $('.subsubsub .current'),
  17. m_filter = $('.button-group.filter-active .active'),
  18. m_sort = $('.button-group.sort .active'),
  19. m_search = $('#srch-term-search-input').val();
  20. if ( m_search.length ) {
  21. url += '&s=' + encodeURIComponent( m_search );
  22. }
  23. if ( ! m_tag.hasClass('all') ) {
  24. url += '&module_tag=' + encodeURIComponent( m_tag.data('title') );
  25. }
  26. if ( m_filter.data('filter-by') ) {
  27. url += '&' + encodeURIComponent( m_filter.data('filter-by') ) + '=' + encodeURIComponent( m_filter.data('filter-value') );
  28. }
  29. if ( 'name' !== m_sort.data('sort-by') ) {
  30. url += '&sort_by=' + encodeURIComponent( m_sort.data('sort-by') );
  31. }
  32. window.history.replaceState( {}, '', url );
  33. },
  34. render : function() {
  35. this.model.filter_and_sort();
  36. this.$el.html( this.template( this.model.attributes ) );
  37. this.updateUrl();
  38. return this;
  39. },
  40. initialize : function() {
  41. this.listenTo( this.model, 'change', this.render );
  42. }
  43. });
  44. return views;
  45. })( this, jQuery, _, Backbone, wp );