| 1 |
CHARITABLE_ADMIN = {}; |
| 2 |
|
| 3 |
CHARITABLE_ADMIN.SetupDatepicker = function( $el ) { |
| 4 |
var $ = jQuery.noConflict(); |
| 5 |
|
| 6 |
$el.datepicker( { |
| 7 |
dateFormat : 'MM d, yy', |
| 8 |
minDate : $(this).data('min-date') || '', |
| 9 |
beforeShow : function( input, inst ) { |
| 10 |
$('#ui-datepicker-div').addClass('charitable-datepicker-table'); |
| 11 |
} |
| 12 |
} ); |
| 13 |
|
| 14 |
$el.each( function(){ |
| 15 |
if ( $(this).data('date') ) { |
| 16 |
$(this).datepicker( 'setDate', $(this).data('date') ); |
| 17 |
} |
| 18 |
|
| 19 |
if ( $(this).data('min-date') ) { |
| 20 |
$(this).datepicker( 'option', 'minDate', $(this).data('min-date') ); |
| 21 |
} |
| 22 |
}); |
| 23 |
}; |
| 24 |
|
| 25 |
( function($){ |
| 26 |
|
| 27 |
var setup_charitable_ajax = function() { |
| 28 |
$('[data-charitable-action]').on( 'click', function( e ){ |
| 29 |
var data = $(this).data( 'charitable-args' ) || {}, |
| 30 |
action = 'charitable-' + $(this).data( 'charitable-action' ); |
| 31 |
|
| 32 |
$.post( ajaxurl, |
| 33 |
{ |
| 34 |
'action' : action, |
| 35 |
'data' : data |
| 36 |
}, |
| 37 |
function( response ) { |
| 38 |
console.log( "Response: " + response ); |
| 39 |
} |
| 40 |
); |
| 41 |
|
| 42 |
return false; |
| 43 |
} ); |
| 44 |
}; |
| 45 |
|
| 46 |
var setup_charitable_toggle = function() { |
| 47 |
$( '[data-charitable-toggle]' ).on( 'click', function( e ){ |
| 48 |
var toggle_id = $(this).data( 'charitable-toggle' ), |
| 49 |
toggle_text = $(this).attr( 'data-charitable-toggle-text' ); |
| 50 |
|
| 51 |
if ( toggle_text && toggle_text.length ) { |
| 52 |
$(this).attr( 'data-charitable-toggle-text', $(this).text() ); |
| 53 |
$(this).text( toggle_text ); |
| 54 |
} |
| 55 |
|
| 56 |
$('#' + toggle_id).toggle(); |
| 57 |
|
| 58 |
return false; |
| 59 |
} ); |
| 60 |
}; |
| 61 |
|
| 62 |
var setup_advanced_meta_box = function() { |
| 63 |
var $meta_box = $('#charitable-campaign-advanced-metabox'); |
| 64 |
|
| 65 |
$meta_box.tabs(); |
| 66 |
|
| 67 |
var min_height = $meta_box.find('.charitable-tabs').height(); |
| 68 |
|
| 69 |
$meta_box.find('.ui-tabs-panel').each( function(){ |
| 70 |
$(this).css( 'min-height', min_height ); |
| 71 |
}); |
| 72 |
}; |
| 73 |
|
| 74 |
var setup_sortable_suggested_donations = function(){ |
| 75 |
$('.charitable-campaign-suggested-donations tbody').sortable({ |
| 76 |
items: "tr:not(.to-copy)", |
| 77 |
handle: ".handle", |
| 78 |
stop: function( event, ui ) { |
| 79 |
reindex_rows(); |
| 80 |
} |
| 81 |
|
| 82 |
}); |
| 83 |
} |
| 84 |
|
| 85 |
var add_suggested_amount_row = function( $button ) { |
| 86 |
var $table = $button.closest( '.charitable-campaign-suggested-donations' ).find('tbody'); |
| 87 |
var $clone = $table.find('tr.to-copy').clone().removeClass('to-copy hidden'); |
| 88 |
$table.find( '.no-suggested-amounts' ).hide(); |
| 89 |
$table.append( $clone ); |
| 90 |
reindex_rows(); |
| 91 |
}; |
| 92 |
|
| 93 |
var delete_suggested_amount_row = function($button) { |
| 94 |
console.log($button); |
| 95 |
$button.closest( 'tr' ).remove(); |
| 96 |
var $table = $button.closest('.charitable-campaign-suggested-donations').find('tbody'); |
| 97 |
if( $table.find( 'tr:not(.to-copy)' ).length == 1 ){ |
| 98 |
$table.find( '.no-suggested-amounts' ).removeClass('hidden').show(); |
| 99 |
} |
| 100 |
reindex_rows(); |
| 101 |
}; |
| 102 |
|
| 103 |
var reindex_rows = function(){ |
| 104 |
$('.charitable-campaign-suggested-donations tbody').each(function(){ |
| 105 |
$(this).children('tr').not('.no-suggested-amounts .to-copy').each(function(index) { |
| 106 |
$(this).data('index', index ); |
| 107 |
$(this).find('input').each(function(i) { |
| 108 |
this.name = this.name.replace(/(\[\d\])/, '[' + index + ']'); |
| 109 |
}); |
| 110 |
}); |
| 111 |
}); |
| 112 |
}; |
| 113 |
|
| 114 |
var setup_dashboard_widgets = function() { |
| 115 |
var $widget = $( '#charitable_dashboard_donations' ); |
| 116 |
|
| 117 |
if ( $widget.length ) { |
| 118 |
$.ajax({ |
| 119 |
type: "GET", |
| 120 |
data: { |
| 121 |
action: 'charitable_load_dashboard_donations_widget' |
| 122 |
}, |
| 123 |
url: ajaxurl, |
| 124 |
success: function (response) { |
| 125 |
$widget.find( '.inside' ).html( response ); |
| 126 |
} |
| 127 |
}); |
| 128 |
} |
| 129 |
}; |
| 130 |
|
| 131 |
$(document).ready( function(){ |
| 132 |
|
| 133 |
if ( $.fn.datepicker ) { |
| 134 |
CHARITABLE_ADMIN.SetupDatepicker( $('.charitable-datepicker') ); |
| 135 |
} |
| 136 |
|
| 137 |
$('body.post-type-campaign .handlediv, body.post-type-donation .handlediv').remove(); |
| 138 |
$('body.post-type-campaign .hndle, body.post-type-donation .hndle').removeClass( 'hndle ui-sortable-handle' ).addClass( 'postbox-title' ); |
| 139 |
|
| 140 |
setup_advanced_meta_box(); |
| 141 |
setup_sortable_suggested_donations(); |
| 142 |
|
| 143 |
setup_charitable_ajax(); |
| 144 |
setup_charitable_toggle(); |
| 145 |
setup_dashboard_widgets(); |
| 146 |
|
| 147 |
$('[data-charitable-add-row]').on( 'click', function() { |
| 148 |
var type = $( this ).data( 'charitable-add-row' ); |
| 149 |
|
| 150 |
if ( 'suggested-amount' === type ) { |
| 151 |
add_suggested_amount_row($(this)); |
| 152 |
} |
| 153 |
|
| 154 |
return false; |
| 155 |
}); |
| 156 |
|
| 157 |
$('.charitable-campaign-suggested-donations').on( 'click', '.charitable-delete-row', function() { console.log('clicked'); |
| 158 |
delete_suggested_amount_row( $(this) ); |
| 159 |
return false; |
| 160 |
}); |
| 161 |
|
| 162 |
$('body').on( 'click', '[data-campaign-benefactor-delete]', function() { |
| 163 |
var $block = $( this ).parents( '.charitable-benefactor' ), |
| 164 |
data = { |
| 165 |
action : 'charitable_delete_benefactor', |
| 166 |
benefactor_id : $(this).data( 'campaign-benefactor-delete' ), |
| 167 |
nonce : $(this).data( 'nonce' ) |
| 168 |
}; |
| 169 |
|
| 170 |
$.ajax({ |
| 171 |
type: "POST", |
| 172 |
data: data, |
| 173 |
dataType: "json", |
| 174 |
url: ajaxurl, |
| 175 |
xhrFields: { |
| 176 |
withCredentials: true |
| 177 |
}, |
| 178 |
success: function (response) { |
| 179 |
if ( response.deleted ) { |
| 180 |
$block.remove(); |
| 181 |
} |
| 182 |
} |
| 183 |
}).fail(function (data) { |
| 184 |
if ( window.console && window.console.log ) { |
| 185 |
console.log( 'failture' ); |
| 186 |
console.log( data ); |
| 187 |
} |
| 188 |
}); |
| 189 |
|
| 190 |
return false; |
| 191 |
}); |
| 192 |
|
| 193 |
$('#change-donation-status').on( 'change', function() { |
| 194 |
$(this).parents( 'form' ).submit(); |
| 195 |
}); |
| 196 |
}); |
| 197 |
|
| 198 |
})( jQuery ); |