| 1 |
( function( $ ) { |
| 2 |
$( function() { |
| 3 |
const _ = window._; |
| 4 |
window.$Vue = window.$Vue || window.Vue; |
| 5 |
const $VueHTTP = $Vue ? $Vue.http : false; |
| 6 |
document.getElementById( 'vue-modal-search-users' ) && $Vue && ( function() { |
| 7 |
$Vue.component( 'learn-press-modal-search-users', { |
| 8 |
template: '#learn-press-modal-search-users', |
| 9 |
data: function data() { |
| 10 |
return { |
| 11 |
paged: 1, |
| 12 |
term: '', |
| 13 |
hasUsers: false, |
| 14 |
selected: [], |
| 15 |
}; |
| 16 |
}, |
| 17 |
watch: { |
| 18 |
show: function show( value ) { |
| 19 |
if ( value ) { |
| 20 |
$( this.$refs.search ).trigger( 'focus' ); |
| 21 |
} |
| 22 |
}, |
| 23 |
}, |
| 24 |
props: [ 'multiple', 'context', 'contextId', 'show', 'callbacks', 'textFormat', 'exclude' ], |
| 25 |
created: function created() {}, |
| 26 |
methods: { |
| 27 |
doSearch: function doSearch( e ) { |
| 28 |
this.term = e.target.value; |
| 29 |
this.paged = 1; |
| 30 |
this.search(); |
| 31 |
}, |
| 32 |
search: _.debounce( function( term ) { |
| 33 |
const that = this; |
| 34 |
$VueHTTP.post( window.location.href, { |
| 35 |
type: this.postType, |
| 36 |
context: this.context, |
| 37 |
context_id: this.contextId, |
| 38 |
term: term || this.term, |
| 39 |
paged: this.paged, |
| 40 |
multiple: this.multiple ? 'yes' : 'no', |
| 41 |
text_format: this.textFormat, |
| 42 |
exclude: this.exclude, |
| 43 |
'lp-ajax': 'modal_search_users', |
| 44 |
}, { |
| 45 |
emulateJSON: true, |
| 46 |
params: {}, |
| 47 |
} ).then( function( response ) { |
| 48 |
const result = LP.parseJSON( response.body || response.bodyText ); |
| 49 |
that.hasUsers = !! _.size( result.users ); |
| 50 |
$( that.$el ).find( '.search-results' ).html( result.html ).find( 'input[type="checkbox"]' ).each( function() { |
| 51 |
const id = parseInt( $( this ).val() ); |
| 52 |
|
| 53 |
if ( _.indexOf( that.selected, id ) >= 0 ) { |
| 54 |
this.checked = true; |
| 55 |
} |
| 56 |
} ); |
| 57 |
|
| 58 |
_.debounce( function() { |
| 59 |
$( that.$el ).find( '.search-nav' ).html( result.nav ).find( 'a, span' ).addClass( 'button' ).filter( 'span' ).addClass( 'disabled' ); |
| 60 |
}, 10 )(); |
| 61 |
} ); |
| 62 |
}, 500 ), |
| 63 |
loadPage: function loadPage( e ) { |
| 64 |
e.preventDefault(); |
| 65 |
const $button = $( e.target ); |
| 66 |
|
| 67 |
if ( $button.is( 'span' ) ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
if ( $button.hasClass( 'next' ) ) { |
| 72 |
this.paged++; |
| 73 |
} else if ( $button.hasClass( 'prev' ) ) { |
| 74 |
this.paged--; |
| 75 |
} else { |
| 76 |
const paged = $button.html(); |
| 77 |
this.paged = parseInt( paged ); |
| 78 |
} |
| 79 |
|
| 80 |
this.search(); |
| 81 |
}, |
| 82 |
selectItem: function selectItem( e ) { |
| 83 |
const $select = $( e.target ).closest( 'li' ), |
| 84 |
$chk = $select.find( 'input[type="checkbox"]' ), |
| 85 |
id = parseInt( $chk.val() ), |
| 86 |
//pos = _.indexOf(this.selected, id), |
| 87 |
pos = _.findLastIndex( this.selected, { |
| 88 |
id, |
| 89 |
} ); |
| 90 |
|
| 91 |
if ( this.multiple ) { |
| 92 |
if ( $chk.is( ':checked' ) ) { |
| 93 |
if ( pos === -1 ) { |
| 94 |
this.selected.push( $select.closest( 'li' ).data( 'data' ) ); |
| 95 |
} |
| 96 |
} else if ( pos >= 0 ) { |
| 97 |
this.selected.splice( pos, 1 ); |
| 98 |
} |
| 99 |
} else { |
| 100 |
e.preventDefault(); |
| 101 |
this.selected = [ $select.closest( 'li' ).data( 'data' ) ]; |
| 102 |
this.addUsers(); |
| 103 |
} |
| 104 |
}, |
| 105 |
addUsers: function addUsers() { |
| 106 |
const $els = $( this.$el ).find( '.lp-result-item' ); |
| 107 |
|
| 108 |
if ( this.callbacks && this.callbacks.addUsers ) { |
| 109 |
this.callbacks.addUsers.call( this, this.selected ); |
| 110 |
} |
| 111 |
|
| 112 |
$( document ).triggerHandler( 'learn-press/modal-add-users', this.selected ); |
| 113 |
}, |
| 114 |
close: function close() { |
| 115 |
this.$emit( 'close' ); |
| 116 |
}, |
| 117 |
}, |
| 118 |
} ); |
| 119 |
window.LP.$modalSearchUsers = new $Vue( { |
| 120 |
el: '#vue-modal-search-users', |
| 121 |
data: { |
| 122 |
show: false, |
| 123 |
term: '', |
| 124 |
multiple: false, |
| 125 |
callbacks: {}, |
| 126 |
textFormat: '{{display_name}} ({{email}})', |
| 127 |
exclude: 0, |
| 128 |
}, |
| 129 |
methods: { |
| 130 |
open: function open( options ) { |
| 131 |
_.each( options.data, function( v, k ) { |
| 132 |
this[ k ] = v; |
| 133 |
}, this ); |
| 134 |
|
| 135 |
this.callbacks = options.callbacks; |
| 136 |
this.focusSearch(); |
| 137 |
}, |
| 138 |
close: function close() { |
| 139 |
this.show = false; |
| 140 |
}, |
| 141 |
focusSearch: _.debounce( function() { |
| 142 |
$( 'input[name="search"]', this.$el ).trigger( 'focus' ); |
| 143 |
}, 200 ), |
| 144 |
}, |
| 145 |
} ); |
| 146 |
}() ); |
| 147 |
} ); |
| 148 |
}( jQuery ) ); |
| 149 |
|
| 150 |
|