admin.js
11 years ago
admin.min.js
11 years ago
input-dependencies.js
11 years ago
input-dependencies.min.js
11 years ago
admin.js
1056 lines
| 1 | /* global jQuery, wp, window: false, Backbone: false, _: false */ |
| 2 | /** |
| 3 | * Menu Icons |
| 4 | * |
| 5 | * @author Dzikri Aziz <kvcrvt@gmail.com> |
| 6 | * @version 0.1.0 |
| 7 | * |
| 8 | */ |
| 9 | (function($) { |
| 10 | 'use strict'; |
| 11 | |
| 12 | $.inputDependencies({ |
| 13 | selector : 'select.hasdep', |
| 14 | disable : false |
| 15 | }); |
| 16 | |
| 17 | /** |
| 18 | * Settings box tabs |
| 19 | * |
| 20 | * We can't use core's tabs script here because it will clear the |
| 21 | * checkboxes upon tab switching |
| 22 | */ |
| 23 | $('#menu-icons-settings-tabs') |
| 24 | .on('click', 'a.mi-settings-nav-tab', function(e) { |
| 25 | e.preventDefault(); |
| 26 | e.stopPropagation(); |
| 27 | |
| 28 | var $el = $(this).blur(); |
| 29 | var $target = $( '#'+$el.data('type') ); |
| 30 | |
| 31 | $el.parent().addClass('tabs').siblings().removeClass('tabs'); |
| 32 | $target |
| 33 | .removeClass('tabs-panel-inactive') |
| 34 | .addClass('tabs-panel-active') |
| 35 | .show() |
| 36 | .siblings('div.tabs-panel') |
| 37 | .hide() |
| 38 | .addClass('tabs-panel-inactive') |
| 39 | .removeClass('tabs-panel-active'); |
| 40 | }) |
| 41 | .find('a.mi-settings-nav-tab').first().click(); |
| 42 | |
| 43 | |
| 44 | if ( 'undefined' === typeof window.menuIcons ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if ( undefined === window.menuIcons.iconTypes ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | window.menuIcons = _.defaults({ |
| 53 | frame : '', |
| 54 | currentItem : {}, |
| 55 | |
| 56 | toggleSelect : function(e) { |
| 57 | var $type = $(e.currentTarget); |
| 58 | var $wrapr = $type.closest('div.menu-icons-wrap'); |
| 59 | var $select = $wrapr.find('a._select'); |
| 60 | var $remove = $wrapr.find('a._remove'); |
| 61 | |
| 62 | if ( '' !== $type.val() ) { |
| 63 | $remove.show(); |
| 64 | } |
| 65 | else { |
| 66 | $select.text( $select.data('text') ); |
| 67 | $remove.hide(); |
| 68 | } |
| 69 | }, |
| 70 | |
| 71 | selectIcon : function(e) { |
| 72 | e.preventDefault(); |
| 73 | e.stopPropagation(); |
| 74 | |
| 75 | var $el = $(this); |
| 76 | var id = media.view.settings.post.id = $el.data('id'); |
| 77 | var attrs = { |
| 78 | id : id, |
| 79 | title : $('#edit-menu-item-title-'+id).val() |
| 80 | }; |
| 81 | |
| 82 | $el.closest('div.menu-icons-wrap').find(':input').each(function(i, input) { |
| 83 | var key = $(input).data('key'); |
| 84 | attrs[ key ] = input.value; |
| 85 | }); |
| 86 | |
| 87 | window.menuIcons.currentItem = attrs; |
| 88 | |
| 89 | if ( ! ( window.menuIcons.frame instanceof media.view.MediaFrame.menuIcons ) ) { |
| 90 | window.menuIcons.frame = new media.view.MediaFrame.menuIcons(); |
| 91 | } |
| 92 | |
| 93 | window.menuIcons.frame.open(); |
| 94 | }, |
| 95 | |
| 96 | removeIcon : function(e) { |
| 97 | e.preventDefault(); |
| 98 | e.stopPropagation(); |
| 99 | |
| 100 | var id = $(this).data('id'); |
| 101 | |
| 102 | $('#menu-icons-'+ id +'-type').val('').trigger('mi:update'); |
| 103 | } |
| 104 | }, window.menuIcons); |
| 105 | |
| 106 | |
| 107 | // WP Media |
| 108 | var media = wp.media; |
| 109 | var Attachment = media.model.Attachment; |
| 110 | |
| 111 | |
| 112 | // Models |
| 113 | media.model.mi = {}; |
| 114 | |
| 115 | // Model: Menu Items |
| 116 | media.model.mi.MenuItems = Backbone.Collection.extend({ |
| 117 | props : new Backbone.Model({ item : '' }), |
| 118 | model : Backbone.Model.extend({ |
| 119 | defaults : { |
| 120 | type : '', |
| 121 | group : 'all', |
| 122 | icon : '' |
| 123 | }, |
| 124 | }) |
| 125 | }); |
| 126 | |
| 127 | // Model: Settings fields |
| 128 | media.model.mi.MenuItems.Settings = Backbone.Collection.extend({ |
| 129 | model : Backbone.Model.extend({ |
| 130 | defaults : { |
| 131 | id : '', |
| 132 | label : '', |
| 133 | value : '', |
| 134 | type : 'text' |
| 135 | } |
| 136 | }) |
| 137 | }); |
| 138 | |
| 139 | // All: Sidebar |
| 140 | media.view.miSidebar = media.view.Sidebar.extend({ |
| 141 | initialize : function() { |
| 142 | var title = new media.View({ |
| 143 | tagName : 'h3', |
| 144 | priority : -10 |
| 145 | }); |
| 146 | var info = new media.View({ |
| 147 | tagName : 'p', |
| 148 | className : '_info', |
| 149 | priority : 1000 |
| 150 | }); |
| 151 | |
| 152 | media.view.Sidebar.prototype.initialize.apply( this, arguments ); |
| 153 | |
| 154 | title.$el.text( window.menuIcons.text.preview ); |
| 155 | this.set( 'title', title ); |
| 156 | |
| 157 | info.$el.html( window.menuIcons.text.settingsInfo ); |
| 158 | this.set( 'info', info ); |
| 159 | } |
| 160 | }); |
| 161 | |
| 162 | // View: Settings wrapper |
| 163 | media.view.miSidebar.Settings = media.view.PriorityList.extend({ |
| 164 | className : 'mi-settings attachment-info', |
| 165 | |
| 166 | prepare : function() { |
| 167 | _.each( this.collection.map( this.createField, this ), function( view ) { |
| 168 | this.set( view.model.id, view ); |
| 169 | }, this ); |
| 170 | }, |
| 171 | |
| 172 | createField : function( model ) { |
| 173 | var field = new media.view.miSidebar.Settings.Field({ |
| 174 | item : this.model, |
| 175 | model : model, |
| 176 | collection : this.collection |
| 177 | }); |
| 178 | |
| 179 | return field; |
| 180 | } |
| 181 | }); |
| 182 | |
| 183 | // View: Settings field |
| 184 | media.view.miSidebar.Settings.Field = media.View.extend({ |
| 185 | tagName : 'label', |
| 186 | className : 'setting', |
| 187 | events : { |
| 188 | 'change :input' : '_update' |
| 189 | }, |
| 190 | |
| 191 | initialize : function() { |
| 192 | media.View.prototype.initialize.apply( this, arguments ); |
| 193 | this.template = media.template( 'menu-icons-settings-field-'+this.model.get('type') ); |
| 194 | this.model.on( 'change', this.render, this ); |
| 195 | }, |
| 196 | |
| 197 | prepare : function() { |
| 198 | return this.model.toJSON(); |
| 199 | }, |
| 200 | |
| 201 | _update : function(e) { |
| 202 | var item = this.options.item; |
| 203 | var $input = $(e.currentTarget); |
| 204 | var value = $input.val(); |
| 205 | var $field = $('#menu-icons-'+ item.id +'-'+ this.model.id +'._setting'); |
| 206 | |
| 207 | this.model.set( 'value', value ); |
| 208 | item.set( this.model.id, value ); |
| 209 | $field.val( value ).trigger('mi:update'); |
| 210 | } |
| 211 | }); |
| 212 | |
| 213 | // View: Item preview on the sidebar |
| 214 | media.view.miPreview = media.View.extend({ |
| 215 | tagName : 'p', |
| 216 | className : 'mi-preview menu-item attachment-info', |
| 217 | events : { |
| 218 | 'click a' : 'preventDefault' |
| 219 | }, |
| 220 | |
| 221 | initialize : function() { |
| 222 | media.View.prototype.initialize.apply( this, arguments ); |
| 223 | this.model.on( 'change', this.render, this ); |
| 224 | }, |
| 225 | |
| 226 | render : function() { |
| 227 | var data = _.extend( this.model.toJSON(), this.options.data ); |
| 228 | var template = 'menu-icons-' + data.type + '-preview-'; |
| 229 | |
| 230 | if ( data.hide_label ) { |
| 231 | template += 'hide_label'; |
| 232 | } |
| 233 | else { |
| 234 | template += data.position; |
| 235 | } |
| 236 | |
| 237 | this.template = media.template( template ); |
| 238 | this.$el.html( this.template( data ) ); |
| 239 | |
| 240 | return this; |
| 241 | }, |
| 242 | |
| 243 | preventDefault: function(e) { |
| 244 | e.preventDefault(); |
| 245 | } |
| 246 | }); |
| 247 | |
| 248 | // Methods for the browser view |
| 249 | media.view.miBrowser = { |
| 250 | createSidebar : function() { |
| 251 | var options = this.options; |
| 252 | var selection = options.selection; |
| 253 | var sidebar = this.sidebar = new media.view.miSidebar({ |
| 254 | controller : this.controller, |
| 255 | type : options.type |
| 256 | }); |
| 257 | |
| 258 | this.views.add( sidebar ); |
| 259 | |
| 260 | selection.on( 'selection:single', this.createSingle, this ); |
| 261 | selection.on( 'selection:unsingle', this.disposeSingle, this ); |
| 262 | |
| 263 | if ( selection.single() ) { |
| 264 | this.createSingle(); |
| 265 | } |
| 266 | }, |
| 267 | |
| 268 | createSingle : function() { |
| 269 | this.createPreview(); |
| 270 | }, |
| 271 | |
| 272 | createSettings : function() { |
| 273 | var item = this.controller.miGetCurrentItem(); |
| 274 | var fields = this.model.get('settings'); |
| 275 | |
| 276 | if ( ! fields.length ) { |
| 277 | return; |
| 278 | } |
| 279 | |
| 280 | _.each( fields, function( field ) { |
| 281 | field.value = item.get( field.id ); |
| 282 | } ); |
| 283 | |
| 284 | this.sidebar.set( 'settings', new media.view.miSidebar.Settings({ |
| 285 | controller : this.controller, |
| 286 | collection : new media.model.mi.MenuItems.Settings( fields ), |
| 287 | model : item, |
| 288 | type : this.options.type, |
| 289 | priority : 120 |
| 290 | }) ); |
| 291 | } |
| 292 | }; |
| 293 | |
| 294 | // View: Font icon: Browser |
| 295 | media.view.miFont = media.View.extend({ |
| 296 | className : 'attachments-browser mi-items-wrap', |
| 297 | |
| 298 | initialize : function() { |
| 299 | this.createToolbar(); |
| 300 | this.createLibrary(); |
| 301 | this.createSidebar(); |
| 302 | }, |
| 303 | |
| 304 | createLibrary : function() { |
| 305 | this.items = new media.view.miFont.Library({ |
| 306 | controller : this.controller, |
| 307 | collection : this.collection, |
| 308 | selection : this.options.selection, |
| 309 | type : this.options.type, |
| 310 | data : this.options.data |
| 311 | }); |
| 312 | this.views.add( this.items ); |
| 313 | }, |
| 314 | |
| 315 | createToolbar : function() { |
| 316 | var library = this.collection; |
| 317 | var group = library.props.get('group'); |
| 318 | |
| 319 | this.toolbar = new media.view.Toolbar({ |
| 320 | controller : this.controller |
| 321 | }); |
| 322 | this.views.add( this.toolbar ); |
| 323 | |
| 324 | |
| 325 | |
| 326 | // Dropdown filter |
| 327 | this.toolbar.set( 'filters', new media.view.miFont.Filters({ |
| 328 | controller : this.controller, |
| 329 | model : this.collection.props, |
| 330 | priority : -80 |
| 331 | }).render() ); |
| 332 | |
| 333 | // Search field |
| 334 | this.toolbar.set( 'search', new media.view.Search({ |
| 335 | controller : this.controller, |
| 336 | model : this.collection.props, |
| 337 | priority : 60 |
| 338 | }).render() ); |
| 339 | }, |
| 340 | |
| 341 | createPreview : function() { |
| 342 | var controller = this.controller; |
| 343 | var menuItem = controller.miGetCurrentItem(); |
| 344 | var selected = this.model.get('selection').single(); |
| 345 | |
| 346 | this.createSettings(); |
| 347 | this.sidebar.set( 'preview', new media.view.miPreview({ |
| 348 | controller : controller, |
| 349 | model : menuItem, |
| 350 | data : { |
| 351 | type : selected.get('type'), |
| 352 | icon : selected.id |
| 353 | }, |
| 354 | priority : 80 |
| 355 | }) ); |
| 356 | }, |
| 357 | |
| 358 | disposeSingle : function() { |
| 359 | var sidebar = this.sidebar; |
| 360 | |
| 361 | sidebar.unset('preview'); |
| 362 | sidebar.unset('settings'); |
| 363 | } |
| 364 | }); |
| 365 | |
| 366 | _.extend( media.view.miFont.prototype, media.view.miBrowser ); |
| 367 | |
| 368 | // View: Font icon: Library |
| 369 | media.view.miFont.Library = media.View.extend({ |
| 370 | tagName : 'ul', |
| 371 | className : 'attachments mi-items clearfix', |
| 372 | |
| 373 | initialize : function() { |
| 374 | this._viewsByCid = {}; |
| 375 | this.collection.on( 'reset', this.refresh, this ); |
| 376 | this.controller.on( 'open', this.scrollToSelected, this ); |
| 377 | }, |
| 378 | |
| 379 | render : function() { |
| 380 | this.collection.each( function( model ) { |
| 381 | this.views.add( this.renderItem( model ), { |
| 382 | at : this.collection.indexOf( model ) |
| 383 | } ); |
| 384 | }, this ); |
| 385 | |
| 386 | return this; |
| 387 | }, |
| 388 | |
| 389 | renderItem : function( model ) { |
| 390 | var view = new media.view.miFont.Icon({ |
| 391 | controller : this.controller, |
| 392 | model : model, |
| 393 | collection : this.collection, |
| 394 | selection : this.options.selection, |
| 395 | type : this.options.type, |
| 396 | data : this.options.data |
| 397 | }); |
| 398 | |
| 399 | return this._viewsByCid[ view.cid ] = view; |
| 400 | }, |
| 401 | |
| 402 | clearItems : function() { |
| 403 | _.each( this._viewsByCid, function( view ) { |
| 404 | delete this._viewsByCid[ view.cid ]; |
| 405 | view.remove(); |
| 406 | }, this ); |
| 407 | }, |
| 408 | |
| 409 | refresh : function() { |
| 410 | this.clearItems(); |
| 411 | this.render(); |
| 412 | }, |
| 413 | |
| 414 | ready : function() { |
| 415 | this.scrollToSelected(); |
| 416 | }, |
| 417 | |
| 418 | scrollToSelected : function() { |
| 419 | var single = this.options.selection.single(); |
| 420 | var singleView; |
| 421 | |
| 422 | if ( ! single ) { |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | singleView = this.getView( single ); |
| 427 | if ( singleView && ! this.isInView( singleView.$el ) ) { |
| 428 | this.$el.scrollTop( singleView.$el.offset().top - this.$el.offset().top + this.$el.scrollTop() - parseInt( this.$el.css('paddingTop') ) ); |
| 429 | } |
| 430 | }, |
| 431 | |
| 432 | getView : function( model ) { |
| 433 | return _.findWhere( this._viewsByCid, { model : model } ); |
| 434 | }, |
| 435 | |
| 436 | isInView: function( $elem ) { |
| 437 | var $window = $(window); |
| 438 | var docViewTop = $window.scrollTop(); |
| 439 | var docViewBottom = docViewTop + $window.height(); |
| 440 | var elemTop = $elem.offset().top; |
| 441 | var elemBottom = elemTop + $elem.height(); |
| 442 | |
| 443 | return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop)); |
| 444 | } |
| 445 | }); |
| 446 | |
| 447 | // View: Font icon: Dropdown filter |
| 448 | media.view.miFont.Filters = media.view.AttachmentFilters.extend({ |
| 449 | createFilters : function() { |
| 450 | this.filters = { |
| 451 | all : { |
| 452 | text : window.menuIcons.text.all, |
| 453 | props : { |
| 454 | group : 'all' |
| 455 | } |
| 456 | } |
| 457 | }; |
| 458 | |
| 459 | var groups = this.controller.state().get('data').groups; |
| 460 | _.each( groups, function( text, id ) { |
| 461 | this.filters[ id ] = { |
| 462 | text : text, |
| 463 | props : { |
| 464 | group : id |
| 465 | } |
| 466 | }; |
| 467 | }, this ); |
| 468 | }, |
| 469 | |
| 470 | change : function() { |
| 471 | var filter = this.filters[ this.el.value ]; |
| 472 | |
| 473 | if ( filter ) { |
| 474 | this.model.set( 'group', filter.props.group ); |
| 475 | } |
| 476 | } |
| 477 | }); |
| 478 | |
| 479 | // View: Font icon: Item |
| 480 | media.view.miFont.Icon = media.view.Attachment.extend({ |
| 481 | className : 'attachment mi-item', |
| 482 | events : { |
| 483 | 'click .attachment-preview' : 'toggleSelectionHandler', |
| 484 | 'click a' : 'preventDefault' |
| 485 | }, |
| 486 | |
| 487 | initialize : function() { |
| 488 | this.template = media.template( 'menu-icons-' + this.options.type + '-item' ); |
| 489 | media.view.Attachment.prototype.initialize.apply( this, arguments ); |
| 490 | }, |
| 491 | |
| 492 | render: function() { |
| 493 | this.$el.html( this.template( this.model.toJSON() ) ); |
| 494 | this.updateSelect(); |
| 495 | |
| 496 | return this; |
| 497 | } |
| 498 | }); |
| 499 | |
| 500 | |
| 501 | // Font icon state |
| 502 | media.controller.miFont = media.controller.State.extend({ |
| 503 | defaults : { |
| 504 | id : 'mi-font', |
| 505 | menu : 'default', |
| 506 | toolbar : 'mi-select', |
| 507 | type : '', |
| 508 | settings : [ 'hide_label', 'position', 'font_size', 'vertical_align' ] |
| 509 | }, |
| 510 | |
| 511 | initialize : function() { |
| 512 | var icons = this.get('data').items; |
| 513 | var library = this.get('library'); |
| 514 | var selection = this.get('selection'); |
| 515 | var fieldIds = this.get('settings'); |
| 516 | var fields; |
| 517 | |
| 518 | if ( ! ( library instanceof media.controller.miFont.Library ) ) { |
| 519 | library = new media.controller.miFont.Library( icons ); |
| 520 | library.props.on( 'change', this.miResetLibrary, this ); |
| 521 | |
| 522 | this.set( 'library', library ); |
| 523 | } |
| 524 | |
| 525 | if ( ! ( selection instanceof media.model.Selection ) ) { |
| 526 | this.set( 'selection', new media.model.Selection( selection, { |
| 527 | multiple : false |
| 528 | }) ); |
| 529 | } |
| 530 | |
| 531 | fields = _.filter( window.menuIcons.settingsFields, function( field ) { |
| 532 | return ( -1 !== $.inArray( field.id, fieldIds ) ); |
| 533 | }); |
| 534 | this.set( 'settings', fields ); |
| 535 | }, |
| 536 | |
| 537 | activate : function() { |
| 538 | this.frame.on( 'open', this.refresh, this ); |
| 539 | this.miUpdateSelection(); |
| 540 | }, |
| 541 | |
| 542 | deactivate : function() { |
| 543 | media.controller.State.prototype.deactivate.apply( this, arguments ); |
| 544 | this.frame.off( 'open', this.refresh, this ); |
| 545 | }, |
| 546 | |
| 547 | refresh : function() { |
| 548 | this.miResetFilter(); |
| 549 | this.miUpdateSelection(); |
| 550 | }, |
| 551 | |
| 552 | miGetContent : function() { |
| 553 | this.miResetFilter(); |
| 554 | |
| 555 | return new media.view.miFont({ |
| 556 | controller : this.frame, |
| 557 | model : this, |
| 558 | collection : this.get('library'), |
| 559 | selection : this.get('selection'), |
| 560 | type : this.get('type') |
| 561 | }); |
| 562 | }, |
| 563 | |
| 564 | miResetLibrary : function() { |
| 565 | var library = this.get('library'); |
| 566 | var group = library.props.get('group'); |
| 567 | var item = this.frame.miGetCurrentItem(); |
| 568 | |
| 569 | item.set( 'group', group ); |
| 570 | |
| 571 | library.reInitialize(); |
| 572 | this.set( 'library', library ); |
| 573 | |
| 574 | this.miUpdateSelection(); |
| 575 | }, |
| 576 | |
| 577 | miResetFilter : function() { |
| 578 | var library = this.get('library'); |
| 579 | var item = this.frame.miGetCurrentItem(); |
| 580 | var groups = this.get('data').groups; |
| 581 | var group = item.get('group'); |
| 582 | |
| 583 | if ( _.isUndefined( groups[ group ] ) ) { |
| 584 | group = 'all'; |
| 585 | } |
| 586 | |
| 587 | library.props.set( 'group', group ); |
| 588 | }, |
| 589 | |
| 590 | miUpdateSelection : function() { |
| 591 | var selection = this.get('selection'); |
| 592 | var type = this.get('type'); |
| 593 | var key = type+'-icon'; |
| 594 | var item = this.frame.miGetCurrentItem(); |
| 595 | var icon = item.get(key); |
| 596 | var selected; |
| 597 | |
| 598 | if ( type === item.get('type') && icon ) { |
| 599 | selected = this.get('library').findWhere({id: icon}); |
| 600 | } |
| 601 | |
| 602 | selection.reset( selected ? selected : [] ); |
| 603 | } |
| 604 | }); |
| 605 | |
| 606 | // Font icon collection |
| 607 | media.controller.miFont.Library = Backbone.Collection.extend({ |
| 608 | props : new Backbone.Model({ |
| 609 | group : 'all', |
| 610 | search : '' |
| 611 | }), |
| 612 | |
| 613 | initialize : function( models ) { |
| 614 | this.icons = new Backbone.Collection( models ); |
| 615 | }, |
| 616 | |
| 617 | reInitialize : function() { |
| 618 | var library = this; |
| 619 | var icons = this.icons.toJSON(); |
| 620 | var props = this.props.toJSON(); |
| 621 | |
| 622 | _.each( props, function( val, filter ) { |
| 623 | if ( library.filters[ filter ] ) { |
| 624 | icons = _.filter( icons, library.filters[ filter ], val ); |
| 625 | } |
| 626 | }, this); |
| 627 | |
| 628 | this.reset( icons ); |
| 629 | }, |
| 630 | |
| 631 | filters : { |
| 632 | group : function( icon ) { |
| 633 | var group = this; |
| 634 | |
| 635 | return ( 'all' === group || icon.group === group || '' === icon.group ); |
| 636 | }, |
| 637 | search : function( icon ) { |
| 638 | var term = this; |
| 639 | var result; |
| 640 | |
| 641 | if ( '' === term ) { |
| 642 | result = true; |
| 643 | } |
| 644 | else { |
| 645 | result = _.any(['id','label'], function( key ) { |
| 646 | var value = icon[key]; |
| 647 | |
| 648 | return value && -1 !== value.search( this ); |
| 649 | }, term ); |
| 650 | } |
| 651 | |
| 652 | return result; |
| 653 | } |
| 654 | } |
| 655 | }); |
| 656 | |
| 657 | |
| 658 | // Image icon state |
| 659 | media.controller.miImage = media.controller.Library.extend({ |
| 660 | defaults : _.defaults({ |
| 661 | id : 'browse', |
| 662 | menu : 'default', |
| 663 | router : 'browse', |
| 664 | toolbar : 'mi-select', |
| 665 | filterable : 'uploaded', |
| 666 | settings : [ 'hide_label', 'position', 'image_size', 'vertical_align' ], |
| 667 | syncSelection : false |
| 668 | }, media.controller.Library.prototype.defaults), |
| 669 | |
| 670 | initialize : function() { |
| 671 | var selection = this.get('selection'); |
| 672 | var fieldIds = this.get('settings'); |
| 673 | var fields; |
| 674 | |
| 675 | this.set( 'library', media.query({ type: 'image' }) ); |
| 676 | |
| 677 | this.routers = { |
| 678 | upload : { |
| 679 | text: media.view.l10n.uploadFilesTitle, |
| 680 | priority: 20 |
| 681 | }, |
| 682 | browse : { |
| 683 | text: media.view.l10n.mediaLibraryTitle, |
| 684 | priority: 40 |
| 685 | } |
| 686 | }; |
| 687 | |
| 688 | if ( ! ( selection instanceof media.model.Selection ) ) { |
| 689 | this.set( 'selection', new media.model.Selection( selection, { |
| 690 | multiple : false |
| 691 | }) ); |
| 692 | } |
| 693 | |
| 694 | fields = _.filter( window.menuIcons.settingsFields, function( field ) { |
| 695 | return ( -1 !== $.inArray( field.id, fieldIds ) ); |
| 696 | }); |
| 697 | this.set( 'settings', fields ); |
| 698 | |
| 699 | media.controller.Library.prototype.initialize.apply( this, arguments ); |
| 700 | }, |
| 701 | |
| 702 | activate : function() { |
| 703 | media.controller.Library.prototype.activate.apply( this, arguments ); |
| 704 | this.frame.on( 'open', this.miUpdateSelection, this ); |
| 705 | this.miUpdateSelection(); |
| 706 | }, |
| 707 | |
| 708 | deactivate : function() { |
| 709 | media.controller.Library.prototype.deactivate.apply( this, arguments ); |
| 710 | this.frame.off( 'open', this.miUpdateSelection, this ); |
| 711 | }, |
| 712 | |
| 713 | miUpdateSelection : function() { |
| 714 | var selection = this.get('selection'); |
| 715 | var type = this.get('type'); |
| 716 | var key = type+'-icon'; |
| 717 | var item = this.frame.miGetCurrentItem(); |
| 718 | var icon = item.get(key); |
| 719 | var attachment; |
| 720 | |
| 721 | if ( type === item.get('type') && icon ) { |
| 722 | attachment = Attachment.get( icon ); |
| 723 | this.dfd = attachment.fetch(); |
| 724 | } |
| 725 | |
| 726 | selection.reset( attachment ? attachment : [] ); |
| 727 | }, |
| 728 | |
| 729 | miGetContent : function( mode ) { |
| 730 | var content = ( 'upload' === mode ) ? this.uploadContent() : this.browseContent(); |
| 731 | |
| 732 | this.frame.$el.removeClass('hide-toolbar'); |
| 733 | |
| 734 | return content; |
| 735 | }, |
| 736 | |
| 737 | browseContent: function() { |
| 738 | var state = this; |
| 739 | |
| 740 | // Browse our library of attachments. |
| 741 | return new media.view.AttachmentsBrowser.miImage({ |
| 742 | type : state.get('type'), |
| 743 | controller : state.frame, |
| 744 | collection : state.get('library'), |
| 745 | selection : state.get('selection'), |
| 746 | model : state, |
| 747 | sortable : state.get('sortable'), |
| 748 | search : state.get('searchable'), |
| 749 | filters : state.get('filterable'), |
| 750 | display : state.get('displaySettings'), |
| 751 | dragInfo : state.get('dragInfo') |
| 752 | }); |
| 753 | }, |
| 754 | |
| 755 | /** |
| 756 | * Render callback for the content region in the `upload` mode. |
| 757 | */ |
| 758 | uploadContent: function() { |
| 759 | return new media.view.UploaderInline({ |
| 760 | controller: this.frame |
| 761 | }); |
| 762 | } |
| 763 | }); |
| 764 | |
| 765 | // View: Image Icon: Browser |
| 766 | media.view.AttachmentsBrowser.miImage = media.view.AttachmentsBrowser.extend({ |
| 767 | disposeSingle : function() { |
| 768 | media.view.AttachmentsBrowser.prototype.disposeSingle.apply( this, arguments ); |
| 769 | this.sidebar.unset('preview'); |
| 770 | this.sidebar.unset('settings'); |
| 771 | }, |
| 772 | |
| 773 | createPreview : function() { |
| 774 | var self = this; |
| 775 | var state = this.model; |
| 776 | var selected, controller, menuItem; |
| 777 | |
| 778 | if ( state.dfd && 'pending' === state.dfd.state() ) { |
| 779 | state.dfd.done( function() { |
| 780 | self.createPreview(); |
| 781 | } ); |
| 782 | |
| 783 | return; |
| 784 | } |
| 785 | |
| 786 | selected = state.get('selection').single(); |
| 787 | |
| 788 | // Disallow anything but image |
| 789 | if ( 'image' !== selected.get('type') ) { |
| 790 | state.get('selection').reset(); |
| 791 | |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | // Wait for the upload process to finish |
| 796 | if ( selected.get('uploading') ) { |
| 797 | selected.on( 'change:uploading', self.createPreview, this ); |
| 798 | |
| 799 | return; |
| 800 | } |
| 801 | |
| 802 | controller = this.controller; |
| 803 | menuItem = controller.miGetCurrentItem(); |
| 804 | |
| 805 | this.createSettings(); |
| 806 | this.sidebar.set( 'preview', new media.view.miPreview.miImage({ |
| 807 | controller : controller, |
| 808 | settings : this.sidebar.get('settings'), |
| 809 | model : menuItem, |
| 810 | data : { |
| 811 | type : state.get('type'), |
| 812 | alt : selected.get('alt'), |
| 813 | sizes : selected.get('sizes') |
| 814 | }, |
| 815 | priority : 80 |
| 816 | }) ); |
| 817 | } |
| 818 | }); |
| 819 | |
| 820 | _.extend( media.view.AttachmentsBrowser.miImage.prototype, media.view.miBrowser ); |
| 821 | |
| 822 | // View: Image Icon: Preview on the sidebar |
| 823 | media.view.miPreview.miImage = media.view.miPreview.extend({ |
| 824 | render : function() { |
| 825 | var size = this.options.model.get('image_size'); |
| 826 | var imageSizes = this.options.data.sizes; |
| 827 | var sizeField = this.options.settings.get('image_size'); |
| 828 | var newChoices = []; |
| 829 | |
| 830 | if ( ! imageSizes.hasOwnProperty( size ) ) { |
| 831 | size = 'full'; |
| 832 | } |
| 833 | |
| 834 | _.each( sizeField.model.get('choices'), function( choice ) { |
| 835 | if ( imageSizes.hasOwnProperty( choice.value ) ) { |
| 836 | newChoices.push( choice ); |
| 837 | } |
| 838 | } ); |
| 839 | |
| 840 | sizeField.model.set( 'choices', newChoices ); |
| 841 | this.options.model.set( 'image_size', size, { silent: true } ); |
| 842 | this.options.data.url = imageSizes[ size ].url; |
| 843 | |
| 844 | return media.view.miPreview.prototype.render.apply( this, arguments ); |
| 845 | } |
| 846 | }); |
| 847 | |
| 848 | // Frame |
| 849 | media.view.MediaFrame.menuIcons = media.view.MediaFrame.extend({ |
| 850 | initialize : function() { |
| 851 | media.view.MediaFrame.prototype.initialize.apply( this, arguments ); |
| 852 | |
| 853 | _.defaults( this.options, { |
| 854 | selection : [], |
| 855 | multiple : false, |
| 856 | editing : false, |
| 857 | toolbar : 'mi-select', |
| 858 | }); |
| 859 | |
| 860 | this.miMenuItems = new media.model.mi.MenuItems(); |
| 861 | this.createStates(); |
| 862 | this.bindHandlers(); |
| 863 | }, |
| 864 | |
| 865 | createStates : function() { |
| 866 | var options = this.options; |
| 867 | var Controller; |
| 868 | |
| 869 | if ( options.states ) { |
| 870 | return; |
| 871 | } |
| 872 | |
| 873 | _.each( window.menuIcons.iconTypes, function( props, type ) { |
| 874 | if ( ! media.controller.hasOwnProperty( props.data.controller ) ) { |
| 875 | delete window.menuIcons.iconTypes[ type ]; |
| 876 | return; |
| 877 | } |
| 878 | |
| 879 | Controller = media.controller[ props.data.controller ]; |
| 880 | |
| 881 | _.defaults( props, { |
| 882 | content : props.id, |
| 883 | selection : options.selection |
| 884 | } ); |
| 885 | |
| 886 | // States |
| 887 | this.states.add( new Controller( props ) ); |
| 888 | }, this ); |
| 889 | }, |
| 890 | |
| 891 | bindHandlers : function() { |
| 892 | this.on( 'router:create:browse', this.createRouter, this ); |
| 893 | this.on( 'router:render:browse', this.browseRouter, this ); |
| 894 | this.on( 'content:render', this.miRenderContent, this ); |
| 895 | this.on( 'toolbar:create:mi-select', this.createToolbar, this ); |
| 896 | this.on( 'toolbar:render:mi-select', this.miSelectToolbar, this ); |
| 897 | this.on( 'open', this.miInitialize, this ); |
| 898 | }, |
| 899 | |
| 900 | browseRouter : function( routerView ) { |
| 901 | var routers = this.state().routers; |
| 902 | |
| 903 | if ( routers ) { |
| 904 | routerView.set( routers ); |
| 905 | } |
| 906 | }, |
| 907 | |
| 908 | miRenderContent : function() { |
| 909 | var state = this.state(); |
| 910 | var mode = this.content.mode(); |
| 911 | var content = state.miGetContent( mode ); |
| 912 | |
| 913 | this.content.set( content ); |
| 914 | }, |
| 915 | |
| 916 | // Toolbars |
| 917 | miSelectToolbar : function( view ) { |
| 918 | var frame = this; |
| 919 | var state = frame.state(); |
| 920 | var type = state.get('type'); |
| 921 | |
| 922 | view.set( state.id, { |
| 923 | style : 'primary', |
| 924 | priority : 80, |
| 925 | text : window.menuIcons.text.select, |
| 926 | requires : { |
| 927 | selection : true |
| 928 | }, |
| 929 | click : function() { |
| 930 | frame.close(); |
| 931 | frame.miUpdateItemProps(); |
| 932 | frame.miUpdateItem(); |
| 933 | } |
| 934 | }); |
| 935 | }, |
| 936 | |
| 937 | // Content |
| 938 | miContentRender : function() { |
| 939 | var state = this.state(); |
| 940 | var content = state.miGetContent(); |
| 941 | |
| 942 | this.content.set( content ); |
| 943 | }, |
| 944 | |
| 945 | miGetState : function() { |
| 946 | var item = window.menuIcons.currentItem; |
| 947 | var type; |
| 948 | |
| 949 | if ( ! _.isUndefined( item.type ) && '' !== item.type && window.menuIcons.iconTypes.hasOwnProperty( item.type ) ) { |
| 950 | type = item.type; |
| 951 | } |
| 952 | else { |
| 953 | type = window.menuIcons.typeNames[0]; |
| 954 | } |
| 955 | |
| 956 | return 'mi-'+type; |
| 957 | }, |
| 958 | |
| 959 | miGetCurrentItem : function() { |
| 960 | return this.miMenuItems.get( window.menuIcons.currentItem.id ); |
| 961 | }, |
| 962 | |
| 963 | miUpdateMenuItems : function() { |
| 964 | var item = this.miGetCurrentItem(); |
| 965 | |
| 966 | if ( _.isUndefined( item ) ) { |
| 967 | this.miMenuItems.add( window.menuIcons.currentItem ); |
| 968 | } |
| 969 | else { |
| 970 | item.set( window.menuIcons.currentItem ); |
| 971 | } |
| 972 | |
| 973 | this.miMenuItems.props.set( 'item', window.menuIcons.currentItem.id ); |
| 974 | }, |
| 975 | |
| 976 | miInitialize : function() { |
| 977 | this.miUpdateMenuItems(); |
| 978 | this.setState( this.miGetState() ); |
| 979 | }, |
| 980 | |
| 981 | miUpdateItemProps : function() { |
| 982 | var state = this.state(); |
| 983 | var type = state.get('type'); |
| 984 | var selection = state.get('selection'); |
| 985 | var single = selection.single(); |
| 986 | var icon = single ? single.id : ''; |
| 987 | var item = this.miGetCurrentItem(); |
| 988 | |
| 989 | item.set( 'type', type ); |
| 990 | item.set( type+'-icon', icon ); |
| 991 | item.set( 'icon', icon ); |
| 992 | }, |
| 993 | |
| 994 | miUpdateItem : function() { |
| 995 | var attrs = this.miGetCurrentItem().toJSON(); |
| 996 | var id = attrs.id; |
| 997 | var state = this.state(); |
| 998 | var selected = state.get('selection').single(); |
| 999 | var template = media.template( 'menu-icons-'+ attrs.type +'-field' ); |
| 1000 | var data = selected.toJSON(); |
| 1001 | var $el; |
| 1002 | |
| 1003 | data._settings = attrs; |
| 1004 | delete attrs.id; |
| 1005 | delete attrs.title; |
| 1006 | |
| 1007 | _.each( attrs, function( value, key ) { |
| 1008 | $el = $('#menu-icons-'+ id +'-'+ key).not('._setting'); |
| 1009 | if ( $el.length ) { |
| 1010 | $el.val( value ).trigger('mi:update'); |
| 1011 | } |
| 1012 | }); |
| 1013 | |
| 1014 | $('#menu-icons-'+ id +'-select').html( template( data ) ); |
| 1015 | } |
| 1016 | }); |
| 1017 | |
| 1018 | |
| 1019 | $('body') |
| 1020 | .on( 'click', 'div.menu-icons-wrap a._select', window.menuIcons.selectIcon ) |
| 1021 | .on( 'click', 'div.menu-icons-wrap a._remove', window.menuIcons.removeIcon ) |
| 1022 | .on( 'mi:update', 'div.menu-icons-wrap select._type', window.menuIcons.toggleSelect ); |
| 1023 | |
| 1024 | $('div.menu-icons-wrap select._type').trigger('mi:update'); |
| 1025 | |
| 1026 | |
| 1027 | // Settings meta box |
| 1028 | $('#menu-item-settings-save').on('click', function(e) { |
| 1029 | var $button = $(this).prop( 'disabled', true ); |
| 1030 | var $spinner = $button.siblings('span.spinner'); |
| 1031 | |
| 1032 | e.preventDefault(); |
| 1033 | |
| 1034 | $spinner.css( 'display', 'inline-block' ); |
| 1035 | |
| 1036 | $.ajax({ |
| 1037 | type : 'POST', |
| 1038 | url : window.menuIcons.ajaxUrls.update, |
| 1039 | data : $('#menu-icons-settings :input').serialize(), |
| 1040 | success : function( response, xhr ) { |
| 1041 | if ( true === response.success && response.data.redirectUrl ) { |
| 1042 | window.location = response.data.redirectUrl; |
| 1043 | } |
| 1044 | else { |
| 1045 | $button.prop( 'disabled', false ); |
| 1046 | } |
| 1047 | }, |
| 1048 | always : function() { |
| 1049 | $spinner.hide(); |
| 1050 | } |
| 1051 | }); |
| 1052 | }); |
| 1053 | |
| 1054 | |
| 1055 | }(jQuery)); |
| 1056 |