picker.js
191 lines
| 1 | /* global menuIcons:false */ |
| 2 | |
| 3 | require( './media' ); |
| 4 | |
| 5 | ( function( $ ) { |
| 6 | var miPicker; |
| 7 | |
| 8 | if ( ! menuIcons.activeTypes || _.isEmpty( menuIcons.activeTypes ) ) { |
| 9 | return; |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | * @property {object} templates - Cached templates for the item previews on the fields |
| 15 | * @property {string} wrapClass - Field wrapper's class |
| 16 | * @property {object} frame - Menu Icons' media frame instance |
| 17 | * @property {object} target - Frame's target model |
| 18 | */ |
| 19 | miPicker = { |
| 20 | templates: {}, |
| 21 | wrapClass: 'div.menu-icons-wrap', |
| 22 | frame: null, |
| 23 | target: new wp.media.model.IconPickerTarget(), |
| 24 | |
| 25 | /** |
| 26 | * Callback function to filter active icon types |
| 27 | * |
| 28 | * TODO: Maybe move to frame view? |
| 29 | * |
| 30 | * @param {string} type - Icon type. |
| 31 | */ |
| 32 | typesFilter: function( type ) { |
| 33 | return ( $.inArray( type.id, menuIcons.activeTypes ) >= 0 ); |
| 34 | }, |
| 35 | |
| 36 | /** |
| 37 | * Create Menu Icons' media frame |
| 38 | */ |
| 39 | createFrame: function() { |
| 40 | miPicker.frame = new wp.media.view.MediaFrame.MenuIcons({ |
| 41 | target: miPicker.target, |
| 42 | ipTypes: _.filter( iconPicker.types, miPicker.typesFilter ), |
| 43 | SidebarView: wp.media.view.MenuIconsSidebar |
| 44 | }); |
| 45 | }, |
| 46 | |
| 47 | /** |
| 48 | * Pick icon for a menu item and open the frame |
| 49 | * |
| 50 | * @param {object} model - Menu item model. |
| 51 | */ |
| 52 | pickIcon: function( model ) { |
| 53 | miPicker.frame.target.set( model, { silent: true }); |
| 54 | miPicker.frame.open(); |
| 55 | }, |
| 56 | |
| 57 | /** |
| 58 | * Set or unset icon |
| 59 | * |
| 60 | * @param {object} e - jQuery click event. |
| 61 | */ |
| 62 | setUnset: function( e ) { |
| 63 | var $el = $( e.currentTarget ), |
| 64 | $clicked = $( e.target ); |
| 65 | |
| 66 | e.preventDefault(); |
| 67 | |
| 68 | if ( $clicked.hasClass( '_select' ) || $clicked.hasClass( '_icon' ) ) { |
| 69 | miPicker.setIcon( $el ); |
| 70 | } else if ( $clicked.hasClass( '_remove' ) ) { |
| 71 | miPicker.unsetIcon( $el ); |
| 72 | } |
| 73 | }, |
| 74 | |
| 75 | /** |
| 76 | * Set Icon |
| 77 | * |
| 78 | * @param {object} $el - jQuery object. |
| 79 | */ |
| 80 | setIcon: function( $el ) { |
| 81 | var id = $el.data( 'id' ), |
| 82 | frame = miPicker.frame, |
| 83 | items = frame.menuItems, |
| 84 | model = items.get( id ); |
| 85 | |
| 86 | if ( model ) { |
| 87 | miPicker.pickIcon( model.toJSON() ); |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | model = { |
| 92 | id: id, |
| 93 | $el: $el, |
| 94 | $title: $( '#edit-menu-item-title-' + id ), |
| 95 | $inputs: {} |
| 96 | }; |
| 97 | |
| 98 | // Collect menu item's settings fields and use them |
| 99 | // as the model's attributes. |
| 100 | $el.find( 'div._settings input' ).each( function() { |
| 101 | var $input = $( this ), |
| 102 | key = $input.attr( 'class' ).replace( '_mi-', '' ), |
| 103 | value = $input.val(); |
| 104 | |
| 105 | if ( ! value ) { |
| 106 | if ( _.has( menuIcons.menuSettings, key ) ) { |
| 107 | value = menuIcons.menuSettings[ key ]; |
| 108 | } else if ( _.has( menuIcons.settingsFields, key ) ) { |
| 109 | value = menuIcons.settingsFields[ key ]['default']; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | model[ key ] = value; |
| 114 | model.$inputs[ key ] = $input; |
| 115 | }); |
| 116 | |
| 117 | items.add( model ); |
| 118 | miPicker.pickIcon( model ); |
| 119 | }, |
| 120 | |
| 121 | /** |
| 122 | * Unset icon |
| 123 | * |
| 124 | * @param {object} $el - jQuery object. |
| 125 | */ |
| 126 | unsetIcon: function( $el ) { |
| 127 | var id = $el.data( 'id' ); |
| 128 | |
| 129 | $el.find( 'div._settings input' ).val( '' ); |
| 130 | $el.trigger( 'mi:update' ); |
| 131 | miPicker.frame.menuItems.remove( id ); |
| 132 | }, |
| 133 | |
| 134 | /** |
| 135 | * Update valeus of menu item's setting fields |
| 136 | * |
| 137 | * When the type and icon is set, this will (re)generate the icon |
| 138 | * preview on the menu item field. |
| 139 | * |
| 140 | * @param {object} e - jQuery event. |
| 141 | */ |
| 142 | updateField: function( e ) { |
| 143 | var $el = $( e.currentTarget ), |
| 144 | $set = $el.find( 'a._select' ), |
| 145 | $unset = $el.find( 'a._remove' ), |
| 146 | type = $el.find( 'input._mi-type' ).val(), |
| 147 | icon = $el.find( 'input._mi-icon' ).val(), |
| 148 | url = $el.find( 'input._mi-url' ).val(), |
| 149 | template; |
| 150 | |
| 151 | if ( type === '' || icon === '' || _.indexOf( menuIcons.activeTypes, type ) < 0 ) { |
| 152 | $set.text( menuIcons.text.select ).attr( 'title', '' ); |
| 153 | $unset.addClass( 'hidden' ); |
| 154 | |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | if ( miPicker.templates[ type ]) { |
| 159 | template = miPicker.templates[ type ]; |
| 160 | } else { |
| 161 | template = miPicker.templates[ type ] = wp.template( 'menu-icons-item-field-preview-' + iconPicker.types[ type ].templateId ); |
| 162 | } |
| 163 | |
| 164 | $unset.removeClass( 'hidden' ); |
| 165 | $set.attr( 'title', menuIcons.text.change ); |
| 166 | $set.html( template({ |
| 167 | type: type, |
| 168 | icon: icon, |
| 169 | url: url |
| 170 | }) ); |
| 171 | }, |
| 172 | |
| 173 | /** |
| 174 | * Initialize picker functionality |
| 175 | * |
| 176 | * #fires mi:update |
| 177 | */ |
| 178 | init: function() { |
| 179 | miPicker.createFrame(); |
| 180 | $( document ) |
| 181 | .on( 'click', miPicker.wrapClass, miPicker.setUnset ) |
| 182 | .on( 'mi:update', miPicker.wrapClass, miPicker.updateField ); |
| 183 | |
| 184 | // Trigger 'mi:update' event to generate the icons on the item fields. |
| 185 | $( miPicker.wrapClass ).trigger( 'mi:update' ); |
| 186 | } |
| 187 | }; |
| 188 | |
| 189 | miPicker.init(); |
| 190 | }( jQuery ) ); |
| 191 |