wp/wp-content/plugins/option-tree/assets/js/ot-postformats.js
changeset 5 5e2f62d02dcd
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
       
     1 /*!
       
     2  * postformats.js v1.0.1
       
     3  */
       
     4 !function ($) {
       
     5 
       
     6   "use strict"; // jshint ;_;
       
     7 
       
     8   /* POSTFORMATS CLASS DEFINITION
       
     9    * ====================== */
       
    10   var formats = "input.post-format"
       
    11     , metaboxes = [
       
    12           '#ot-post-format-gallery'
       
    13         , '#ot-post-format-link'
       
    14         , '#ot-post-format-image'
       
    15         , '#ot-post-format-quote'
       
    16         , '#ot-post-format-video'
       
    17         , '#ot-post-format-audio'
       
    18       ]
       
    19     , ids = metaboxes.join(',')
       
    20     , insertAfter = '#titlediv'
       
    21     , imageBox = '#postimagediv'
       
    22     , placeholder = 'postimagediv-placeholder'
       
    23     , Postformats = function (element, options) {
       
    24         this.$element = $(element)
       
    25           .on('click.postformats.data-api', $.proxy(this.toggle, this))
       
    26         this.$id = this.$element.attr('id')
       
    27         this.init()
       
    28       }
       
    29 
       
    30   Postformats.prototype = {
       
    31 
       
    32     constructor: Postformats
       
    33   
       
    34   , init: function () {
       
    35 
       
    36       // Moves the metaboxes into place
       
    37       $( '#ot-' + this.$id ).insertAfter( $( insertAfter ) ).hide()
       
    38       
       
    39       // Show the checked metabox
       
    40       if ( this.$element.is(':checked') ) {
       
    41       
       
    42         this.show()
       
    43         
       
    44       }
       
    45       
       
    46     }
       
    47     
       
    48   , toggle: function () {
       
    49 
       
    50       // Hides all the post format metaboxes
       
    51       $(ids).each(function() {
       
    52       
       
    53         $(this).hide()
       
    54         
       
    55       })
       
    56       
       
    57       // Shows the clicked post format metabox
       
    58       this.show()
       
    59       
       
    60     }
       
    61   
       
    62   , show: function () {
       
    63       
       
    64       // Featured image is never really hidden so it requires different code 
       
    65       if ( this.$id == 'post-format-image' ) {
       
    66         
       
    67         if ( $( '#' + placeholder ).length == 0 )
       
    68           $( imageBox ).after( '<div id="' + placeholder + '"></div>' ).insertAfter( insertAfter ).css({'marginTop':'20px','marginBottom':'0px'}).find('h3 span').text(option_tree.with)
       
    69         
       
    70       // Revert image
       
    71       } else {
       
    72 
       
    73         $( '#' + placeholder ).replaceWith( $( imageBox ).css({'marginTop':'0px','marginBottom':'20px'}) )
       
    74         $( imageBox ).find('h3 span').text(option_tree.replace)
       
    75         
       
    76       }
       
    77       
       
    78       // Show the metabox
       
    79       $( '#ot-' + this.$id ).css({'marginTop':'20px','marginBottom':'0px'}).show()
       
    80       
       
    81     }
       
    82   
       
    83   }
       
    84     
       
    85   /* POSTFORMATS PLUGIN DEFINITION
       
    86    * ======================= */
       
    87   var old = $.fn.postformats
       
    88 
       
    89   $.fn.postformats = function (option) {
       
    90     return this.each(function () {
       
    91       var $this = $(this)
       
    92         , data = $this.data('postformats')
       
    93         , options = typeof option == 'object' && option
       
    94       if (!data) $this.data('postformats', (data = new Postformats(this, options)))
       
    95       if (typeof option == 'string') data[option]()
       
    96     })
       
    97   }
       
    98 
       
    99   $.fn.postformats.Constructor = Postformats
       
   100   
       
   101   /* POSTFORMATS NO CONFLICT
       
   102    * ================= */
       
   103   $.fn.postformats.noConflict = function () {
       
   104     $.fn.postformats = old
       
   105     return this
       
   106   }
       
   107 
       
   108   /* POSTFORMATS DATA-API
       
   109    * ============== */
       
   110   $(document).on('ready.postformats.data-api', function () {
       
   111     $(formats).each(function () {
       
   112       $(this).postformats()
       
   113     })
       
   114   })
       
   115 
       
   116 }(window.jQuery);