script-insert.js
155 lines
| 1 | (function($){ |
| 2 | |
| 3 | $(document).ready(function(){ |
| 4 | |
| 5 | var last_sort = 'desc'; |
| 6 | |
| 7 | var send_editor = function( content = '' ){ |
| 8 | if( typeof parent.send_to_editor === 'function' ){ |
| 9 | parent.send_to_editor( content ); |
| 10 | }else{ |
| 11 | alert( 'Editor does not exist. Cannot insert content !' ); |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | var close_window = function(){ |
| 16 | if( typeof parent.tb_remove === 'function' ){ |
| 17 | parent.tb_remove(); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | var sort = function( ele, orderby ){ |
| 22 | var total = ele.length; |
| 23 | while( total ){ |
| 24 | ele.each(function(){ |
| 25 | var $cur = $(this); |
| 26 | var $next = $cur.next(); |
| 27 | if( $next.length ){ |
| 28 | var cur_name = $cur.attr( 'data-name' ).toLowerCase(); |
| 29 | var nxt_name = $next.attr( 'data-name' ).toLowerCase(); |
| 30 | if( ( orderby == 'asc' && cur_name > nxt_name ) || ( orderby == 'desc' && cur_name < nxt_name ) ){ |
| 31 | $next.after( $cur ); |
| 32 | } |
| 33 | } |
| 34 | }); |
| 35 | total--; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | $('.sc_shortcode_name').append('<span class="sc_toggle"></span>'); |
| 40 | |
| 41 | $( document ).on( 'click', '.sc_insert', function(){ |
| 42 | |
| 43 | var params = ''; |
| 44 | var scname = $(this).closest( '.sc_shortcode' ).attr( 'data-name' ); |
| 45 | var sc = ''; |
| 46 | |
| 47 | $(this).parent().children().find('input[type="text"]').each(function(){ |
| 48 | if($(this).val() != ''){ |
| 49 | attr = $(this).attr('data-param'); |
| 50 | val = $(this).val().replace( /\"/g, '' ); |
| 51 | params += attr + '="' + val + '" '; |
| 52 | } |
| 53 | }); |
| 54 | |
| 55 | sc = '[sc name="' + scname + '" ' + params + ']'; |
| 56 | send_editor( sc ); |
| 57 | close_window(); |
| 58 | |
| 59 | }); |
| 60 | |
| 61 | $( document ).on( 'click', '.sc_quick_insert', function(){ |
| 62 | |
| 63 | var scname = $(this).closest( '.sc_shortcode' ).attr( 'data-name' ); |
| 64 | var sc = '[sc name="' + scname + '"]'; |
| 65 | |
| 66 | send_editor( sc ); |
| 67 | close_window(); |
| 68 | |
| 69 | }); |
| 70 | |
| 71 | $( document ).on( 'click', '.sc_shortcode_name', function(e){ |
| 72 | $('.sc_params').slideUp(); |
| 73 | if($(this).next('.sc_params').is(':visible')){ |
| 74 | $(this).next('.sc_params').slideUp(); |
| 75 | }else{ |
| 76 | $(this).next('.sc_params').slideDown(); |
| 77 | } |
| 78 | }); |
| 79 | |
| 80 | $( document ).on( 'change', '.coffee_amt', function(){ |
| 81 | var btn = $( '.buy_coffee_btn' ); |
| 82 | btn.attr( 'href', btn.data( 'link' ) + $(this).val() ); |
| 83 | }); |
| 84 | |
| 85 | $( document ).on( 'click', '.sort_btn', function(){ |
| 86 | last_sort = ( last_sort == 'asc' ) ? 'desc' : 'asc'; |
| 87 | sort( $( '.sc_shortcode' ), last_sort ); |
| 88 | }); |
| 89 | |
| 90 | $( document ).on( 'keyup', '.search_box', function(){ |
| 91 | var re = new RegExp($(this).val(), 'gi'); |
| 92 | $('.sc_wrap .sc_shortcode').each(function(){ |
| 93 | var name = $(this).attr('data-name'); |
| 94 | if( name.match(re) === null ){ |
| 95 | $(this).hide(); |
| 96 | }else{ |
| 97 | $(this).show(); |
| 98 | } |
| 99 | }); |
| 100 | |
| 101 | var visible = $('.sc_wrap .sc_shortcode:visible').length; |
| 102 | var $no_scs_msg = $('.sc_wrap').find('p'); |
| 103 | if( visible == 0 ){ |
| 104 | if( $no_scs_msg.length == 0 ){ |
| 105 | $('.sc_wrap').append( '<p align="center"><i>No shortcodes match search term !</i></p>' ); |
| 106 | } |
| 107 | }else{ |
| 108 | $no_scs_msg.remove(); |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | $( document ).on( 'click', '.tags_filter_btn', function(){ |
| 113 | $( '.sc_tags' ).slideToggle(); |
| 114 | }); |
| 115 | |
| 116 | $( document ).on( 'click', '.sc_tags li', function(){ |
| 117 | |
| 118 | $(this).toggleClass( 'active' ); |
| 119 | var tags_sel = []; |
| 120 | |
| 121 | $('.sc_tags li.active').each(function(){ |
| 122 | var tag = $(this).attr('data-value'); |
| 123 | tags_sel.push(tag); |
| 124 | }); |
| 125 | |
| 126 | if(tags_sel.length == 0){ |
| 127 | $('.sc_wrap .sc_shortcode').show(); |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | $('.sc_wrap .sc_shortcode').each(function(){ |
| 132 | var tags = $(this).attr('data-tags'); |
| 133 | var tags_split = $.map(tags.split(','), $.trim); |
| 134 | var has_tag = false; |
| 135 | |
| 136 | $.each(tags_sel, function(i, tag){ |
| 137 | if(tags_split.includes(tag)){ |
| 138 | has_tag = true; |
| 139 | return true; |
| 140 | } |
| 141 | }); |
| 142 | |
| 143 | if(has_tag){ |
| 144 | $(this).show(); |
| 145 | }else{ |
| 146 | $(this).hide(); |
| 147 | } |
| 148 | |
| 149 | }); |
| 150 | |
| 151 | }); |
| 152 | |
| 153 | }); |
| 154 | |
| 155 | })( jQuery ); |