Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 8dcbff9

Browse files
committed
Merge pull request #1649 from mariodian/refactor_suggestions
Refactor autocomplete suggestions
2 parents fdc3880 + cd8fbbf commit 8dcbff9

File tree

4 files changed

+279
-176
lines changed

4 files changed

+279
-176
lines changed

css/obBase.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1801,10 +1801,11 @@ removed because I don't think we're using the trick to hide the borders any more
18011801
width: 100%;
18021802
max-height: 155px;
18031803
background: #fff;
1804-
margin-top: 14px;
1804+
margin-top: 12px;
18051805
border-bottom-left-radius: 3px;
18061806
border-bottom-right-radius: 3px;
18071807
overflow-y: scroll;
1808+
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.10);
18081809
}
18091810
18101811
.suggestionsList a {

js/templates/pageNav.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ <h2 class="ion-android-close custCol-text clickable"></h2>
155155
</div>
156156
<% } %>
157157

158-
<div class="suggestionsList js-addressBarSuggestions"></div>
159158
</div>
160159
</div>
161160
<div class="ctrlGroup">

js/views/pageNavVw.js

Lines changed: 13 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var __ = require('underscore'),
1111
//adminPanelView = require('../views/adminPanelVw'),
1212
NotificationsVw = require('../views/notificationsVw'),
1313
PageNavServersVw = require('../views/pageNavServersVw'),
14+
SuggestionsVw = require('../views/suggestionsVw'),
1415
pjson = require('../../package.json');
1516

