PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.3
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / _inc / jetpack-modules.views.js

jetpack-modules.views.js in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.3, at _inc/jetpack-modules.views.js

105 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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