| 1 |
window.jetpackModules = window.jetpackModules || {}; |
| 2 |
|
| 3 |
window.jetpackModules.views = ( function ( window, $, Backbone, wp ) { |
| 4 |
'use strict'; |
| 5 |
|
| 6 |
var views = {}; |
| 7 |
|
| 8 |
views.List_Table = Backbone.View.extend( { |
| 9 |
template: wp.template( 'Jetpack_Modules_List_Table_Template' ), |
| 10 |
|
| 11 |
/** |
| 12 |
* If we can, use replaceState to change the URL and indicate the new filtering. |
| 13 |
* This will be handy with redirecting back to the same state after activating/deactivating. |
| 14 |
*/ |
| 15 |
updateUrl: function () { |
| 16 |
if ( ! window.history.replaceState ) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
var url = window.location.href.split( '?' )[ 0 ] + '?page=jetpack_modules', |
| 21 |
m_tag = $( '.subsubsub .current' ), |
| 22 |
m_purpose = $( '.purpose-filter .current' ), |
| 23 |
m_availability = $( '.button-group.availability-filter .active' ), |
| 24 |
m_search = $( '#srch-term-search-input' ).val(); |
| 25 |
|
| 26 |
if ( m_search.length ) { |
| 27 |
url += '&s=' + encodeURIComponent( m_search ); |
| 28 |
} |
| 29 |
|
| 30 |
// "Hide unavailable" is the default in Offline Mode, so only the opt-out |
| 31 |
// ("All") needs to be persisted in the URL. |
| 32 |
if ( 'all' === m_availability.data( 'availability' ) ) { |
| 33 |
url += '&offline_available=all'; |
| 34 |
} |
| 35 |
|
| 36 |
if ( ! m_tag.hasClass( 'all' ) ) { |
| 37 |
url += '&module_tag=' + encodeURIComponent( m_tag.find( 'a' ).data( 'title' ) ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( m_purpose.length && ! m_purpose.hasClass( 'all' ) ) { |
| 41 |
url += '&product_group=' + encodeURIComponent( m_purpose.find( 'a' ).data( 'group' ) ); |
| 42 |
} |
| 43 |
|
| 44 |
// State facet (segmented buttons). "All" carries no data-filter-by. |
| 45 |
$( '.button-group.module-filter .active' ).each( function () { |
| 46 |
var by = $( this ).data( 'filter-by' ), |
| 47 |
value = $( this ).data( 'filter-value' ); |
| 48 |
|
| 49 |
if ( by ) { |
| 50 |
url += '&' + encodeURIComponent( by ) + '=' + encodeURIComponent( value ); |
| 51 |
} |
| 52 |
} ); |
| 53 |
|
| 54 |
window.history.replaceState( {}, '', url ); |
| 55 |
}, |
| 56 |
|
| 57 |
/** |
| 58 |
* Writes faceted counts into a facet list (the tag list or the Purpose |
| 59 |
* list), and greys out entries that have no matches under the current |
| 60 |
* filters. The "All" entry always shows the total and is never disabled. |
| 61 |
* |
| 62 |
* @param {string} selector Facet list selector (e.g. '.subsubsub'). |
| 63 |
* @param {Object} counts Map of key to count, plus `__all__`. |
| 64 |
* @param {string} dataKey data-* attribute on each link holding its key. |
| 65 |
*/ |
| 66 |
updateFacetCounts: function ( selector, counts, dataKey ) { |
| 67 |
$( selector + ' > li' ).each( function () { |
| 68 |
var $li = $( this ), |
| 69 |
isAll = $li.hasClass( 'all' ), |
| 70 |
key = $li.children( 'a' ).data( dataKey ), |
| 71 |
n = isAll ? counts.__all__ : counts[ key ] || 0; |
| 72 |
|
| 73 |
$li.find( '.count' ).text( '(' + n + ')' ); |
| 74 |
|
| 75 |
if ( ! isAll ) { |
| 76 |
$li.toggleClass( 'is-empty', 0 === n ); |
| 77 |
} |
| 78 |
} ); |
| 79 |
}, |
| 80 |
|
| 81 |
/** |
| 82 |
* Refreshes both facet lists ("Show" tags and "Purpose") so their counts |
| 83 |
* reflect the currently-active filters. |
| 84 |
*/ |
| 85 |
updateCounts: function () { |
| 86 |
this.updateFacetCounts( '.subsubsub', this.model.tag_counts(), 'title' ); |
| 87 |
this.updateFacetCounts( '.purpose-filter', this.model.purpose_counts(), 'group' ); |
| 88 |
}, |
| 89 |
|
| 90 |
render: function () { |
| 91 |
this.model.filter_and_sort(); |
| 92 |
this.$el.html( this.template( this.model.attributes ) ); |
| 93 |
this.updateCounts(); |
| 94 |
this.updateUrl(); |
| 95 |
return this; |
| 96 |
}, |
| 97 |
|
| 98 |
initialize: function () { |
| 99 |
this.listenTo( this.model, 'change', this.render ); |
| 100 |
}, |
| 101 |
} ); |
| 102 |
|
| 103 |
return views; |
| 104 |
} )( window, jQuery, Backbone, wp ); |
| 105 |
|