script.js
254 lines
| 1 | (function($){ |
| 2 | $(document).ready(function(){ |
| 3 | |
| 4 | var delete_ctext = 'Are you sure want to delete this shortcode ?'; |
| 5 | var last_sort = 'desc'; |
| 6 | |
| 7 | var init = function(){ |
| 8 | if(window.sc_cm_editor){ |
| 9 | window.sc_cm = CodeMirror.fromTextArea( document.getElementById( 'sc_content' ), { |
| 10 | lineNumbers: true, |
| 11 | mode: "htmlmixed", |
| 12 | indentWithTabs: false, |
| 13 | lineWrapping: true |
| 14 | }); |
| 15 | sc_cm.setSize( null, 500 ); |
| 16 | sc_cm.on( 'change', function(){ |
| 17 | sc_cm.save(); |
| 18 | }); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | var sort = function( ele, orderby ){ |
| 23 | var total = ele.length; |
| 24 | while( total ){ |
| 25 | ele.each(function(){ |
| 26 | var $cur = $(this); |
| 27 | var $next = $cur.next(); |
| 28 | if( $next.length ){ |
| 29 | var cur_name = $cur.attr( 'data-name' ).toLowerCase(); |
| 30 | var nxt_name = $next.attr( 'data-name' ).toLowerCase(); |
| 31 | if( ( orderby == 'asc' && cur_name > nxt_name ) || ( orderby == 'desc' && cur_name < nxt_name ) ){ |
| 32 | $next.after( $cur ); |
| 33 | } |
| 34 | } |
| 35 | }); |
| 36 | total--; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | var insert_in_editor = function( data ){ |
| 41 | if( window.sc_cm_editor ){ |
| 42 | var doc = window.sc_cm.getDoc(); |
| 43 | doc.replaceRange( data, doc.getCursor() ); |
| 44 | }else{ |
| 45 | send_to_editor( data ); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | $( document ).on( 'click', '.sc_delete', function(e){ |
| 50 | |
| 51 | e.preventDefault(); |
| 52 | |
| 53 | var del_btn = $(this); |
| 54 | var href = del_btn.attr( 'href' ); |
| 55 | var confirm_user = confirm( delete_ctext ); |
| 56 | |
| 57 | if( confirm_user ){ |
| 58 | |
| 59 | var ajax = $.get( href ); |
| 60 | del_btn.addClass( 'spin' ); |
| 61 | |
| 62 | ajax.done(function( data ){ |
| 63 | if( data.search( 'DELETED' ) != -1 ){ |
| 64 | del_btn.closest( 'li' ).fadeOut( 'slow', function(){ |
| 65 | $(this).remove(); |
| 66 | }); |
| 67 | }else{ |
| 68 | alert( 'Delete failed ! - ' + data ); |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | ajax.fail(function(){ |
| 73 | alert( 'Auth failed !' ); |
| 74 | }); |
| 75 | |
| 76 | } |
| 77 | |
| 78 | }); |
| 79 | |
| 80 | $( document ).on( 'click', '.sc_delete_ep', function(e){ |
| 81 | |
| 82 | e.preventDefault(); |
| 83 | |
| 84 | var $delete_btn = $(this); |
| 85 | var href = $delete_btn.attr( 'href' ); |
| 86 | var confirm_user = confirm( delete_ctext ); |
| 87 | |
| 88 | if( confirm_user ){ |
| 89 | |
| 90 | var ajax = $.get( href ); |
| 91 | $delete_btn.addClass( 'spin' ); |
| 92 | |
| 93 | ajax.done(function( data ){ |
| 94 | if( data.search( 'DELETED' ) != -1 ){ |
| 95 | var back_href = $( '.sc_back_btn' ).attr( 'href' ); |
| 96 | window.location = back_href + '&msg=3'; |
| 97 | }else{ |
| 98 | alert( 'Delete failed ! - ' + data ); |
| 99 | } |
| 100 | }); |
| 101 | |
| 102 | ajax.fail(function(){ |
| 103 | alert( 'Auth failed !' ); |
| 104 | }); |
| 105 | |
| 106 | $delete_btn.removeClass( 'spin' ); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | }); |
| 111 | |
| 112 | $( document ).on( 'click', '.sc_copy', function(e){ |
| 113 | |
| 114 | e.preventDefault(); |
| 115 | |
| 116 | var btn = $(this); |
| 117 | var box = btn.closest( 'li' ).find( '.sc_copy_box' ); |
| 118 | |
| 119 | $( '.sc_copy_box' ).not( box ).hide(); |
| 120 | |
| 121 | box.fadeToggle(); |
| 122 | box.select(); |
| 123 | |
| 124 | }); |
| 125 | |
| 126 | $(window).load(function(){ |
| 127 | |
| 128 | var insert_button = function(){ |
| 129 | return '<button class="button button-primary sc_insert_params"><span class="dashicons dashicons-plus"></span> Insert shortcode paramerters <span class="dashicons dashicons-arrow-down"></span></button>'; |
| 130 | } |
| 131 | |
| 132 | $( '.wp-media-buttons' ).append( insert_button ); |
| 133 | |
| 134 | if( window.sc_cm_editor ){ |
| 135 | $( '.CodeMirror' ).before( insert_button ); |
| 136 | } |
| 137 | |
| 138 | $( '.params_wrap' ).appendTo( 'body' ); |
| 139 | |
| 140 | $( '.quicktags-toolbar' ).append(function(){ |
| 141 | var html = '<a href="#" class="ed_button button button-small fright sc_switch_editor" data-type="1" title="Enable visual editor"><span class="dashicons dashicons-visibility"></span> Visual editor</a>'; |
| 142 | html += '<a href="#" class="ed_button button button-small fright sc_switch_editor" data-type="2" title="Enable code editor"><span class="dashicons dashicons-editor-code"></span> Code editor (beta)</a>'; |
| 143 | return html; |
| 144 | }); |
| 145 | }); |
| 146 | |
| 147 | $( document ).on( 'click', '.sc_insert_params', function(e){ |
| 148 | |
| 149 | e.preventDefault(); |
| 150 | |
| 151 | var offset = $(this).offset(); |
| 152 | var mtop = offset.top + $(this).outerHeight(); |
| 153 | |
| 154 | $( '.params_wrap' ).css({ |
| 155 | top: mtop, |
| 156 | left: offset.left |
| 157 | }).toggle(); |
| 158 | }); |
| 159 | |
| 160 | $( document ).on( 'click', '.cp_btn', function(){ |
| 161 | |
| 162 | var $cp_box = $( '.cp_box' ); |
| 163 | var $cp_info = $( '.cp_info' ); |
| 164 | var param_val = $cp_box.val().trim(); |
| 165 | |
| 166 | if( param_val != '' && $cp_box[0].checkValidity() ){ |
| 167 | insert_in_editor( '%%' + param_val + '%%' ); |
| 168 | $cp_info.removeClass( 'red' ); |
| 169 | $( '.params_wrap' ).hide(); |
| 170 | }else{ |
| 171 | $cp_info.addClass( 'red' ); |
| 172 | } |
| 173 | |
| 174 | }); |
| 175 | |
| 176 | $( document ).on( 'click', '.wp_params li', function(){ |
| 177 | insert_in_editor('$$' + $(this).data( 'id' ) + '$$'); |
| 178 | $( '.params_wrap' ).hide(); |
| 179 | }); |
| 180 | |
| 181 | $( document ).on( 'change', '.coffee_amt', function(){ |
| 182 | var btn = $( '.buy_coffee_btn' ); |
| 183 | btn.attr( 'href', btn.data( 'link' ) + $(this).val() ); |
| 184 | }); |
| 185 | |
| 186 | $( document ).on( 'click', '.sort_btn', function(){ |
| 187 | last_sort = ( last_sort == 'asc' ) ? 'desc' : 'asc'; |
| 188 | sort( $( '.sc_list li' ), last_sort ); |
| 189 | $( '.sort_icon' ).toggleClass( 'dashicons-arrow-down-alt' ); |
| 190 | $( '.sort_icon' ).toggleClass( 'dashicons-arrow-up-alt' ); |
| 191 | }); |
| 192 | |
| 193 | $( document ).on( 'change', '#import', function(){ |
| 194 | if( !confirm( $( '.import_desc' ).text() ) ){ |
| 195 | return false; |
| 196 | } |
| 197 | |
| 198 | if( confirm( $( '.import_desc2' ).text() ) ){ |
| 199 | $( '#fresh_import' ).prop( 'checked', true ); |
| 200 | }else{ |
| 201 | $( '#fresh_import' ).prop( 'checked', false ); |
| 202 | } |
| 203 | |
| 204 | $( '#import_form' ).submit(); |
| 205 | }); |
| 206 | |
| 207 | $( document ).on( 'click', '.search_btn', function(e){ |
| 208 | var $search_box = $(this).find('.search_box'); |
| 209 | if(e.target === $search_box[0]){ |
| 210 | return false; |
| 211 | } |
| 212 | $(this).toggleClass('active'); |
| 213 | $search_box.focus(); |
| 214 | }); |
| 215 | |
| 216 | $( document ).on( 'keyup', '.search_box', function(){ |
| 217 | var search_term = $(this).val(); |
| 218 | var re = new RegExp(search_term, 'gi'); |
| 219 | $('.sc_list li').each(function(){ |
| 220 | var name = $(this).attr('data-name'); |
| 221 | if(name.match(re) === null){ |
| 222 | $(this).hide(); |
| 223 | }else{ |
| 224 | $(this).show(); |
| 225 | } |
| 226 | }); |
| 227 | |
| 228 | if(search_term){ |
| 229 | $(this).parent().addClass('filtered'); |
| 230 | }else{ |
| 231 | $(this).parent().removeClass('filtered'); |
| 232 | } |
| 233 | |
| 234 | var visible = $('.sc_list li:visible').length; |
| 235 | var $no_scs_msg = $('.sc_list').find('p'); |
| 236 | if( visible == 0 ){ |
| 237 | if( $no_scs_msg.length == 0 ){ |
| 238 | $('.sc_list').append( '<p align="center" class="search_empty_msg"><i>No shortcodes match search term !</i></p>' ); |
| 239 | } |
| 240 | }else{ |
| 241 | $no_scs_msg.remove(); |
| 242 | } |
| 243 | |
| 244 | }); |
| 245 | |
| 246 | $( document ).on( 'click', '.sc_switch_editor', function(e){ |
| 247 | e.preventDefault(); |
| 248 | window.location = window.location + '&editor=' + $(this).data('type'); |
| 249 | }); |
| 250 | |
| 251 | init(); |
| 252 | |
| 253 | }); |
| 254 | })( jQuery ); |