script.js
332 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 | var tags_filt_api = false; |
| 7 | |
| 8 | var init = function(){ |
| 9 | if(window.sc_cm_editor){ |
| 10 | window.sc_cm = CodeMirror.fromTextArea( document.getElementById( 'sc_content' ), { |
| 11 | lineNumbers: true, |
| 12 | mode: "htmlmixed", |
| 13 | indentWithTabs: false, |
| 14 | lineWrapping: true, |
| 15 | styleActiveLine: true |
| 16 | }); |
| 17 | sc_cm.setSize( null, 500 ); |
| 18 | sc_cm.on( 'change', function(){ |
| 19 | sc_cm.save(); |
| 20 | }); |
| 21 | } |
| 22 | |
| 23 | if( $.fn.selectize ){ |
| 24 | |
| 25 | $('.sc_edit_tags').selectize({ |
| 26 | create: true |
| 27 | }); |
| 28 | |
| 29 | if( $('.sc_tags_filter').length ){ |
| 30 | |
| 31 | $tags_filter_ele = $('.sc_tags_filter').selectize({ |
| 32 | onChange: filter_list_tags |
| 33 | }); |
| 34 | |
| 35 | tags_filt_api = $tags_filter_ele[0].selectize; |
| 36 | |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | } |
| 41 | |
| 42 | var sort = function( ele, orderby ){ |
| 43 | var total = ele.length; |
| 44 | while( total ){ |
| 45 | ele.each(function(){ |
| 46 | var $cur = $(this); |
| 47 | var $next = $cur.next(); |
| 48 | if( $next.length ){ |
| 49 | var cur_name = $cur.attr( 'data-name' ).toLowerCase(); |
| 50 | var nxt_name = $next.attr( 'data-name' ).toLowerCase(); |
| 51 | if( ( orderby == 'asc' && cur_name > nxt_name ) || ( orderby == 'desc' && cur_name < nxt_name ) ){ |
| 52 | $next.after( $cur ); |
| 53 | } |
| 54 | } |
| 55 | }); |
| 56 | total--; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | var insert_in_editor = function( data ){ |
| 61 | if( window.sc_cm_editor ){ |
| 62 | var doc = window.sc_cm.getDoc(); |
| 63 | doc.replaceRange( data, doc.getCursor() ); |
| 64 | }else{ |
| 65 | send_to_editor( data ); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | var filter_list_tags = function(){ |
| 70 | |
| 71 | var sel_tags = tags_filt_api.items; |
| 72 | var $sc_list = $( '.sc_list > li' ); |
| 73 | |
| 74 | if( sel_tags.length == 0){ |
| 75 | $sc_list.show(); |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | $sc_list.each(function(){ |
| 80 | var $sc_item = $( this ); |
| 81 | var sc_tags = $sc_item.attr( 'data-tags' ); |
| 82 | |
| 83 | if( typeof sc_tags === 'undefined' ){ |
| 84 | $sc_item.hide(); |
| 85 | return true; |
| 86 | }else{ |
| 87 | $sc_item.show(); |
| 88 | } |
| 89 | |
| 90 | var sc_tags_split = sc_tags.split( ',' ); |
| 91 | var has_tag = false; |
| 92 | |
| 93 | $.each( sel_tags, function( i, tag ){ |
| 94 | if( sc_tags_split.includes( tag ) ){ |
| 95 | has_tag = true; |
| 96 | return true; |
| 97 | } |
| 98 | }); |
| 99 | |
| 100 | if( has_tag ){ |
| 101 | $sc_item.show(); |
| 102 | }else{ |
| 103 | $sc_item.hide(); |
| 104 | } |
| 105 | |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | $( document ).on( 'click', '.sc_delete', function(e){ |
| 110 | |
| 111 | e.preventDefault(); |
| 112 | |
| 113 | var del_btn = $(this); |
| 114 | var href = del_btn.attr( 'href' ); |
| 115 | var confirm_user = confirm( delete_ctext ); |
| 116 | |
| 117 | if( confirm_user ){ |
| 118 | |
| 119 | var ajax = $.get( href ); |
| 120 | del_btn.addClass( 'spin' ); |
| 121 | |
| 122 | ajax.done(function( data ){ |
| 123 | if( data.search( 'DELETED' ) != -1 ){ |
| 124 | del_btn.closest( 'li' ).fadeOut( 'slow', function(){ |
| 125 | $(this).remove(); |
| 126 | }); |
| 127 | }else{ |
| 128 | alert( 'Delete failed ! - ' + data ); |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | ajax.fail(function(){ |
| 133 | alert( 'Auth failed !' ); |
| 134 | }); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | }); |
| 139 | |
| 140 | $( document ).on( 'click', '.sc_delete_ep', function(e){ |
| 141 | |
| 142 | e.preventDefault(); |
| 143 | |
| 144 | var $delete_btn = $(this); |
| 145 | var href = $delete_btn.attr( 'href' ); |
| 146 | var confirm_user = confirm( delete_ctext ); |
| 147 | |
| 148 | if( confirm_user ){ |
| 149 | |
| 150 | var ajax = $.get( href ); |
| 151 | $delete_btn.addClass( 'spin' ); |
| 152 | |
| 153 | ajax.done(function( data ){ |
| 154 | if( data.search( 'DELETED' ) != -1 ){ |
| 155 | var back_href = $( '.sc_back_btn' ).attr( 'href' ); |
| 156 | window.location = back_href + '&msg=3'; |
| 157 | }else{ |
| 158 | alert( 'Delete failed ! - ' + data ); |
| 159 | } |
| 160 | }); |
| 161 | |
| 162 | ajax.fail(function(){ |
| 163 | alert( 'Auth failed !' ); |
| 164 | }); |
| 165 | |
| 166 | $delete_btn.removeClass( 'spin' ); |
| 167 | |
| 168 | } |
| 169 | |
| 170 | }); |
| 171 | |
| 172 | $( document ).on( 'click', '.sc_copy', function(e){ |
| 173 | |
| 174 | e.preventDefault(); |
| 175 | |
| 176 | var btn = $(this); |
| 177 | var box = btn.closest( 'li' ).find( '.sc_copy_box' ); |
| 178 | |
| 179 | $( '.sc_copy_box' ).not( box ).hide(); |
| 180 | |
| 181 | box.fadeToggle(); |
| 182 | box.select(); |
| 183 | |
| 184 | }); |
| 185 | |
| 186 | $(window).load(function(){ |
| 187 | |
| 188 | var insert_button = function(){ |
| 189 | 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>'; |
| 190 | } |
| 191 | |
| 192 | $( '.wp-media-buttons' ).append( insert_button ); |
| 193 | $( '.sc_editor_list' ).appendTo( '.wp-media-buttons' ); |
| 194 | |
| 195 | if( window.sc_cm_editor ){ |
| 196 | $( '.sc_cm_menu' ).append( insert_button ); |
| 197 | $( '.sc_editor_list' ).appendTo( '.sc_cm_menu' ); |
| 198 | } |
| 199 | |
| 200 | $( '.params_wrap' ).appendTo( 'body' ); |
| 201 | |
| 202 | }); |
| 203 | |
| 204 | $( document ).on( 'click', '.sc_insert_params', function(e){ |
| 205 | |
| 206 | e.preventDefault(); |
| 207 | |
| 208 | var offset = $(this).offset(); |
| 209 | var mtop = offset.top + $(this).outerHeight(); |
| 210 | |
| 211 | $( '.params_wrap' ).css({ |
| 212 | top: mtop, |
| 213 | left: offset.left |
| 214 | }).toggle(); |
| 215 | }); |
| 216 | |
| 217 | $( document ).on( 'click', '.sc_tags_filt_icon', function(e){ |
| 218 | |
| 219 | e.preventDefault(); |
| 220 | $( this ).closest( '.sc_tags_filt_btn' ).toggleClass( 'active' ); |
| 221 | |
| 222 | }); |
| 223 | |
| 224 | $( document ).on( 'click', '.sc_tags_list li', function(e){ |
| 225 | |
| 226 | if(tags_filt_api){ |
| 227 | var tag = $(this).attr( 'data-tag-id' ); |
| 228 | tags_filt_api.addItem( tag ); |
| 229 | $( '.sc_tags_filt_btn' ).addClass( 'active' ); |
| 230 | } |
| 231 | |
| 232 | }); |
| 233 | |
| 234 | $( document ).on( 'click', '.cp_btn', function(){ |
| 235 | |
| 236 | var $cp_box = $( '.cp_box' ); |
| 237 | var $cp_info = $( '.cp_info' ); |
| 238 | var param_val = $cp_box.val().trim(); |
| 239 | |
| 240 | if( param_val != '' && $cp_box[0].checkValidity() ){ |
| 241 | insert_in_editor( '%%' + param_val + '%%' ); |
| 242 | $cp_info.removeClass( 'red' ); |
| 243 | $( '.params_wrap' ).hide(); |
| 244 | }else{ |
| 245 | $cp_info.addClass( 'red' ); |
| 246 | } |
| 247 | |
| 248 | }); |
| 249 | |
| 250 | $( document ).on( 'click', '.wp_params li', function(){ |
| 251 | insert_in_editor('$$' + $(this).data( 'id' ) + '$$'); |
| 252 | $( '.params_wrap' ).hide(); |
| 253 | }); |
| 254 | |
| 255 | $( document ).on( 'change', '.coffee_amt', function(){ |
| 256 | var btn = $( '.buy_coffee_btn' ); |
| 257 | btn.attr( 'href', btn.data( 'link' ) + $(this).val() ); |
| 258 | }); |
| 259 | |
| 260 | $( document ).on( 'click', '.sort_btn', function(){ |
| 261 | last_sort = ( last_sort == 'asc' ) ? 'desc' : 'asc'; |
| 262 | sort( $( '.sc_list > li' ), last_sort ); |
| 263 | $( '.sort_icon' ).toggleClass( 'dashicons-arrow-down-alt' ); |
| 264 | $( '.sort_icon' ).toggleClass( 'dashicons-arrow-up-alt' ); |
| 265 | }); |
| 266 | |
| 267 | $( document ).on( 'change', '#import', function(){ |
| 268 | if( !confirm( $( '.import_desc' ).text() ) ){ |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | if( confirm( $( '.import_desc2' ).text() ) ){ |
| 273 | $( '#fresh_import' ).prop( 'checked', true ); |
| 274 | }else{ |
| 275 | $( '#fresh_import' ).prop( 'checked', false ); |
| 276 | } |
| 277 | |
| 278 | $( '#import_form' ).submit(); |
| 279 | }); |
| 280 | |
| 281 | $( document ).on( 'change', '.sc_editor', function(e){ |
| 282 | window.location = window.location + '&editor=' + $(this).val(); |
| 283 | }); |
| 284 | |
| 285 | $( document ).on( 'click', '.search_btn', function(e){ |
| 286 | var $search_box = $(this).find('.search_box'); |
| 287 | if(e.target === $search_box[0]){ |
| 288 | return false; |
| 289 | } |
| 290 | $(this).toggleClass('active'); |
| 291 | $search_box.focus(); |
| 292 | |
| 293 | }); |
| 294 | |
| 295 | $( document ).on( 'keyup', '.search_box', function(){ |
| 296 | var search_term = $(this).val(); |
| 297 | var re = new RegExp(search_term, 'gi'); |
| 298 | $('.sc_list > li').each(function(){ |
| 299 | var name = $(this).attr('data-name'); |
| 300 | if(name.match(re) === null){ |
| 301 | $(this).hide(); |
| 302 | }else{ |
| 303 | $(this).show(); |
| 304 | } |
| 305 | }); |
| 306 | |
| 307 | if(search_term){ |
| 308 | $(this).parent().addClass('filtered'); |
| 309 | }else{ |
| 310 | $(this).parent().removeClass('filtered'); |
| 311 | } |
| 312 | |
| 313 | var visible = $('.sc_list > li:visible').length; |
| 314 | var $no_scs_msg = $('.sc_list').find('p'); |
| 315 | if( visible == 0 ){ |
| 316 | if( $no_scs_msg.length == 0 ){ |
| 317 | $('.sc_list').append( '<p align="center" class="search_empty_msg"><i>No shortcodes match search term !</i></p>' ); |
| 318 | } |
| 319 | }else{ |
| 320 | $no_scs_msg.remove(); |
| 321 | } |
| 322 | |
| 323 | }); |
| 324 | |
| 325 | $( document ).on( 'click', '.sc_note_btn', function(e){ |
| 326 | $('.sc_note').slideToggle(); |
| 327 | }); |
| 328 | |
| 329 | init(); |
| 330 | |
| 331 | }); |
| 332 | })( jQuery ); |