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
208 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('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_coffee_btn(); |
| 36 | |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | $('.sc_params_list').appendTo('body'); |
| 41 | |
| 42 | } |
| 43 | |
| 44 | var set_sc_preview_text = function(name){ |
| 45 | $('.sc_preview_text').text('[sc name="' + name + '"]'); |
| 46 | } |
| 47 | |
| 48 | var insert_in_editor = function(data){ |
| 49 | console.log(data); |
| 50 | if(window.SC_EDITOR == 'code'){ |
| 51 | var doc = window.sc_cm.getDoc(); |
| 52 | doc.replaceRange(data, doc.getCursor()); |
| 53 | }else{ |
| 54 | send_to_editor(data); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | var copy_to_clipboard = function(str){ |
| 59 | var el = document.createElement('textarea'); |
| 60 | el.value = str; |
| 61 | el.setAttribute('readonly', ''); |
| 62 | el.style.position = 'absolute'; |
| 63 | el.style.left = '-9999px'; |
| 64 | document.body.appendChild(el); |
| 65 | el.select(); |
| 66 | document.execCommand('copy'); |
| 67 | document.body.removeChild(el); |
| 68 | }; |
| 69 | |
| 70 | var load_cm_sc_mode = function(){ |
| 71 | |
| 72 | if(typeof CodeMirror.overlayMode === 'undefined'){ |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | CodeMirror.defineMode('sc_mode', function(config, parserConfig){ |
| 77 | var mustacheOverlay = { |
| 78 | token: function(stream, state){ |
| 79 | if(stream.match(/\$\$[a-z0-9A-Z:_]+\$\$/)){ |
| 80 | return 'number sc_param'; |
| 81 | } |
| 82 | if(stream.match(/%%[a-z0-9A-Z_]+%%/)){ |
| 83 | return 'atom sc_param'; |
| 84 | } |
| 85 | stream.next(); |
| 86 | } |
| 87 | }; |
| 88 | return CodeMirror.overlayMode(CodeMirror.getMode(config, parserConfig.backdrop || 'htmlmixed'), mustacheOverlay); |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | var close_params_list = function(){ |
| 93 | $('.sc_params_list').hide(); |
| 94 | } |
| 95 | |
| 96 | var add_top_coffee_btn = function(){ |
| 97 | |
| 98 | $('#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>'); |
| 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 | $('#screen-meta').append('<div id="import-export-tab" class="hidden"></div>'); |
| 103 | |
| 104 | $('#ie_content > div').appendTo('#import-export-tab'); |
| 105 | |
| 106 | } |
| 107 | |
| 108 | $('#post_name').on('change keyup', function(){ |
| 109 | set_sc_preview_text($(this).val()); |
| 110 | }); |
| 111 | |
| 112 | $('.sc_editor').on('focus', function(){ |
| 113 | window.sc_old_editor = $(this).val(); |
| 114 | }).on('change', function(e){ |
| 115 | |
| 116 | new_editor = $(this).val(); |
| 117 | response = confirm(SC_VARS.text_editor_switch_notice); |
| 118 | |
| 119 | if(!response){ |
| 120 | e.preventDefault(); |
| 121 | $(this).val(window.sc_old_editor); |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | window.location = window.location + '&editor=' + $(this).val(); |
| 126 | |
| 127 | }); |
| 128 | |
| 129 | $('.sc_insert_param').on('click', function(e){ |
| 130 | |
| 131 | e.preventDefault(); |
| 132 | |
| 133 | var offset = $(this).offset(); |
| 134 | var mtop = offset.top + $(this).outerHeight(); |
| 135 | |
| 136 | $('.sc_params_list').css({ |
| 137 | top: mtop, |
| 138 | left: offset.left |
| 139 | }).toggle(); |
| 140 | |
| 141 | }); |
| 142 | |
| 143 | $('.sc_wp_params li').on('click', function(){ |
| 144 | insert_in_editor('$$' + $(this).data('id') + '$$'); |
| 145 | close_params_list(); |
| 146 | }); |
| 147 | |
| 148 | $('.sc_cp_btn').on('click', function(){ |
| 149 | |
| 150 | var $cp_box = $('.sc_cp_box'); |
| 151 | var $cp_info = $('.sc_cp_info'); |
| 152 | var param_val = $cp_box.val().trim(); |
| 153 | |
| 154 | if( param_val != '' && $cp_box[0].checkValidity() ){ |
| 155 | insert_in_editor('%%' + param_val + '%%'); |
| 156 | $cp_info.removeClass('red'); |
| 157 | close_params_list(); |
| 158 | }else{ |
| 159 | $cp_info.addClass('red'); |
| 160 | } |
| 161 | |
| 162 | }); |
| 163 | |
| 164 | $('.sc_cf_btn').on('click', function(){ |
| 165 | |
| 166 | var $cf_box = $('.sc_cf_box'); |
| 167 | var $cf_info = $('.sc_cf_info'); |
| 168 | var param_val = $cf_box.val().trim(); |
| 169 | |
| 170 | if( param_val != '' && $cf_box[0].checkValidity() ){ |
| 171 | insert_in_editor('$$custom_field:' + param_val + '$$'); |
| 172 | $cf_info.removeClass('red'); |
| 173 | close_params_list(); |
| 174 | }else{ |
| 175 | $cf_info.addClass('red'); |
| 176 | } |
| 177 | |
| 178 | }); |
| 179 | |
| 180 | $('.sc_copy').on('click', function(){ |
| 181 | copy_to_clipboard($('.sc_preview_text').text()); |
| 182 | $this = $(this); |
| 183 | $this.addClass('copied'); |
| 184 | setTimeout(function() { |
| 185 | $this.removeClass('copied'); |
| 186 | }, 3000); |
| 187 | }) |
| 188 | |
| 189 | $('.sc_changelog .dismiss_btn').on('click', function(){ |
| 190 | var url = SC_VARS.ajax_url + '?action=sc_admin_ajax&do=close_changelog'; |
| 191 | $.get(url, function( data ){ |
| 192 | if(data.search( /done/g ) == -1){ |
| 193 | $( '.sc_changelog article' ).html('Failed to close window. <a href="' + url + '" target="_blank">Please click here to close</a>'); |
| 194 | }else{ |
| 195 | $( '.sc_changelog' ).fadeOut(); |
| 196 | } |
| 197 | }); |
| 198 | }); |
| 199 | |
| 200 | $('.cfe_amt').on('click', function(){ |
| 201 | var $btn = $(this).closest('.cfe_form').find('.cfe_btn'); |
| 202 | $btn.attr('href', $btn.data('link') + $(this).val()); |
| 203 | }); |
| 204 | |
| 205 | init(); |
| 206 | |
| 207 | }); |
| 208 | })( jQuery ); |