暫無描述

admin.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. var br_saved_timeout;
  2. var br_savin_ajax = false;
  3. // function brand_widget_init() {
  4. // jQuery('.colorpicker_field_brand').each(function (i,o){
  5. // jQuery(o).css('backgroundColor', '#'+jQuery(o).data('color'));
  6. // jQuery(o).colpick({
  7. // layout: 'hex',
  8. // submit: 0,
  9. // color: '#'+jQuery(o).data('color'),
  10. // onChange: function(hsb,hex,rgb,el,bySetColor) {
  11. // jQuery(el).css('backgroundColor', '#'+hex).next().val(hex).trigger('change');
  12. // }
  13. // })
  14. // });
  15. // }
  16. (function ($){
  17. function setColorPicker() {
  18. $('.br_brand_colorpicker').wpColorPicker( {
  19. change: function (event, ui) {
  20. var element = event.target;
  21. var color = ui.color.toString();
  22. $(event.target).val(color).trigger('change');
  23. }
  24. } );
  25. }
  26. $(document).ajaxComplete( setColorPicker );
  27. function show_depending_options( checkbox ) {
  28. let depending_options = checkbox.closest('.widget').find( '.' + checkbox.attr('id') );
  29. if ( checkbox.find('[type="checkbox"]').prop('checked') ) {
  30. depending_options.show();
  31. } else {
  32. depending_options.hide();
  33. }
  34. }
  35. $(document).ready( function () {
  36. // brand_widget_init();
  37. setColorPicker();
  38. $(document).ajaxComplete( function() {
  39. $('.br_brand_show_more_options').each( function() {
  40. show_depending_options( $(this) );
  41. });
  42. } );
  43. $(document).on('click', '.toggle-indicator, .wp-block-legacy-widget', function() {
  44. $(this).closest('.widget').find('.br_brand_show_more_options').each( function() {
  45. show_depending_options( $(this) );
  46. });
  47. });
  48. $(document).on('click', '.br_brand_show_more_options', function() {
  49. show_depending_options( $(this) );
  50. });
  51. $(document).on('click', '.br_brand_image', function() {
  52. $(this).closest('.form-field').find('.berocket_aapf_upload_icon').trigger('focus');
  53. });
  54. $(document).on('click', '.br_brand_category_checklist', function() {
  55. $(this).closest('.form-field').find('.br_brand_product_categories > li:first-of-type input').trigger('focus');
  56. });
  57. $(document).on('click', '.br_shortcode_title', function() {
  58. $(this).closest('li').find('.br_shortcode_attributes').slideToggle();
  59. $(this).find('i').toggleClass('arrow-rotate');
  60. });
  61. $(document).on('change', '.br_has_dependent_options', function() {
  62. var self = $(this),
  63. dependent_element = $( '.' + self.attr('id') + '_dependent' );
  64. if ( self.prop('checked') ) {
  65. dependent_element.closest('tr').show();
  66. } else {
  67. dependent_element.closest('tr').hide();
  68. }
  69. });
  70. $('.br_has_dependent_options').trigger('change');
  71. $(document).on('click', '.br_brand_order', function() {
  72. var self = $(this),
  73. input = self.closest('td').find('.br_brand_order_input'),
  74. order = parseInt( input.val() ),
  75. new_order = self.attr('href') == '#order-up' ? order+1 : Math.max(0, order-1);
  76. input.val( new_order );
  77. });
  78. $(document).on('click', '.br_brand_order_save', function() {
  79. var
  80. self = $(this),
  81. parent = self.closest('.berocket_post_set_new_sortable_input'),
  82. save_button = parent.find('.br_brand_order_save'),
  83. waiting_title = parent.find('.br_brand_order_wait');
  84. waiting_title.show();
  85. save_button.hide();
  86. $.ajax({
  87. type: 'GET',
  88. url: brandsHelper.ajax_url,
  89. data: {
  90. 'action': 'br_brands_save_order',
  91. 'term_id': self.data('term_id'),
  92. 'order': parent.find('.br_brand_order_input').val(),
  93. },
  94. success: function(result){
  95. waiting_title.hide();
  96. save_button.show();
  97. },
  98. });
  99. });
  100. $(".taxonomy-berocket_brand #the-list").sortable({
  101. cursor: 'move',
  102. handle: '.column-order',
  103. placeholder: 'brand-sortable-placeholder',
  104. start: function (event, ui) {
  105. ui.item.toggleClass('br-sortable-placeholder');
  106. },
  107. stop: function (event, ui) {
  108. ui.item.toggleClass('br-sortable-placeholder');
  109. $('html, body').css("cursor", "wait");
  110. let term_ids = $('#the-list tr').map( function(element) { return this.id; }).get();
  111. $.ajax({
  112. type: 'GET',
  113. url: brandsHelper.ajax_url,
  114. data: {
  115. 'action': 'br_brands_save_all_orders',
  116. 'term_ids': term_ids,
  117. },
  118. success: function(result){
  119. // console.log(result);
  120. term_ids.forEach( function(id, order) {
  121. $('#the-list #' + id + ' .br_brand_order_input').val(order);
  122. });
  123. $('html, body').css("cursor", "auto");
  124. },
  125. error: function(){
  126. $('html, body').css("cursor", "auto");
  127. },
  128. });
  129. // console.log( $('#the-list tr').map( function(element) {
  130. // return this.id;
  131. // }).get() );
  132. // $('#the-list tr').each(function(index) {
  133. // console.log( index, $(this).attr('id') );
  134. // });
  135. }
  136. }).disableSelection();
  137. $(document).on('change', '#br_brand_list_image', function(e) {
  138. var
  139. self = $(this),
  140. options = self.closest('fieldset').find('p:not(:first)');
  141. if ( self.prop('checked') ) {
  142. console.log('show image options');
  143. options.removeClass('br_brand_hide_option');
  144. } else {
  145. console.log('hide image options');
  146. options.addClass('br_brand_hide_option');
  147. }
  148. });
  149. $('#br_brand_list_image').trigger('change');
  150. $(document).on('click', '#br_brands_clear_cache', function(e) {
  151. let self = $('#br_brands_clear_cache');
  152. let label = self.val();
  153. let process = self.data('process');
  154. let done = self.data('done');
  155. self.addClass('active').val(process);
  156. $.ajax({
  157. type: 'GET',
  158. url: brandsHelper.ajax_url,
  159. data: {
  160. 'action': 'br_brands_clear_cache'
  161. },
  162. success: function(result){
  163. self.removeClass('active').val(done);
  164. // setTimeout(function(){ self.val(label) }, 1000);
  165. // console.log(result);
  166. },
  167. });
  168. });
  169. $(document).on('change', '.br_brands_display_options', function() {
  170. var
  171. self = $(this),
  172. options = self.closest('td').find('label:not(:first), .br_line_delimiter');
  173. if ( self.prop('checked') ) {
  174. options.show();
  175. } else {
  176. options.hide();
  177. }
  178. });
  179. $('.br_brands_display_options').trigger('change');
  180. $(document).on('change', '.br_brand_display_options', function(){
  181. var
  182. self = $(this),
  183. option_class = self.data('option_class');
  184. if ( self.prop('checked') ) {
  185. $(option_class).removeClass('br_brand_hide_option');
  186. } else {
  187. $(option_class).addClass('br_brand_hide_option');
  188. }
  189. });
  190. $('.br_brand_display_options').trigger('change');
  191. $(document).on('click', '.br_brands_image .berocket_aapf_upload_icon', function(e) {
  192. e.preventDefault();
  193. $p = $(this);
  194. var custom_uploader = wp.media({
  195. title: 'Select custom Icon',
  196. button: {
  197. text: 'Set Icon'
  198. },
  199. multiple: false
  200. }).on('select', function() {
  201. var attachment = custom_uploader.state().get('selection').first().toJSON();
  202. $p.prevAll(".berocket_aapf_selected_icon_show").html('<i class="fa"><image src="'+attachment.url+'" alt=""></i>');
  203. $p.prevAll(".berocket_aapf_icon_text_value").val(attachment.url);
  204. }).open();
  205. });
  206. $(document).on('click', '.br_brands_image .berocket_aapf_remove_icon',function(event) {
  207. event.preventDefault();
  208. $(this).prevAll(".berocket_aapf_icon_text_value").val("");
  209. $(this).prevAll(".berocket_aapf_selected_icon_show").html("");
  210. });
  211. $(document).on('click', '.theme_default', function (event) {
  212. event.preventDefault();
  213. $(this).prev().prev().css('backgroundColor', '#000000').colpickSetColor('#000000');
  214. $(this).prev().val('');
  215. });
  216. $(document).on('change', '.br_brandw_perrow, .br_brandw_count', function() {
  217. var $parent = $(this).parents('.br_brandw_js');
  218. if( $parent.find('.br_brandw_count').val() && $parent.find('.br_brandw_perrow').val() ) {
  219. var count = parseInt($parent.find('.br_brandw_count').val());
  220. var perrow = parseInt($parent.find('.br_brandw_perrow').val());
  221. if( perrow > count ) {
  222. $parent.find('.br_brandw_perrow').val(count);
  223. }
  224. }
  225. });
  226. function shop_display_brand() {
  227. if( $('.shop_display_brand').prop('checked') ) {
  228. $('.shop_display_brand_enabled').show();
  229. } else {
  230. $('.shop_display_brand_enabled').hide();
  231. }
  232. }
  233. $(document).on('change', '.shop_display_brand', shop_display_brand);
  234. shop_display_brand();
  235. function product_display_brand() {
  236. if( $('.product_display_brand').prop('checked') ) {
  237. $('.product_display_brand_enabled').show();
  238. } else {
  239. $('.product_display_brand_enabled').hide();
  240. }
  241. }
  242. $(document).on('change', '.product_display_brand', product_display_brand);
  243. product_display_brand();
  244. });
  245. function split( val ) {
  246. return val.split( /,\s*/ );
  247. }
  248. function extractLast( term ) {
  249. return split( term ).pop();
  250. }
  251. $(document).on('keydown', ".br_brand_autocomplete", function(e){
  252. let selected_brands = $(e.target).val().split(',').filter(function (el) { return el.length; });
  253. $(this).autocomplete({
  254. classes: {
  255. "ui-autocomplete": "br-brands-ui-autocomplete"
  256. },
  257. source: function( request, response ) {
  258. // response( $.ui.autocomplete.filter( brands, extractLast( request.term ) ) );
  259. $.ajax( {
  260. type: 'GET',
  261. url: brandsHelper.ajax_url,
  262. data: {
  263. 'action': 'br_get_brands',
  264. },
  265. success: function( data ) {
  266. let brands = JSON.parse(data).filter((item) => !selected_brands.includes(item));
  267. response( brands );
  268. }
  269. } );
  270. },
  271. focus: function() {
  272. return false;
  273. },
  274. select: function( event, ui ) {
  275. var terms = split( this.value );
  276. terms.pop();
  277. terms.push( ui.item.value );
  278. terms.push( "" );
  279. this.value = terms.join( "," );
  280. return false;
  281. }
  282. });
  283. });
  284. })(jQuery);