blocks
6 years ago
tinymce
6 years ago
script-insert.js
6 years ago
script-tools.js
6 years ago
script.js
6 years ago
script.js
224 lines
| 1 | (function($){ |
| 2 | $(document).ready(function(){ |
| 3 | |
| 4 | var init = function(){ |
| 5 | |
| 6 | if(window.SC_EDITOR == 'code'){ |
| 7 | |
| 8 | load_cm_sc_mode(); |
| 9 | |
| 10 | window.sc_cm = CodeMirror.fromTextArea(document.getElementById('sc_content'), { |
| 11 | lineNumbers: true, |
| 12 | mode: 'sc_mode', |
| 13 | indentWithTabs: false, |
| 14 | lineWrapping: true, |
| 15 | styleActiveLine: true, |
| 16 | htmlMode: true |
| 17 | }); |
| 18 | sc_cm.setSize( null, 500 ); |
| 19 | sc_cm.on('change', function(){ |
| 20 | sc_cm.save(); |
| 21 | }); |
| 22 | |
| 23 | $('.sc_editor_toolbar').appendTo('.sc_cm_menu'); |
| 24 | |
| 25 | }else{ |
| 26 | $('.sc_editor_toolbar').appendTo('.wp-media-buttons'); |
| 27 | } |
| 28 | |
| 29 | if(typeof window.SC_VARS !== 'undefined'){ |
| 30 | if(SC_VARS['screen']['base'] == 'edit'){ |
| 31 | |
| 32 | var version = '<small>v' + SC_VARS['sc_version'] + '</small>'; |
| 33 | $('.wp-heading-inline').append(version); |
| 34 | |
| 35 | add_top_import_export_btn(); |
| 36 | |
| 37 | } |
| 38 | |
| 39 | add_top_coffee_btn(); |
| 40 | } |
| 41 | |
| 42 | $('.sc_params_list').appendTo('body'); |
| 43 | |
| 44 | } |
| 45 | |
| 46 | var set_sc_preview_text = function(name){ |
| 47 | $('.sc_preview_text').text('[sc name="' + name + '"]'); |
| 48 | } |
| 49 | |
| 50 | var insert_in_editor = function(data){ |
| 51 | console.log(data); |
| 52 | if(window.SC_EDITOR == 'code'){ |
| 53 | var doc = window.sc_cm.getDoc(); |
| 54 | doc.replaceRange(data, doc.getCursor()); |
| 55 | }else{ |
| 56 | send_to_editor(data); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | var copy_to_clipboard = function(str){ |
| 61 | var el = document.createElement('textarea'); |
| 62 | el.value = str; |
| 63 | el.setAttribute('readonly', ''); |
| 64 | el.style.position = 'absolute'; |
| 65 | el.style.left = '-9999px'; |
| 66 | document.body.appendChild(el); |
| 67 | el.select(); |
| 68 | document.execCommand('copy'); |
| 69 | document.body.removeChild(el); |
| 70 | }; |
| 71 | |
| 72 | var load_cm_sc_mode = function(){ |
| 73 | |
| 74 | if(typeof CodeMirror.overlayMode === 'undefined'){ |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | CodeMirror.defineMode('sc_mode', function(config, parserConfig){ |
| 79 | var mustacheOverlay = { |
| 80 | token: function(stream, state){ |
| 81 | if(stream.match(/\$\$[a-z0-9A-Z:_]+\$\$/)){ |
| 82 | return 'number sc_param'; |
| 83 | } |
| 84 | if(stream.match(/%%.*?%%/)){ |
| 85 | return 'atom sc_param'; |
| 86 | } |
| 87 | stream.next(); |
| 88 | } |
| 89 | }; |
| 90 | return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || 'htmlmixed'), mustacheOverlay); |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | var close_params_list = function(){ |
| 95 | $('.sc_params_list').hide(); |
| 96 | } |
| 97 | |
| 98 | var add_top_coffee_btn = function(){ |
| 99 | |
| 100 | $('#screen-meta-links').prepend('<div class="screen-meta-toggle cfe_top_link"><a class="show-settings button" href="https://www.paypal.me/vaakash/" target="_blank">Buy me a Coffee</a></div>'); |
| 101 | |
| 102 | } |
| 103 | |
| 104 | var add_top_import_export_btn = function(){ |
| 105 | |
| 106 | $('#screen-meta-links').prepend('<div class="screen-meta-toggle ie_top_link hide-if-no-js"><button aria-controls="import-export-tab" aria-expanded="false" class="show-settings button">Import / Export</button></div>'); |
| 107 | |
| 108 | $('#screen-meta').append('<div id="import-export-tab" class="hidden"></div>'); |
| 109 | |
| 110 | $('#ie_content > div').appendTo('#import-export-tab'); |
| 111 | |
| 112 | } |
| 113 | |
| 114 | $('#post_name').on('change keyup', function(){ |
| 115 | set_sc_preview_text($(this).val()); |
| 116 | }); |
| 117 | |
| 118 | $('.sc_editor').on('focus', function(){ |
| 119 | window.sc_old_editor = $(this).val(); |
| 120 | }).on('change', function(e){ |
| 121 | |
| 122 | new_editor = $(this).val(); |
| 123 | response = confirm(SC_VARS.text_editor_switch_notice); |
| 124 | |
| 125 | if(!response){ |
| 126 | e.preventDefault(); |
| 127 | $(this).val(window.sc_old_editor); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | window.location = window.location + '&editor=' + $(this).val(); |
| 132 | |
| 133 | }); |
| 134 | |
| 135 | $('.sc_insert_param').on('click', function(e){ |
| 136 | |
| 137 | e.preventDefault(); |
| 138 | |
| 139 | var offset = $(this).offset(); |
| 140 | var mtop = offset.top + $(this).outerHeight(); |
| 141 | |
| 142 | $('.sc_params_list').css({ |
| 143 | top: mtop, |
| 144 | left: offset.left |
| 145 | }).toggle(); |
| 146 | |
| 147 | }); |
| 148 | |
| 149 | $('.sc_wp_params li').on('click', function(){ |
| 150 | insert_in_editor('$$' + $(this).data('id') + '$$'); |
| 151 | close_params_list(); |
| 152 | }); |
| 153 | |
| 154 | $('.sc_cp_btn').on('click', function(){ |
| 155 | |
| 156 | var $cp_box = $('.sc_cp_box'); |
| 157 | var $cp_default = $('.sc_cp_default'); |
| 158 | var $cp_info = $('.sc_cp_info'); |
| 159 | var param_val = $cp_box.val().trim(); |
| 160 | var default_val = $cp_default.val().trim(); |
| 161 | |
| 162 | if( param_val != '' && $cp_box[0].checkValidity() ){ |
| 163 | |
| 164 | var the_code = ''; |
| 165 | if(default_val == ''){ |
| 166 | the_code = '%%' + param_val + '%%'; |
| 167 | }else{ |
| 168 | the_code = '%%' + param_val + ':' + default_val + '%%'; |
| 169 | } |
| 170 | |
| 171 | insert_in_editor(the_code); |
| 172 | $cp_info.removeClass('red'); |
| 173 | close_params_list(); |
| 174 | }else{ |
| 175 | $cp_info.addClass('red'); |
| 176 | } |
| 177 | |
| 178 | }); |
| 179 | |
| 180 | $('.sc_cf_btn').on('click', function(){ |
| 181 | |
| 182 | var $cf_box = $('.sc_cf_box'); |
| 183 | var $cf_info = $('.sc_cf_info'); |
| 184 | var param_val = $cf_box.val().trim(); |
| 185 | |
| 186 | if( param_val != '' && $cf_box[0].checkValidity() ){ |
| 187 | insert_in_editor('$$custom_field:' + param_val + '$$'); |
| 188 | $cf_info.removeClass('red'); |
| 189 | close_params_list(); |
| 190 | }else{ |
| 191 | $cf_info.addClass('red'); |
| 192 | } |
| 193 | |
| 194 | }); |
| 195 | |
| 196 | $('.sc_copy').on('click', function(){ |
| 197 | copy_to_clipboard($('.sc_preview_text').text()); |
| 198 | $this = $(this); |
| 199 | $this.addClass('copied'); |
| 200 | setTimeout(function() { |
| 201 | $this.removeClass('copied'); |
| 202 | }, 3000); |
| 203 | }) |
| 204 | |
| 205 | $('.sc_changelog .dismiss_btn').on('click', function(){ |
| 206 | var url = SC_VARS.ajax_url + '?action=sc_admin_ajax&do=close_changelog'; |
| 207 | $.get(url, function( data ){ |
| 208 | if(data.search( /done/g ) == -1){ |
| 209 | $( '.sc_changelog article' ).html('Failed to close window. <a href="' + url + '" target="_blank">Please click here to close</a>'); |
| 210 | }else{ |
| 211 | $( '.sc_changelog' ).fadeOut(); |
| 212 | } |
| 213 | }); |
| 214 | }); |
| 215 | |
| 216 | $('.cfe_amt').on('click', function(){ |
| 217 | var $btn = $(this).closest('.cfe_form').find('.cfe_btn'); |
| 218 | $btn.attr('href', $btn.data('link') + $(this).val()); |
| 219 | }); |
| 220 | |
| 221 | init(); |
| 222 | |
| 223 | }); |
| 224 | })( jQuery ); |