2015-02-13 07:22:32 +03:00
|
|
|
import Ember from 'ember';
|
2015-06-13 17:34:09 +03:00
|
|
|
|
2015-09-16 20:02:06 +03:00
|
|
|
var joinUrlParts,
|
|
|
|
isRelative;
|
|
|
|
|
|
|
|
joinUrlParts = function (url, path) {
|
2015-01-18 03:16:54 +03:00
|
|
|
if (path[0] !== '/' && url.slice(-1) !== '/') {
|
|
|
|
path = '/' + path;
|
|
|
|
} else if (path[0] === '/' && url.slice(-1) === '/') {
|
|
|
|
path = path.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return url + path;
|
2015-09-16 20:02:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
isRelative = function (url) {
|
|
|
|
// "protocol://", "//example.com", "scheme:", "#anchor", & invalid paths
|
|
|
|
// should all be treated as absolute
|
|
|
|
return !url.match(/\s/) && !validator.isURL(url) && !url.match(/^(\/\/|#|[a-zA-Z0-9\-]+:)/);
|
|
|
|
};
|
2015-01-18 03:16:54 +03:00
|
|
|
|
2015-06-13 17:34:09 +03:00
|
|
|
export default Ember.TextField.extend({
|
2015-09-16 20:02:06 +03:00
|
|
|
classNames: 'gh-input',
|
2015-01-18 03:16:54 +03:00
|
|
|
classNameBindings: ['fakePlaceholder'],
|
|
|
|
|
2015-06-13 17:34:09 +03:00
|
|
|
didReceiveAttrs: function () {
|
|
|
|
var url = this.get('url'),
|
|
|
|
baseUrl = this.get('baseUrl');
|
|
|
|
|
|
|
|
// if we have a relative url, create the absolute url to be displayed in the input
|
2015-09-16 20:02:06 +03:00
|
|
|
if (isRelative(url)) {
|
2015-06-13 17:34:09 +03:00
|
|
|
url = joinUrlParts(baseUrl, url);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('value', url);
|
|
|
|
},
|
|
|
|
|
2015-01-18 03:16:54 +03:00
|
|
|
isBaseUrl: Ember.computed('baseUrl', 'value', function () {
|
|
|
|
return this.get('baseUrl') === this.get('value');
|
|
|
|
}),
|
|
|
|
|
|
|
|
fakePlaceholder: Ember.computed('isBaseUrl', 'hasFocus', function () {
|
2015-02-14 02:27:22 +03:00
|
|
|
return this.get('isBaseUrl') && this.get('last') && !this.get('hasFocus');
|
2015-01-18 03:16:54 +03:00
|
|
|
}),
|
|
|
|
|
|
|
|
focusIn: function (event) {
|
|
|
|
this.set('hasFocus', true);
|
|
|
|
|
|
|
|
if (this.get('isBaseUrl')) {
|
|
|
|
// position the cursor at the end of the input
|
|
|
|
Ember.run.next(function (el) {
|
|
|
|
var length = el.value.length;
|
|
|
|
|
|
|
|
el.setSelectionRange(length, length);
|
|
|
|
}, event.target);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
keyDown: function (event) {
|
|
|
|
// delete the "placeholder" value all at once
|
|
|
|
if (this.get('isBaseUrl') && (event.keyCode === 8 || event.keyCode === 46)) {
|
|
|
|
this.set('value', '');
|
|
|
|
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2015-09-16 20:02:06 +03:00
|
|
|
|
|
|
|
// CMD-S
|
|
|
|
if (event.keyCode === 83 && event.metaKey) {
|
|
|
|
this.notifyUrlChanged();
|
|
|
|
}
|
2015-01-18 03:16:54 +03:00
|
|
|
},
|
|
|
|
|
2015-01-28 02:14:45 +03:00
|
|
|
keyPress: function (event) {
|
|
|
|
// enter key
|
|
|
|
if (event.keyCode === 13) {
|
|
|
|
event.preventDefault();
|
|
|
|
this.notifyUrlChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2015-01-18 03:16:54 +03:00
|
|
|
focusOut: function () {
|
|
|
|
this.set('hasFocus', false);
|
2015-01-28 02:14:45 +03:00
|
|
|
|
|
|
|
this.notifyUrlChanged();
|
|
|
|
},
|
|
|
|
|
|
|
|
notifyUrlChanged: function () {
|
2015-01-18 03:16:54 +03:00
|
|
|
this.set('value', this.get('value').trim());
|
|
|
|
|
|
|
|
var url = this.get('value'),
|
2015-09-16 20:02:06 +03:00
|
|
|
urlParts = document.createElement('a'),
|
|
|
|
baseUrl = this.get('baseUrl'),
|
|
|
|
baseUrlParts = document.createElement('a');
|
|
|
|
|
|
|
|
// leverage the browser's native URI parsing
|
|
|
|
urlParts.href = url;
|
|
|
|
baseUrlParts.href = baseUrl;
|
|
|
|
|
|
|
|
// if we have an email address, add the mailto:
|
|
|
|
if (validator.isEmail(url)) {
|
|
|
|
url = `mailto:${url}`;
|
|
|
|
this.set('value', url);
|
|
|
|
}
|
2015-01-18 03:16:54 +03:00
|
|
|
|
2015-03-08 21:27:00 +03:00
|
|
|
// if we have a relative url, create the absolute url to be displayed in the input
|
2015-09-16 20:02:06 +03:00
|
|
|
if (isRelative(url)) {
|
|
|
|
url = joinUrlParts(baseUrl, url);
|
|
|
|
this.set('value', url);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove the base url before sending to action
|
|
|
|
if (urlParts.host === baseUrlParts.host && !url.match(/^#/)) {
|
|
|
|
url = url.replace(/^[a-zA-Z0-9\-]+:/, '');
|
|
|
|
url = url.replace(/^\/\//, '');
|
|
|
|
url = url.replace(baseUrlParts.host, '');
|
|
|
|
url = url.replace(baseUrlParts.pathname, '');
|
|
|
|
if (!url.match(/^\//)) {
|
|
|
|
url = '/' + url;
|
|
|
|
}
|
2015-01-18 03:16:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
this.sendAction('change', url);
|
|
|
|
}
|
|
|
|
});
|