Advertisement
Guest User

Untitled

a guest
Jun 24th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // closure to avoid namespace collision
  2. (function(){
  3.     // creates the plugin
  4.     tinymce.create('tinymce.plugins.mygallery', {
  5.         // creates control instances based on the control's id.
  6.         // our button's id is "mygallery_button"
  7.         createControl : function(id, controlManager) {
  8.             if (id == 'mygallery_button') {
  9.                 // creates the button
  10.                 var button = controlManager.createButton('mygallery_button', {
  11.                     title : 'MyGallery Shortcode', // title of the button
  12.                     image : '../wp-includes/images/smilies/icon_mrgreen.gif',  // path to the button's image
  13.                     onclick : function() {
  14.                         // triggers the thickbox
  15.                         var width = jQuery(window).width(), H = jQuery(window).height(), W = ( 720 < width ) ? 720 : width;
  16.                         W = W - 80;
  17.                         H = H - 84;
  18.                         tb_show( 'My Gallery Shortcode', '#TB_inline?width=' + W + '&height=' + H + '&inlineId=mygallery-form' );
  19.                     }
  20.                 });
  21.                 return button;
  22.             }
  23.             return null;
  24.         }
  25.     });
  26.    
  27.     // registers the plugin. DON'T MISS THIS STEP!!!
  28.     tinymce.PluginManager.add('mygallery', tinymce.plugins.mygallery);
  29.    
  30.     // executes this when the DOM is ready
  31.     jQuery(function(){
  32.         // creates a form to be displayed everytime the button is clicked
  33.         // you should achieve this using AJAX instead of direct html code like this
  34.         var form = jQuery('<div id="mygallery-form"><table id="mygallery-table" class="form-table">\
  35.             <tr>\
  36.                 <th><label for="mygallery-columns">Columns</label></th>\
  37.                 <td><input type="text" id="mygallery-columns" name="columns" value="3" /><br />\
  38.                 <small>specify the number of columns.</small></td>\
  39.             </tr>\
  40.             <tr>\
  41.                 <th><label for="mygallery-id">Post ID</label></th>\
  42.                 <td><input type="text" name="id" id="mygallery-id" value="" /><br />\
  43.                 <small>specify the post ID. Leave blank if you want to use the current post.</small>\
  44.             </tr>\
  45.             <tr>\
  46.                 <th><label for="mygallery-size">Size</label></th>\
  47.                 <td><select name="size" id="mygallery-size">\
  48.                     <option value="thumbnail">Thumbnail</option>\
  49.                     <option value="medium">Medium</option>\
  50.                     <option value="large">Large</option>\
  51.                     <option value="full">Full</option>\
  52.                 </select><br />\
  53.                 <small>specify the image size to use for the thumbnail display.</small></td>\
  54.             </tr>\
  55.             <tr>\
  56.                 <th><label for="mygallery-orderby">Order By</label></th>\
  57.                 <td><input type="text" name="orderby" id="mygallery-orderby" value="menu_order ASC, ID ASC" /><br /><small>RAND (random) is also supported.</small></td>\
  58.             </tr>\
  59.             <tr>\
  60.                 <th><label for="mygallery-itemtag">Item Tag</label></th>\
  61.                 <td><input type="text" name="itemtag" id="mygallery-itemtag" value="dl" /><br />\
  62.                     <small>the name of the XHTML tag used to enclose each item in the gallery.</small></td>\
  63.             </tr>\
  64.             <tr>\
  65.                 <th><label for="mygallery-icontag">Icon Tag</label></th>\
  66.                 <td><input type="text" name="icontag" id="mygallery-icontag" value="dt" /><br />\
  67.                     <small>the name of the XHTML tag used to enclose each thumbnail icon in the gallery.</small></td>\
  68.             </tr>\
  69.             <tr>\
  70.                 <th><label for="mygallery-captiontag">Caption Tag</label></th>\
  71.                 <td><input type="text" name="captiontag" id="mygallery-captiontag" value="dd" /><br />\
  72.                     <small>the name of the XHTML tag used to enclose each caption.</small></td>\
  73.             </tr>\
  74.             <tr>\
  75.                 <th><label for="mygallery-link">Link</label></th>\
  76.                 <td><input type="text" name="link" id="mygallery-link" value="" /><br />\
  77.                     <small>you can set it to "file" so each image will link to the image file, otherwise leave blank.</small></td>\
  78.             </tr>\
  79.             <tr>\
  80.                 <th><label for="mygallery-include">Include Attachment IDs</label></th>\
  81.                 <td><input type="text" name="include" id="mygallery-include" value="" /><br />\
  82.                     <small>comma separated attachment IDs</small>\
  83.                 </td>\
  84.             </tr>\
  85.             <tr>\
  86.                 <th><label for="mygallery-exclude">Exclude Attachment IDs</label></th>\
  87.                 <td><input type="text" id="mygallery-exclude" name="exclude" value="" /><br />\
  88.                     <small>comma separated attachment IDs</small>\
  89.                 </td>\
  90.             </tr>\
  91.         </table>\
  92.         <p class="submit">\
  93.             <input type="button" id="mygallery-submit" class="button-primary" value="Insert Gallery" name="submit" />\
  94.         </p>\
  95.         </div>');
  96.        
  97.         var table = form.find('table');
  98.         form.appendTo('body').hide();
  99.        
  100.         // handles the click event of the submit button
  101.         form.find('#mygallery-submit').click(function(){
  102.             // defines the options and their default values
  103.             // again, this is not the most elegant way to do this
  104.             // but well, this gets the job done nonetheless
  105.             var options = {
  106.                 'columns'    : '3',
  107.                 'id'         : '',
  108.                 'size'       : 'thumbnail',
  109.                 'orderby'    : 'menu_order ASC, ID ASC',
  110.                 'itemtag'    : 'dl',
  111.                 'icontag'    : 'dt',
  112.                 'captiontag' : 'dd',
  113.                 'link'       : '',
  114.                 'include'    : '',
  115.                 'exclude'    : ''
  116.                 };
  117.             var shortcode = '[gallery';
  118.            
  119.             for( var index in options) {
  120.                 var value = table.find('#mygallery-' + index).val();
  121.                
  122.                 // attaches the attribute to the shortcode only if it's different from the default value
  123.                 if ( value !== options[index] )
  124.                     shortcode += ' ' + index + '="' + value + '"';
  125.             }
  126.            
  127.             shortcode += ']';
  128.            
  129.             // inserts the shortcode into the active editor
  130.             tinyMCE.activeEditor.execCommand('mceInsertContent', 0, shortcode);
  131.            
  132.             // closes Thickbox
  133.             tb_remove();
  134.         });
  135.     });
  136. })()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement