| 1 |
<?php |
| 2 |
/** |
| 3 |
* Template choose quiz question items. |
| 4 |
* |
| 5 |
* @since 3.0.0 |
| 6 |
*/ |
| 7 |
|
| 8 |
learn_press_admin_view( 'quiz/added-items-preview' ); |
| 9 |
learn_press_admin_view( 'quiz/pagination' ); |
| 10 |
?> |
| 11 |
|
| 12 |
<script type="text/x-template" id="tmpl-lp-quiz-choose-item"> |
| 13 |
<li class="question-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 |
jQuery( function($) { |
| 21 |
var $Vue = window.$Vue || Vue; |
| 22 |
var $store = window.LP_Quiz_Store; |
| 23 |
|
| 24 |
$Vue.component('lp-quiz-choose-item', { |
| 25 |
template: '#tmpl-lp-quiz-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 |
}); |
| 46 |
</script> |
| 47 |
|
| 48 |
<script type="text/x-template" id="tmpl-lp-quiz-choose-items"> |
| 49 |
<div id="lp-modal-choose-items" :class="{show:show, loading: loading}"> |
| 50 |
<div class="lp-choose-items" :class="{'show-preview': showPreview}"> |
| 51 |
<div class="header"> |
| 52 |
<div class="preview-title"><span><?php esc_html_e( 'Selected items', 'learnpress' ); ?> |
| 53 |
({{addedItems.length}})</span></div> |
| 54 |
<ul class="tabs"> |
| 55 |
<li class="tab active"><a href="#"><?php esc_html_e( 'Questions', 'learnpress' ); ?></a></li> |
| 56 |
</ul> |
| 57 |
<a class="close" @click="close"> |
| 58 |
<span class="dashicons dashicons-no-alt" title="<?php esc_attr_e( 'Close', 'learnpress' ); ?>"></span> |
| 59 |
</a> |
| 60 |
</div> |
| 61 |
|
| 62 |
<div class="main"> |
| 63 |
<form class="search" @submit.prevent=""> |
| 64 |
<input type="text" class="modal-search-input" placeholder="<?php esc_attr_e( 'Type here to search for the question', 'learnpress' ); ?>" @input="onChangeQuery" v-model="query"> |
| 65 |
</form> |
| 66 |
|
| 67 |
<ul class="list-items"> |
| 68 |
<template v-if="!items.length"> |
| 69 |
<div><?php esc_html_e( 'No item found.', 'learnpress' ); ?></div> |
| 70 |
</template> |
| 71 |
<template v-for="item in items"> |
| 72 |
<lp-quiz-choose-item :added="item.added" :item="item" @add="addItem" @remove="removeItem"></lp-quiz-choose-item> |
| 73 |
</template> |
| 74 |
</ul> |
| 75 |
|
| 76 |
<lp-quiz-pagination :total="totalPage" @update="changePage"></lp-quiz-pagination> |
| 77 |
<lp-quiz-added-items-preview :show="showPreview"></lp-quiz-added-items-preview> |
| 78 |
</div> |
| 79 |
|
| 80 |
<div class="footer"> |
| 81 |
<div class="cart"> |
| 82 |
<button type="button" class="button button-primary checkout" @click="checkout" :disabled="!addedItems.length || adding"> |
| 83 |
<span v-if="!adding"><?php esc_html_e( 'Add', 'learnpress' ); ?></span> |
| 84 |
<span v-if="adding"><?php esc_html_e( 'Adding', 'learnpress' ); ?></span> |
| 85 |
</button> |
| 86 |
|
| 87 |
<button type="button" class="button button-secondary edit-selected" @click.prevent="showPreview = !showPreview" :disabled="adding || (!addedItems.length && !showPreview)"> |
| 88 |
{{editCartButton}} |
| 89 |
</button> |
| 90 |
</div> |
| 91 |
</div> |
| 92 |
</div> |
| 93 |
</div> |
| 94 |
</script> |
| 95 |
|
| 96 |
<script> |
| 97 |
jQuery( function($) { |
| 98 |
var $Vue = window.$Vue || Vue; |
| 99 |
var $store = window.LP_Quiz_Store; |
| 100 |
|
| 101 |
$Vue.component('lp-quiz-choose-items', { |
| 102 |
template: '#tmpl-lp-quiz-choose-items', |
| 103 |
data: function () { |
| 104 |
return { |
| 105 |
query: '', |
| 106 |
page: 1, |
| 107 |
delayTimeOut: null, |
| 108 |
showPreview: false, |
| 109 |
adding: false |
| 110 |
} |
| 111 |
}, |
| 112 |
created: function () { |
| 113 |
var vm = this; |
| 114 |
|
| 115 |
$store.subscribe(function (mutation) { |
| 116 |
if (!mutation || mutation.type !== 'cqi/TOGGLE') { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
if (vm.show) { |
| 121 |
vm.init(); |
| 122 |
|
| 123 |
$('body').addClass('lp-quiz-modal-choose-items-open'); |
| 124 |
} else { |
| 125 |
$('body').removeClass('lp-quiz-modal-choose-item-open'); |
| 126 |
} |
| 127 |
}) |
| 128 |
}, |
| 129 |
mounted: function () { |
| 130 |
|
| 131 |
}, |
| 132 |
computed: { |
| 133 |
status: function () { |
| 134 |
return $store.getters['cqi/status']; |
| 135 |
}, |
| 136 |
loading: function () { |
| 137 |
return this.status === 'loading'; |
| 138 |
}, |
| 139 |
editCartButton: function () { |
| 140 |
if (this.showPreview) { |
| 141 |
return $store.getters['i18n/all'].back; |
| 142 |
} |
| 143 |
|
| 144 |
return $store.getters['i18n/all'].selected_items + ' (' + this.addedItems.length + ')'; |
| 145 |
}, |
| 146 |
addedItems: function () { |
| 147 |
return $store.getters['cqi/addedItems']; |
| 148 |
}, |
| 149 |
pagination: function () { |
| 150 |
return $store.getters['cqi/pagination']; |
| 151 |
}, |
| 152 |
totalPage: function () { |
| 153 |
if (this.pagination) { |
| 154 |
return parseInt(this.pagination.total) || 1; |
| 155 |
} |
| 156 |
}, |
| 157 |
items: function () { |
| 158 |
return $store.getters['cqi/items']; |
| 159 |
}, |
| 160 |
show: function () { |
| 161 |
var isShow = $store.getters['cqi/isOpen']; |
| 162 |
|
| 163 |
isShow && this.focusInput(); |
| 164 |
|
| 165 |
return isShow; |
| 166 |
}, |
| 167 |
// check new quiz |
| 168 |
new_quiz: function () { |
| 169 |
return $store.getters['autoDraft']; |
| 170 |
} |
| 171 |
}, |
| 172 |
methods: { |
| 173 |
init: function () { |
| 174 |
this.query = ''; |
| 175 |
this.page = 1; |
| 176 |
this.showPreview = false; |
| 177 |
this.adding = false; |
| 178 |
this.makeSearch(); |
| 179 |
}, |
| 180 |
focusInput: function () { |
| 181 |
var $input = $(this.$el).find('.main .search input[type="text"]').focus(); |
| 182 |
setTimeout(function () { |
| 183 |
$input.focus(); |
| 184 |
}, 300) |
| 185 |
}, |
| 186 |
checkout: function () { |
| 187 |
this.adding = true; |
| 188 |
this.$emit('addItems', this.page); |
| 189 |
}, |
| 190 |
changePage: function (page) { |
| 191 |
if (page === this.page) { |
| 192 |
return; |
| 193 |
} |
| 194 |
this.page = page; |
| 195 |
this.makeSearch(); |
| 196 |
}, |
| 197 |
addItem: function(item) { |
| 198 |
$store.dispatch('cqi/addItem', item); |
| 199 |
}, |
| 200 |
|
| 201 |
removeItem: function(item) { |
| 202 |
$store.dispatch('cqi/removeItem', item); |
| 203 |
}, |
| 204 |
|
| 205 |
close: function() { |
| 206 |
$store.dispatch('cqi/toggle'); |
| 207 |
}, |
| 208 |
|
| 209 |
onChangeQuery: function() { |
| 210 |
var vm = this; |
| 211 |
|
| 212 |
if ( this.delayTimeOut ) { |
| 213 |
clearTimeout( this.delayTimeOut ); |
| 214 |
} |
| 215 |
|
| 216 |
this.delayTimeOut = setTimeout( function() { |
| 217 |
vm.page = 1; |
| 218 |
vm.makeSearch(); |
| 219 |
}, 500 ); |
| 220 |
}, |
| 221 |
|
| 222 |
makeSearch: function() { |
| 223 |
$store.dispatch('cqi/searchItems', { |
| 224 |
query: this.query, |
| 225 |
page: this.page |
| 226 |
}); |
| 227 |
} |
| 228 |
} |
| 229 |
}); |
| 230 |
}); |
| 231 |
</script> |
| 232 |
|