-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathurl.js
More file actions
105 lines (88 loc) · 2.26 KB
/
url.js
File metadata and controls
105 lines (88 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var View = wp.media.View,
$ = jQuery,
l10n = wp.media.view.l10n,
EmbedUrl;
/**
* wp.media.view.EmbedUrl
*
* @memberOf wp.media.view
*
* @class
* @augments wp.media.View
* @augments wp.Backbone.View
* @augments Backbone.View
*/
EmbedUrl = View.extend(/** @lends wp.media.view.EmbedUrl.prototype */{
tagName: 'span',
className: 'embed-url',
events: {
'input': 'url'
},
initialize: function() {
this.$input = $( '<input id="embed-url-field" type="url" />' )
.attr( 'aria-label', l10n.insertFromUrlTitle )
.val( this.model.get('url') );
this.input = this.$input[0];
this.spinner = $('<span class="spinner" />')[0];
this.error = $('<div class="notice notice-error embed-url-error"><p></p></div>')[0];
this.$error = $(this.error);
this.$error.hide();
this.$el.append([ this.input, this.spinner, this.error ]);
this.listenTo( this.model, 'change:url', this.render );
if ( this.model.get( 'url' ) ) {
_.delay( _.bind( function () {
this.model.trigger( 'change:url' );
}, this ), 500 );
}
this.updateUrl = _.debounce( this.updateUrl, 500 );
},
/**
* @return {wp.media.view.EmbedUrl} Returns itself to allow chaining.
*/
render: function() {
var $input = this.$input;
if ( $input.is(':focus') ) {
return;
}
if ( this.model.get( 'url' ) ) {
this.input.value = this.model.get('url');
} else {
this.input.setAttribute( 'placeholder', 'https://' );
}
/**
* Call `render` directly on parent class with passed arguments
*/
View.prototype.render.apply( this, arguments );
return this;
},
url: function( event ) {
var $el = $( event.target );
var url = $el.val() || '';
var valid = this.isValidUrlInput( event.target );
this.updateUrl( url, valid );
},
isValidUrlInput: function ( el ) {
var url = ( el.value || '' ).trim();
try {
url = new URL( url );
return [ 'http:', 'https:' ].includes(url.protocol);
} catch ( e ) {
return false;
}
},
updateUrl: function ( url, valid ) {
if ( valid ) {
this.model.set( 'url', url.trim() );
this.$error.hide();
} else {
if ( url.length > 0 ) {
this.model.set( 'url', '' );
this.$error.find( 'p' ).text( l10n.invalidUrl );
this.$error.show();
} else {
this.$error.hide();
}
}
}
});
module.exports = EmbedUrl;