-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
163 lines (131 loc) · 5.74 KB
/
app.js
File metadata and controls
163 lines (131 loc) · 5.74 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
define(['jquery', 'ractive', 'rv!templates/template', 'text!css/widget-styles.css', 'app/helpers'], function ($, Ractive, mainTemplate, css, helpers) {
'use strict';
$.noConflict();
var xhr_suggestions = null;
var timeout_suggestions = null;
var search_widget = {
init: function (z) {
var $style = $("<style></style>", {type: "text/css"});
$style.text(css);
$("head").append($style);
Ractive.DEBUG = /unminified/.test(function(){/*unminified*/});
$('.openstack-search-bar').each(function() {
var el = $(this);
var baseUrl = el.data('baseurl') ? el.data('baseurl') : 'search.openstack.org';
var context = el.data('context') ? el.data('context') : 'www-openstack';
// render our main view
this.ractive = new Ractive({
el: el,
baseUrl: baseUrl,
context: context,
template: mainTemplate,
data: {
term: '',
page: 1,
perPage: 10,
pagesToShow: [1,2,3,4,5],
fromResult: 1,
toResult: 10,
total: 88,
results: [],
suggestions: []
}
});
this.ractive.on({
clear: function(ev) {
ev.original.preventDefault();
$('.ossw-search-suggestions-wrapper', el).hide();
this.set('term', '');
},
search: function(ev) {
var that = this;
ev.original.preventDefault();
if(ev.original.keyCode == 13) {
if ( xhr_suggestions ) xhr_suggestions.abort();
if ( timeout_suggestions ) clearTimeout(timeout_suggestions);
doSearch(that, true);
centerPopup(el);
} else {
$('.ossw-search-suggestions-wrapper', el).show();
if ( timeout_suggestions ) clearTimeout(timeout_suggestions);
timeout_suggestions = window.setTimeout(doSuggestions, 500, that);
}
},
searchPopup: function(ev) {
var that = this;
ev.original.preventDefault();
if(ev.original.keyCode == 13) {
doSearch(that, true);
$('.ossw-suggestions-wrapper', el).hide();
} else {
$('.ossw-suggestions-wrapper', el).show();
if ( timeout_suggestions ) clearTimeout(timeout_suggestions);
timeout_suggestions = window.setTimeout(doSuggestions, 500, that);
}
},
closePopup: function(ev) {
$('.ossw-search-results', el).hide();
},
changePage: function(ev, newPage) {
var total = this.get('total');
var perPage = this.get('perPage');
var totalPages = Math.ceil(total / perPage);
var that = this;
ev.original.preventDefault();
if (newPage > totalPages || newPage < 1) return false;
helpers.changePagination(that, newPage);
doSearch(that, false);
}
});
});
}
};
function doSearch(that, reset) {
var term = that.get('term');
var page = that.get('page');
var perPage = that.get('perPage');
var url = 'https://'+that.baseUrl+'/api/public/v1/search/'+that.context;
$.ajax({
url: url + '/'+term+'?page='+page+'&page_size='+perPage,
dataType: "json"
}).done(function(resp) {
var results = helpers.mapSearchResults(resp.results);
that.set('total', resp.qty);
that.set('results', results);
if(reset) helpers.resetPagination(that);
}).fail(function(resp) {
// error response
});
}
function doSuggestions(that) {
if ( xhr_suggestions ) xhr_suggestions.abort();
var term = that.get('term');
xhr_suggestions = $.ajax({
url: 'https://'+that.baseUrl+'/api/public/v1/suggestions/'+that.context+'/'+term,
dataType: "json"
}).done(function(resp) {
var results = helpers.mapSuggestions(resp.results);
xhr_suggestions = null;
that.set('suggestions', results);
}).fail(function(resp) {
// error response
});
}
function centerPopup(el) {
var winW = $(window).width();
var newPopWidth = winW * 0.8;
var popMaxWidth = parseInt($('.ossw-container', el).css('max-width').slice(0, -2));
$('.ossw-search-results', el).show();
$('.ossw-suggestions-wrapper', el).hide();
$('.ossw-search-suggestions-wrapper', el).hide();
if (newPopWidth < popMaxWidth) {
$('.ossw-container', el).css('left', winW*0.1);
$('.ossw-container', el).css('width', winW*0.8);
} else {
var halfWin = winW / 2;
var halfPop = popMaxWidth / 2;
$('.ossw-container', el).css('left', (halfWin - halfPop) );
}
}
return search_widget;
});