| 1 | var View = wp.media.View, |
|---|
| 2 | $ = jQuery, |
|---|
| 3 | l10n = wp.media.view.l10n, |
|---|
| 4 | EmbedUrl; |
|---|
| 5 | |
|---|
| 6 | /** |
|---|
| 7 | * wp.media.view.EmbedUrl |
|---|
| 8 | * |
|---|
| 9 | * @memberOf wp.media.view |
|---|
| 10 | * |
|---|
| 11 | * @class |
|---|
| 12 | * @augments wp.media.View |
|---|
| 13 | * @augments wp.Backbone.View |
|---|
| 14 | * @augments Backbone.View |
|---|
| 15 | */ |
|---|
| 16 | EmbedUrl = View.extend(/** @lends wp.media.view.EmbedUrl.prototype */{ |
|---|
| 17 | tagName: 'span', |
|---|
| 18 | className: 'embed-url', |
|---|
| 19 | |
|---|
| 20 | events: { |
|---|
| 21 | 'input': 'url' |
|---|
| 22 | }, |
|---|
| 23 | |
|---|
| 24 | initialize: function() { |
|---|
| 25 | this.$input = $( '<input id="embed-url-field" type="url" />' ) |
|---|
| 26 | .attr( 'aria-label', l10n.insertFromUrlTitle ) |
|---|
| 27 | .val( this.model.get('url') ); |
|---|
| 28 | this.input = this.$input[0]; |
|---|
| 29 | |
|---|
| 30 | this.spinner = $('<span class="spinner" />')[0]; |
|---|
| 31 | this.$el.append([ this.input, this.spinner ]); |
|---|
| 32 | |
|---|
| 33 | this.listenTo( this.model, 'change:url', this.render ); |
|---|
| 34 | |
|---|
| 35 | if ( this.model.get( 'url' ) ) { |
|---|
| 36 | _.delay( _.bind( function () { |
|---|
| 37 | this.model.trigger( 'change:url' ); |
|---|
| 38 | }, this ), 500 ); |
|---|
| 39 | } |
|---|
| 40 | }, |
|---|
| 41 | /** |
|---|
| 42 | * @return {wp.media.view.EmbedUrl} Returns itself to allow chaining. |
|---|
| 43 | */ |
|---|
| 44 | render: function() { |
|---|
| 45 | var $input = this.$input; |
|---|
| 46 | |
|---|
| 47 | if ( $input.is(':focus') ) { |
|---|
| 48 | return; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | if ( this.model.get( 'url' ) ) { |
|---|
| 52 | this.input.value = this.model.get('url'); |
|---|
| 53 | } else { |
|---|
| 54 | this.input.setAttribute( 'placeholder', 'https://' ); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Call `render` directly on parent class with passed arguments |
|---|
| 59 | */ |
|---|
| 60 | View.prototype.render.apply( this, arguments ); |
|---|
| 61 | return this; |
|---|
| 62 | }, |
|---|
| 63 | |
|---|
| 64 | url: function( event ) { |
|---|
| 65 | var url = event.target.value || ''; |
|---|
| 66 | this.model.set( 'url', url.trim() ); |
|---|
| 67 | } |
|---|
| 68 | }); |
|---|
| 69 | |
|---|
| 70 | module.exports = EmbedUrl; |
|---|