1617
var ipcRenderer = require('ipc-renderer'); // Allows to talk Electon main process
@@ -33,7 +34,6 @@ module.exports = baseVw.extend({
3334
'focus .js-navAddressBar': 'addressBarFocus',
3435
'keyup .js-navAddressBar': 'addressBarKeyup',
3536
'blur .js-navAddressBar': 'addressBarBlur',
36-
'click .js-suggestion': 'suggestionClick',
3737
'click .js-closeStatus': 'closeStatusBar',
3838
'blur input': 'validateInput',
3939
'blur textarea': 'validateInput',
@@ -387,7 +387,13 @@ module.exports = baseVw.extend({
387387
self.$statusBar = self.$el.find('.js-navStatusBar');
388388
self.$serverSubmenu = self.$('.js-serverSubmenu');
389389
self.$serverSubmenuTrigger = self.$('.js-serverSubmenuTrigger');
390-
self.$suggestionsList = self.$('.js-addressBarSuggestions');
390+
391+
self.suggestionsVw && self.suggestionsVw.remove();
392+
self.suggestionsVw = new SuggestionsVw({
393+
$input: self.$addressInput
394+
});
395+
396+
self.$('.js-mainSearchWrapper').append(self.suggestionsVw.render().el);
391397

392398
//listen for address bar set events
393399
self.listenTo(window.obEventBus, 'setAddressBar', function(options){
@@ -553,7 +559,7 @@ module.exports = baseVw.extend({
553559

554560
if ($popMenu.hasClass('popMenu-opened')) {
555561
app.showOverlay();
556-
this.hideSuggestions();
562+
this.suggestionsVw.hideSuggestions();
557563
openHandler = $popMenu.data('onopen');
558564
openHandler && this[openHandler] && this[openHandler].call(this);
559565
} else {
@@ -568,9 +574,9 @@ module.exports = baseVw.extend({
568574
$popMenu,
569575
closeHandler;
570576

571-
if (!$target.hasClass('js-navAddressBar') && this.suggestionsVisible) {
577+
if (!$target.hasClass('js-navAddressBar') && this.suggestionsVw.isVisible()) {
572578
app.hideOverlay();
573-
this.hideSuggestions();
579+
this.suggestionsVw.hideSuggestions();
574580
} else if ($target.hasClass('js-navAddressBar') ||
575581
!(
576582
$target.hasClass('popMenu') ||
@@ -579,7 +585,7 @@ module.exports = baseVw.extend({
579585
$target.parents('[data-popmenu]').length
580586
))
581587
{
582-
if (!this.suggestionsVisible) {
588+
if (!this.suggestionsVw.isVisible()) {
583589
app.hideOverlay();
584590
}
585591

@@ -648,11 +654,6 @@ module.exports = baseVw.extend({
648654
$(e.target).one('mouseup', function () {
649655
$('#addressBar').select();
650656
});
651-
652-
// Trigger suggestions box
653-
if (!this.suggestionsVisible) {
654-
this.$addressInput.trigger('keyup');
655-
}
656657
},
657658

658659
addressBarBlur: function(){
@@ -666,7 +667,7 @@ module.exports = baseVw.extend({
666667
//detect enter key
667668
if (e.keyCode == 13){
668669
// Stop right here! Route was already changed
669-
if (this.selectSuggestionOnEnter()) {
670+
if (this.suggestionsVw.wasSelectedOnEnter()) {
670671
return;
671672
}
672673

@@ -677,7 +678,6 @@ module.exports = baseVw.extend({
677678
this.addressBarProcess(barText);
678679
}
679680
} else {
680-
this.showSuggestionsBox(e, barText);
681681
this.closeStatusBar();
682682
}
683683
},
@@ -687,167 +687,6 @@ module.exports = baseVw.extend({
687687
Backbone.history.navigate(route, {trigger: true});
688688
});
689689
},
690-
691-
suggestionClick: function() {
692-
this.hideSuggestions();
693-
},
694-
695-
showSuggestionsBox: function(e, query) {
696-
var self = this,
697-
keyCode = e.keyCode || e.which,
698-
type = null,
699-
list = [],
700-
suggestions = [];
701-
702-
query = query.replace('ob://', '');
703-
704-
if (query.startsWith('@')) {
705-
type = 'handles';
706-
list = this.findInHandlesList(query);
707-
} else if (query.startsWith('#') || !query.startsWith('ob://')) {
708-
type = 'tags';
709-
list = this.findInTagsList(query);
710-
}
711-
712-
if (list.length) {
713-
// On query text change
714-
if (query !== '' && this.lastQuery !== query) {
715-
__.each(list, function(item){
716-
var itemUrl = '',
717-
itemTitle = '';
718-
719-
if (type == 'tags') {
720-
itemTitle = item.startsWith('#') ? item : '#' + item;
721-
itemUrl = '#home/products/' + (item.startsWith('#') ? item.substr(1, item.length) : item);
722-
} else if (type == 'handles') {
723-
itemTitle = item.handle + ' &ndash; ' + item.name;
724-
itemUrl = '#userPage/' + item.guid + '/store';
725-
}
726-
727-
loadTemplate('./js/templates/pageNavSuggestionItem.html', function(loadedTemplate) {
728-
suggestions.push(loadedTemplate({
729-
itemTitle: itemTitle,
730-
itemUrl: itemUrl,
731-
}));
732-
});
733-
});
734-
735-
self.$suggestionsList.html(suggestions);
736-
app.showOverlay();
737-
self.showSuggestions();
738-
739-
self.lastQuery = query;
740-
}
741-
742-
// Up and down keys
743-
if (keyCode == 38 || keyCode == 40) {
744-
this.$selectedSuggestion = this.$suggestionsList.find('a.selected');
745-
this.$newSuggestion = null;
746-
747-
if (keyCode == 40) {
748-
this.suggestionMoveDown();
749-
}
750-
else {
751-
this.suggestionMoveUp();
752-
}
753-
}
754-
755-
} else {
756-
if (this.lastQuery !== query) {
757-
app.hideOverlay();
758-
this.hideSuggestions();
759-
}
760-
}
761-
},
762-
763-
showSuggestions: function() {
764-
this.$suggestionsList.show();
765-
766-
this.suggestionsVisible = true;
767-
},
768-
769-
hideSuggestions: function() {
770-
this.$suggestionsList.hide();
771-
772-
this.suggestionsVisible = false;
773-
this.lastQuery = '';
774-
},
775-
776-
selectSuggestionOnEnter: function() {
777-
var $selected = this.$suggestionsList.find('a.selected');
778-
779-
app.hideOverlay();
780-
this.hideSuggestions();
781-
782-
if (typeof $selected !== 'undefined' && $selected.length) {
783-
Backbone.history.navigate($selected.attr('href'), {
784-
trigger: true
785-
});
786-
787-
return true;
788-
}
789-
},
790-
791-
selectNewSuggestion: function() {
792-
if (this.$newSuggestion !== null) {
793-
this.$selectedSuggestion.removeClass('selected');
794-
this.$newSuggestion.addClass('selected');
795-
796-
var itemPos = this.$newSuggestion.position().top;
797-
798-
// Show current item if not visible
799-
if (this.$suggestionsList.outerHeight() <= itemPos || itemPos < 0) {
800-
this.$suggestionsList.scrollTop(itemPos);
801-
}
802-
}
803-
},
804-
805-
suggestionMoveUp: function() {
806-
if (this.$selectedSuggestion.length) {
807-
this.$newSuggestion = this.$selectedSuggestion.prev();
808-
809-
// Return back to address bar
810-
if (!this.$newSuggestion.length) {
811-
this.$selectedSuggestion.removeClass('selected');
812-
this.$addressInput.focus();
813-
814-
this.$newSuggestion = null;
815-
}
816-
}
817-
818-
this.selectNewSuggestion();
819-
},
820-
821-
suggestionMoveDown: function() {
822-
if (this.$selectedSuggestion.length) {
823-
this.$newSuggestion = this.$selectedSuggestion.next();
824-
825-
// Last item
826-
if (!this.$newSuggestion.length) {
827-
this.$newSuggestion = this.$selectedSuggestion;
828-
}
829-
} else {
830-
this.$newSuggestion = this.$suggestionsList.find('a').first();
831-
}
832-
833-
this.selectNewSuggestion();
834-
},
835-
836-
findInTagsList: function(query) {
837-
var re = new RegExp(query, 'i');
838-
839-
return __.filter(JSON.parse(localStorage.getItem('tagsHistory')), function(item){
840-
return query && re.exec(item) !== null;
841-
});
842-
},
843-
844-
findInHandlesList: function(query) {
845-
var re = new RegExp(query, 'i');
846-
847-
return __.filter(JSON.parse(localStorage.getItem('handlesHistory')), function(item){
848-
return query && re.exec(item.handle) !== null;
849-
});
850-
},
851690

852691
showStatusBar: function(msgText){
853692
this.$statusBar.find('.js-statusBarMessage').text(msgText);

0 commit comments

Comments
 (0)