blocks
2 days ago
build
2 days ago
content-guidelines-ai
2 days ago
fonts
1 year ago
genericons
4 months ago
lib
2 days ago
accessible-focus.js
5 years ago
blogging-prompts.php
1 week ago
class.jetpack-provision.php
7 months ago
content-guidelines-ai.js
1 week ago
content-guidelines-ai.php
1 week ago
crowdsignal-shortcode.js
1 year ago
crowdsignal-survey.js
5 years ago
deprecate.js
7 months ago
facebook-embed.js
4 years ago
gallery-settings.js
5 years ago
genericons.php
1 year ago
jetpack-admin.js
3 years ago
jetpack-deactivate-dialog.js
1 year ago
jetpack-modules.js
1 year ago
jetpack-modules.models.js
7 months ago
jetpack-modules.views.js
7 months ago
polldaddy-shortcode.js
3 months ago
site-switcher-endpoint.php
5 months ago
site-switcher.js
5 months ago
site-switcher.php
5 months ago
social-logos.php
3 months ago
twitter-timeline.js
1 month ago
jetpack-modules.models.js
77 lines
| 1 | window.jetpackModules = window.jetpackModules || {}; |
| 2 | |
| 3 | window.jetpackModules.models = ( function ( window, $, Backbone ) { |
| 4 | 'use strict'; |
| 5 | |
| 6 | var models = {}; |
| 7 | |
| 8 | models.Modules = Backbone.Model.extend( { |
| 9 | visibles: {}, |
| 10 | |
| 11 | /** |
| 12 | * Updates modules.items dataset to be a reflection of both the current |
| 13 | * modules.raw data, as well as any filters or sorting that may be in effect. |
| 14 | */ |
| 15 | filter_and_sort: function () { |
| 16 | var subsubsub = $( '.subsubsub .current a' ), |
| 17 | items = Object.values( this.get( 'raw' ) ), |
| 18 | m_filter = $( '.button-group.filter-active .active' ), |
| 19 | m_sort = $( '.button-group.sort .active' ), |
| 20 | m_search = $( '#srch-term-search-input' ).val().toLowerCase(); |
| 21 | |
| 22 | // If a module filter has been selected, filter it! |
| 23 | if ( ! subsubsub.closest( 'li' ).hasClass( 'all' ) ) { |
| 24 | items = items.filter( item => item.module_tags.includes( subsubsub.data( 'title' ) ) ); |
| 25 | } |
| 26 | |
| 27 | if ( m_filter.data( 'filter-by' ) ) { |
| 28 | items = items.filter( |
| 29 | item => item[ m_filter.data( 'filter-by' ) ] === m_filter.data( 'filter-value' ) |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | if ( m_search.length ) { |
| 34 | items = items.filter( function ( item ) { |
| 35 | var search_text = |
| 36 | item.name + |
| 37 | ' ' + |
| 38 | item.description + |
| 39 | ' ' + |
| 40 | item.long_description + |
| 41 | ' ' + |
| 42 | item.search_terms + |
| 43 | ' ' + |
| 44 | item.module_tags; |
| 45 | return -1 !== search_text.toLowerCase().indexOf( m_search ); |
| 46 | } ); |
| 47 | } |
| 48 | |
| 49 | if ( m_sort.data( 'sort-by' ) ) { |
| 50 | const key = m_sort.data( 'sort-by' ); |
| 51 | const cmpret = 'reverse' === m_sort.data( 'sort-order' ) ? -1 : 1; |
| 52 | |
| 53 | items.sort( ( a, b ) => |
| 54 | // eslint-disable-next-line no-nested-ternary |
| 55 | a[ key ] > b[ key ] ? cmpret : a[ key ] < b[ key ] ? -cmpret : 0 |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | // Sort unavailable modules to the end if the user is running in local mode. |
| 60 | // JS sort is supposed to be stable since 2019, and is in browsers we care about, so this is safe. |
| 61 | items.sort( ( a, b ) => b.available - a.available ); |
| 62 | |
| 63 | // Now shove it back in. |
| 64 | this.set( 'items', items ); |
| 65 | |
| 66 | return this; |
| 67 | }, |
| 68 | |
| 69 | initialize: function () { |
| 70 | var items = this.get( 'items' ); |
| 71 | this.set( 'raw', items ); |
| 72 | }, |
| 73 | } ); |
| 74 | |
| 75 | return models; |
| 76 | } )( window, jQuery, Backbone ); |
| 77 |