Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4826 from jaswilli/issue-4807
Implement Navigation item input behaviors
- Loading branch information
Showing
10 changed files
with
168 additions
and
503 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
function joinUrlParts(url, path) { | ||
if (path[0] !== '/' && url.slice(-1) !== '/') { | ||
path = '/' + path; | ||
} else if (path[0] === '/' && url.slice(-1) === '/') { | ||
path = path.slice(1); | ||
} | ||
|
||
return url + path; | ||
} | ||
|
||
var NavItemUrlInputComponent = Ember.TextField.extend({ | ||
classNameBindings: ['fakePlaceholder'], | ||
|
||
isBaseUrl: Ember.computed('baseUrl', 'value', function () { | ||
return this.get('baseUrl') === this.get('value'); | ||
}), | ||
|
||
fakePlaceholder: Ember.computed('isBaseUrl', 'hasFocus', function () { | ||
return this.get('isBaseUrl') && !this.get('hasFocus'); | ||
}), | ||
|
||
isRelative: Ember.computed('value', function () { | ||
return !validator.isURL(this.get('value')); | ||
}), | ||
|
||
didInsertElement: 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 | ||
if (this.get('isRelative')) { | ||
url = joinUrlParts(baseUrl, url); | ||
} | ||
|
||
this.set('value', url); | ||
this.sendAction('change', this.get('value')); | ||
}, | ||
|
||
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(); | ||
} | ||
}, | ||
|
||
focusOut: function () { | ||
this.set('hasFocus', false); | ||
this.set('value', this.get('value').trim()); | ||
|
||
var url = this.get('value'), | ||
baseUrl = this.get('baseUrl'); | ||
|
||
if (this.get('isRelative')) { | ||
this.set('value', joinUrlParts(baseUrl, url)); | ||
} | ||
|
||
this.sendAction('change', url); | ||
} | ||
}); | ||
|
||
export default NavItemUrlInputComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var NavItemComponent = Ember.Component.extend({ | ||
classNames: 'navigation-item', | ||
|
||
keyPress: function (event) { | ||
// enter key | ||
if (event.keyCode === 13) { | ||
event.preventDefault(); | ||
this.get('controller').send('addItem'); | ||
} | ||
}, | ||
|
||
actions: { | ||
addItem: function () { | ||
this.sendAction('addItem'); | ||
}, | ||
|
||
deleteItem: function (item) { | ||
this.sendAction('deleteItem', item); | ||
}, | ||
|
||
updateUrl: function (value) { | ||
this.sendAction('updateUrl', value, this.get('navItem')); | ||
} | ||
} | ||
}); | ||
|
||
export default NavItemComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.