| 1 |
( function( $ ) { |
| 2 |
'use strict'; |
| 3 |
window.$Vue = window.$Vue || Vue; |
| 4 |
|
| 5 |
jQuery( function() { |
| 6 |
// eslint-disable-next-line no-var |
| 7 |
var $listItems = $( '.list-order-items' ).find( 'tbody' ), |
| 8 |
$listUsers = $( '#list-users' ), |
| 9 |
template = function( templateHTML, data ) { |
| 10 |
return _.template( templateHTML, { |
| 11 |
evaluate: /<#([\s\S]+?)#>/g, |
| 12 |
interpolate: /\{\{\{([\s\S]+?)\}\}\}/g, |
| 13 |
escape: /\{\{([^\}]+?)\}\}(?!\})/g, |
| 14 |
} )( data ); |
| 15 |
}, |
| 16 |
advancedListOptions = { |
| 17 |
template: '#tmpl-order-advanced-list-item', |
| 18 |
onRemove() { |
| 19 |
if ( this.$el.children().length === 0 ) { |
| 20 |
this.$el.append( '<li class="user-guest">' + orderOptions.i18n_guest + '</li>' ); |
| 21 |
} |
| 22 |
console.log( this.$el ); |
| 23 |
}, |
| 24 |
onAdd() { |
| 25 |
this.$el.find( '.user-guest' ).remove(); |
| 26 |
}, |
| 27 |
}, |
| 28 |
orderOptions = lpMetaBoxOrderSettings; |
| 29 |
|
| 30 |
function getAddedUsers() { |
| 31 |
return $( '#list-users' ).children().map( function() { |
| 32 |
return $( this ).data( 'id' ); |
| 33 |
} ).get(); |
| 34 |
} |
| 35 |
|
| 36 |
function getAddedItems() { |
| 37 |
return $( '.list-order-items tbody' ).children( '.order-item-row' ).map( function() { |
| 38 |
return $( this ).data( 'id' ); |
| 39 |
} ).get(); |
| 40 |
} |
| 41 |
|
| 42 |
if ( $listUsers.length ) { |
| 43 |
$listUsers.LP( 'AdvancedList', advancedListOptions ); |
| 44 |
if ( orderOptions.users ) { |
| 45 |
_.forEach( orderOptions.users, function( userData, userId ) { |
| 46 |
$listUsers.LP( 'AdvancedList', 'add', [ |
| 47 |
template( orderOptions.userTextFormat, userData ), |
| 48 |
userId, |
| 49 |
] ); |
| 50 |
} ); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
$listItems.on( 'click', '.remove-order-item', function( e ) { |
| 55 |
e.preventDefault(); |
| 56 |
const $item = $( this ).closest( 'tr' ), |
| 57 |
item_id = $item.data( 'item_id' ); |
| 58 |
|
| 59 |
$item.remove(); |
| 60 |
if ( $listItems.children( ':not(.no-order-items)' ).length === 0 ) { |
| 61 |
$listItems.find( '.no-order-items' ).show(); |
| 62 |
} |
| 63 |
|
| 64 |
$Vue.http.post( |
| 65 |
window.location.href, { |
| 66 |
order_id: $( '#post_ID' ).val(), |
| 67 |
items: [ item_id ], |
| 68 |
'lp-ajax': 'remove_items_from_order', |
| 69 |
remove_nonce: $( this ).closest( '.order-item-row' ).data( 'remove_nonce' ), |
| 70 |
}, { |
| 71 |
emulateJSON: true, |
| 72 |
params: {}, |
| 73 |
} |
| 74 |
).then( function( response ) { |
| 75 |
const result = LP.parseJSON( response.body || response.bodyText ); |
| 76 |
$( '.order-subtotal' ).html( result.order_data.subtotal_html ); |
| 77 |
$( '.order-total' ).html( result.order_data.total_html ); |
| 78 |
} ); |
| 79 |
} ); |
| 80 |
|
| 81 |
$( '.order-date.date-picker-backendorder' ).on( 'change', function() { |
| 82 |
const m = this.value.split( '-' ); |
| 83 |
[ 'aa', 'mm', 'jj' ].forEach( function( v, k ) { |
| 84 |
$( 'input[name="' + v + '"]' ).val( m[ k ] ); |
| 85 |
} ); |
| 86 |
} ).datepicker( { |
| 87 |
dateFormat: 'yy-mm-dd', |
| 88 |
numberOfMonths: 1, |
| 89 |
showButtonPanel: true, |
| 90 |
select() { |
| 91 |
console.log( arguments ); |
| 92 |
}, |
| 93 |
} ); |
| 94 |
|
| 95 |
$( '#learn-press-add-order-item' ).on( 'click', function() { |
| 96 |
LP.$modalSearchItems.open( { |
| 97 |
data: { |
| 98 |
postType: 'lp_course', |
| 99 |
context: 'order-items', |
| 100 |
contextId: $( '#post_ID' ).val(), |
| 101 |
exclude: getAddedItems(), |
| 102 |
show: true, |
| 103 |
}, |
| 104 |
callbacks: { |
| 105 |
addItems() { |
| 106 |
const that = this; |
| 107 |
$Vue.http.post( |
| 108 |
window.location.href, { |
| 109 |
order_id: this.contextId, |
| 110 |
items: this.selected, |
| 111 |
'lp-ajax': 'add_items_to_order', |
| 112 |
}, { |
| 113 |
emulateJSON: true, |
| 114 |
params: {}, |
| 115 |
} |
| 116 |
).then( function( response ) { |
| 117 |
const result = LP.parseJSON( response.body || response.bodyText ), |
| 118 |
$noItem = $listItems.find( '.no-order-items' ).hide(); |
| 119 |
$( result.item_html ).insertBefore( $noItem ); |
| 120 |
$( '.order-subtotal' ).html( result.order_data.subtotal_html ); |
| 121 |
$( '.order-total' ).html( result.order_data.total_html ); |
| 122 |
} ); |
| 123 |
this.close(); |
| 124 |
}, |
| 125 |
}, |
| 126 |
} ); |
| 127 |
} ); |
| 128 |
|
| 129 |
$( document ).on( 'click', '.change-user', function( e ) { |
| 130 |
e.preventDefault(); |
| 131 |
LP.$modalSearchUsers.open( { |
| 132 |
data: { |
| 133 |
context: 'order-items', |
| 134 |
contextId: $( '#post_ID' ).val(), |
| 135 |
show: true, |
| 136 |
multiple: $( this ).data( 'multiple' ) === 'yes', |
| 137 |
exclude: getAddedUsers(), |
| 138 |
textFormat: orderOptions.userTextFormat, |
| 139 |
}, |
| 140 |
callbacks: { |
| 141 |
addUsers( data ) { |
| 142 |
if ( this.multiple ) { |
| 143 |
if ( ! $listUsers.length ) { |
| 144 |
$listUsers = $( LP.template( 'tmpl-order-data-user' )( { multiple: true } ) ); |
| 145 |
$listUsers.LP( 'AdvancedList', advancedListOptions ); |
| 146 |
|
| 147 |
$( '.order-data-user' ).replaceWith( $listUsers ); |
| 148 |
} |
| 149 |
for ( let i = 0; i < this.selected.length; i++ ) { |
| 150 |
$listUsers.LP( 'AdvancedList', 'add', [ template( this.textFormat, this.selected[ i ] ), this.selected[ i ].id ] ); |
| 151 |
} |
| 152 |
} else { |
| 153 |
const $html = LP.template( 'tmpl-order-data-user' )( { |
| 154 |
name: template( this.textFormat, this.selected[ 0 ] ), |
| 155 |
id: this.selected[ 0 ].id, |
| 156 |
} ); |
| 157 |
|
| 158 |
$( '.order-data-user' ).replaceWith( $html ); |
| 159 |
} |
| 160 |
|
| 161 |
this.close(); |
| 162 |
}, |
| 163 |
}, |
| 164 |
} ); |
| 165 |
} ); |
| 166 |
} ); |
| 167 |
}( jQuery ) ); |
| 168 |
|