| 1 |
(function($) { |
| 2 |
$(document).on('tinymce-editor-setup', function(event, editor) { |
| 3 |
|
| 4 |
if( void 0 === wbcr_inp_shortcode_snippets ) { |
| 5 |
console.log('Unknown error.'); |
| 6 |
return; |
| 7 |
} |
| 8 |
|
| 9 |
if( $.isEmptyObject(wbcr_inp_shortcode_snippets) ) { |
| 10 |
return; |
| 11 |
} |
| 12 |
|
| 13 |
editor.settings.toolbar1 += ',wbcr_insert_php_button'; |
| 14 |
|
| 15 |
var menu = []; |
| 16 |
|
| 17 |
$.each(wbcr_inp_shortcode_snippets, function(index, item) { |
| 18 |
menu.push({ |
| 19 |
text: item.title, |
| 20 |
value: item.id, |
| 21 |
onclick: function() { |
| 22 |
var content = ""; |
| 23 |
var snippet_type = item.type; |
| 24 |
var snippet_name = item.name; |
| 25 |
var selected_content = editor.selection.getContent(); |
| 26 |
|
| 27 |
for( var tag in item ) { |
| 28 |
if( !item.hasOwnProperty(tag) ) { |
| 29 |
continue; |
| 30 |
} |
| 31 |
|
| 32 |
if( 'type' === tag ) { |
| 33 |
snippet_type = item[tag]; |
| 34 |
} else if( 'name' === tag ) { |
| 35 |
snippet_name = item[tag]; |
| 36 |
} else if( tag.indexOf('snippet_tags') === -1 ) { |
| 37 |
if( !('' !== selected_content && 'content' === tag) ) { |
| 38 |
content += ' ' + tag + '="' + item[tag] + '"'; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
if( '' === snippet_name || undefined === snippet_name ) { |
| 44 |
if( 'universal' === snippet_type ) { |
| 45 |
snippet_name = "wbcr_snippet"; |
| 46 |
} else { |
| 47 |
snippet_name = "wbcr_" + snippet_type + "_snippet"; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
if( '' === selected_content ) { |
| 52 |
editor.selection.setContent('[' + snippet_name + content + ']'); |
| 53 |
} else { |
| 54 |
editor.selection.setContent( |
| 55 |
'[' + snippet_name + content + ']' + |
| 56 |
selected_content + |
| 57 |
'[/' + snippet_name + ']'); |
| 58 |
} |
| 59 |
} |
| 60 |
}); |
| 61 |
}); |
| 62 |
|
| 63 |
editor.addButton('wbcr_insert_php_button', { |
| 64 |
title: wbcr_inp_tinymce_snippets_button_title, |
| 65 |
type: 'menubutton', |
| 66 |
icon: 'icon wbcr-inp-shortcode-icon', |
| 67 |
menu: menu |
| 68 |
}); |
| 69 |
|
| 70 |
}); |
| 71 |
})(jQuery); |