Нет описания

jetpack-modules.models.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. this.jetpackModules = this.jetpackModules || {};
  2. window.jetpackModules.models = ( function( window, $, _, Backbone ) {
  3. 'use strict';
  4. var models = {};
  5. models.Modules = Backbone.Model.extend( {
  6. visibles: {},
  7. /**
  8. * Updates modules.items dataset to be a reflection of both the current
  9. * modules.raw data, as well as any filters or sorting that may be in effect.
  10. */
  11. filter_and_sort: function() {
  12. var subsubsub = $( '.subsubsub .current' ),
  13. items = this.get( 'raw' ),
  14. m_filter = $( '.button-group.filter-active .active' ),
  15. m_sort = $( '.button-group.sort .active' ),
  16. m_search = $( '#srch-term-search-input' )
  17. .val()
  18. .toLowerCase(),
  19. groups;
  20. // If a module filter has been selected, filter it!
  21. if ( ! subsubsub.closest( 'li' ).hasClass( 'all' ) ) {
  22. items = _.filter( items, function( item ) {
  23. return _.contains( item.module_tags, subsubsub.data( 'title' ) );
  24. } );
  25. }
  26. if ( m_filter.data( 'filter-by' ) ) {
  27. items = _.filter( items, function( item ) {
  28. return item[ m_filter.data( 'filter-by' ) ] === m_filter.data( 'filter-value' );
  29. } );
  30. }
  31. if ( m_search.length ) {
  32. items = _.filter( items, function( item ) {
  33. var search_text =
  34. item.name +
  35. ' ' +
  36. item.description +
  37. ' ' +
  38. item.long_description +
  39. ' ' +
  40. item.search_terms +
  41. ' ' +
  42. item.module_tags;
  43. return -1 !== search_text.toLowerCase().indexOf( m_search );
  44. } );
  45. }
  46. if ( m_sort.data( 'sort-by' ) ) {
  47. items = _.sortBy( items, m_sort.data( 'sort-by' ) );
  48. if ( 'reverse' === m_sort.data( 'sort-order' ) ) {
  49. items.reverse();
  50. }
  51. }
  52. // Sort unavailable modules to the end if the user is running in local mode.
  53. groups = _.groupBy( items, 'available' );
  54. if ( _.has( groups, 'false' ) ) {
  55. items = [].concat( groups[ true ], groups[ false ] );
  56. }
  57. // Now shove it back in.
  58. this.set( 'items', items );
  59. return this;
  60. },
  61. initialize: function() {
  62. var items = this.get( 'items' );
  63. this.set( 'raw', items );
  64. },
  65. } );
  66. return models;
  67. } )( this, jQuery, _, Backbone );