| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template choose items. |
| 4 |
* |
| 5 |
* @since 3.0.0 |
| 6 |
*/ |
| 7 |
|
| 8 |
learn_press_admin_view( 'course/added-items-preview' ); |
| 9 |
learn_press_admin_view( 'course/pagination' ); |
| 10 |
?> |
| 11 |
|
| 12 |
<script type="text/x-template" id="tmpl-lp-course-choose-item"> |
| 13 |
<li class="section-item" :class="[item.type, item.added ? 'added' : 'addable']" @click="add"> |
| 14 |
<input type="checkbox" :checked="item.added === true"> |
| 15 |
<span class="title">{{item.title}} <strong>(#{{item.id}})</strong></span> |
| 16 |
</li> |
| 17 |
</script> |
| 18 |
|
| 19 |
<script type="text/javascript"> |
| 20 |
window.$Vue = window.$Vue || Vue; |
| 21 |
|
| 22 |
jQuery( function( $ ) { |
| 23 |
( function( $store ) { |
| 24 |
$Vue.component( 'lp-course-choose-item', { |
| 25 |
template: '#tmpl-lp-course-choose-item', |
| 26 |
props: ['item', 'added'], |
| 27 |
watch: { |
| 28 |
added: function() { |
| 29 |
this.$forceUpdate(); |
| 30 |
} |
| 31 |
}, |
| 32 |
methods: { |
| 33 |
add: function() { |
| 34 |
if ( this.item.added ) { |
| 35 |
return this.remove(); |
| 36 |
} |
| 37 |
|
| 38 |
this.$emit( 'add', this.item ); |
| 39 |
}, |
| 40 |
remove: function() { |
| 41 |
this.$emit( 'remove', this.item ); |
| 42 |
} |
| 43 |
} |
| 44 |
}); |
| 45 |
})(LP_Curriculum_Store); |
| 46 |
}); |
| 47 |
</script> |
| 48 |
|
| 49 |
<script type="text/x-template" id="tmpl-lp-course-choose-items"> |
| 50 |
<div id="lp-modal-choose-items" :class="{show:show, loading: loading}"> |
| 51 |
<div class="lp-choose-items" :class="{'show-preview': showPreview}"> |
| 52 |
<div class="header"> |
| 53 |
<div class="preview-title"> |
| 54 |
<span><?php esc_html_e( 'Selected items', 'learnpress' ); ?> ({{addedItems.length}})</span> |
| 55 |
</div> |
| 56 |
|
| 57 |
<ul class="tabs"> |
| 58 |
<template v-for="(type, key) in types"> |
| 59 |
<li :data-type="key" :class="['tab', key === tab ? 'active': 'inactive']" @click.prevent="changeTab(key)"> |
| 60 |
<a href="#" @click.prevent="">{{type}}</a> |
| 61 |
</li> |
| 62 |
</template> |
| 63 |
</ul> |
| 64 |
|
| 65 |
<a class="close" @click="close"> |
| 66 |
<span class="dashicons dashicons-no-alt" title="<?php esc_attr_e( 'Close', 'learnpress' ); ?>"></span> |
| 67 |
</a> |
| 68 |
</div> |
| 69 |
<div class="main"> |
| 70 |
<form class="search" @submit.prevent=""> |
| 71 |
<input type="text" class="modal-search-input" placeholder="<?php esc_attr_e( 'Type here to search for an item', 'learnpress' ); ?>" title="search" @input="onChangeQuery" v-model="query"> |
| 72 |
</form> |
| 73 |
|
| 74 |
<ul class="list-items"> |
| 75 |
<template v-if="!items.length"> |
| 76 |
<div><?php esc_html_e( 'No item found.', 'learnpress' ); ?></div> |
| 77 |
</template> |
| 78 |
|
| 79 |
<template v-for="item in items"> |
| 80 |
<lp-course-choose-item @add="addItem" @remove="removeItem" :added="item.added" :item="item"></lp-course-choose-item> |
| 81 |
</template> |
| 82 |
</ul> |
| 83 |
|
| 84 |
<lp-pagination :total="totalPage" @update="changePage"></lp-pagination> |
| 85 |
<lp-added-items-preview :show="showPreview"></lp-added-items-preview> |
| 86 |
</div> |
| 87 |
|
| 88 |
<div class="footer"> |
| 89 |
<div class="cart"> |
| 90 |
<button type="button" class="button button-primary checkout" |
| 91 |
@click="checkout" :disabled="!addedItems.length || adding"> |
| 92 |
<span v-if="!adding"><?php esc_html_e( 'Add', 'learnpress' ); ?></span> |
| 93 |
<span v-if="adding"><?php esc_html_e( 'Adding', 'learnpress' ); ?></span> |
| 94 |
</button> |
| 95 |
|
| 96 |
<button type="button" class="button button-secondary edit-selected" |
| 97 |
:disabled="adding || (!addedItems.length && !showPreview)" |
| 98 |
@click.prevent="showPreview = !showPreview"> |
| 99 |
{{textButtonEdit}} |
| 100 |
</button> |
| 101 |
</div> |
| 102 |
</div> |
| 103 |
</div> |
| 104 |
</div> |
| 105 |
</script> |
| 106 |
|
| 107 |
<script type="text/javascript"> |
| 108 |
window.$Vue = window.$Vue || Vue; |
| 109 |
|
| 110 |
jQuery( function( $ ) { |
| 111 |
( function( $store ) { |
| 112 |
$Vue.component( 'lp-curriculum-choose-items', { |
| 113 |
template: '#tmpl-lp-course-choose-items', |
| 114 |
data: function() { |
| 115 |
return { |
| 116 |
query: '', |
| 117 |
page: 1, |
| 118 |
tab: 'lp_lesson', |
| 119 |
delayTimeout: null, |
| 120 |
showPreview: false, |
| 121 |
adding: false |
| 122 |
}; |
| 123 |
}, |
| 124 |
mounted: function() { |
| 125 |
}, |
| 126 |
created: function() { |
| 127 |
var vm = this; |
| 128 |
|
| 129 |
$store.subscribe( function( mutation ) { |
| 130 |
if ( ! mutation || mutation.type !== 'ci/TOGGLE' ) { |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
if ( vm.show ) { |
| 135 |
vm.init(); |
| 136 |
|
| 137 |
$( 'body' ).addClass( 'lp-modal-choose-items-open' ); |
| 138 |
} else { |
| 139 |
$( 'body' ).removeClass( 'lp-modal-choose-items-open' ); |
| 140 |
} |
| 141 |
}); |
| 142 |
}, |
| 143 |
computed: { |
| 144 |
status: function() { |
| 145 |
return $store.getters['ci/status']; |
| 146 |
}, |
| 147 |
loading: function() { |
| 148 |
return this.status === 'loading'; |
| 149 |
}, |
| 150 |
textButtonEdit: function() { |
| 151 |
if ( this.showPreview ) { |
| 152 |
return $store.getters['i18n/all'].back; |
| 153 |
} |
| 154 |
|
| 155 |
return $store.getters['i18n/all'].selected_items + ' (' + this.addedItems.length + ')'; |
| 156 |
}, |
| 157 |
addedItems: function() { |
| 158 |
return $store.getters['ci/addedItems']; |
| 159 |
}, |
| 160 |
pagination: function() { |
| 161 |
return $store.getters['ci/pagination']; |
| 162 |
}, |
| 163 |
totalPage: function() { |
| 164 |
if ( this.pagination ) { |
| 165 |
return parseInt(this.pagination.total) || 1; |
| 166 |
} |
| 167 |
}, |
| 168 |
items: function() { |
| 169 |
return $store.getters['ci/items']; |
| 170 |
}, |
| 171 |
show: function() { |
| 172 |
var isShow = $store.getters['ci/isOpen']; |
| 173 |
|
| 174 |
if ( isShow ) { |
| 175 |
this.focusInput(); |
| 176 |
} |
| 177 |
|
| 178 |
return isShow; |
| 179 |
}, |
| 180 |
types: function() { |
| 181 |
return $store.getters['ci/types']; |
| 182 |
}, |
| 183 |
firstType: function() { |
| 184 |
for ( var type in $store.getters['ci/types'] ) { |
| 185 |
return type; |
| 186 |
} |
| 187 |
|
| 188 |
return false; |
| 189 |
} |
| 190 |
}, |
| 191 |
methods: { |
| 192 |
init: function() { |
| 193 |
this.query = ''; |
| 194 |
this.page = 1; |
| 195 |
this.tab = this.firstType; |
| 196 |
this.showPreview = false; |
| 197 |
this.adding = false; |
| 198 |
this.makeSearch(); |
| 199 |
}, |
| 200 |
focusInput: function() { |
| 201 |
var $input = $( this.$el ).find( '.main .search input[type="text"]' ).focus(); |
| 202 |
setTimeout( function () { |
| 203 |
$input.focus(); |
| 204 |
}, 300 ); |
| 205 |
}, |
| 206 |
checkout: function() { |
| 207 |
this.adding = true; |
| 208 |
$store.dispatch( 'ci/addItemsToSection' ); |
| 209 |
}, |
| 210 |
changePage: function( page ) { |
| 211 |
if ( page === this.page ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
this.page = page; |
| 216 |
this.makeSearch(); |
| 217 |
}, |
| 218 |
addItem: function( item ) { |
| 219 |
$store.dispatch( 'ci/addItem', item ); |
| 220 |
}, |
| 221 |
removeItem: function( item ) { |
| 222 |
$store.dispatch( 'ci/removeItem', item ); |
| 223 |
}, |
| 224 |
close: function() { |
| 225 |
$store.dispatch( 'ci/toggle' ); |
| 226 |
}, |
| 227 |
changeTab: function( key ) { |
| 228 |
if ( key === this.tab ) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
this.tab = key; |
| 233 |
this.page = 1; |
| 234 |
this.makeSearch(); |
| 235 |
this.focusInput(); |
| 236 |
}, |
| 237 |
onChangeQuery: function() { |
| 238 |
var vm = this; |
| 239 |
|
| 240 |
if ( this.delayTimeout ) { |
| 241 |
clearTimeout( this.delayTimeout ); |
| 242 |
} |
| 243 |
|
| 244 |
this.delayTimeout = setTimeout( function() { |
| 245 |
vm.page = 1; |
| 246 |
vm.makeSearch(); |
| 247 |
}, 500 ); |
| 248 |
}, |
| 249 |
|
| 250 |
makeSearch: function() { |
| 251 |
$store.dispatch( 'ci/searchItems', { |
| 252 |
query: this.query, |
| 253 |
page: this.page, |
| 254 |
type: this.tab |
| 255 |
}); |
| 256 |
} |
| 257 |
} |
| 258 |
}); |
| 259 |
|
| 260 |
})( LP_Curriculum_Store ); |
| 261 |
}); |
| 262 |
</script> |
| 263 |
|