reqless 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +8 -0
- data/README.md +648 -0
- data/Rakefile +117 -0
- data/bin/docker-build-and-test +22 -0
- data/exe/reqless-web +11 -0
- data/lib/reqless/config.rb +31 -0
- data/lib/reqless/failure_formatter.rb +43 -0
- data/lib/reqless/job.rb +496 -0
- data/lib/reqless/job_reservers/ordered.rb +29 -0
- data/lib/reqless/job_reservers/round_robin.rb +46 -0
- data/lib/reqless/job_reservers/shuffled_round_robin.rb +21 -0
- data/lib/reqless/lua/reqless-lib.lua +2965 -0
- data/lib/reqless/lua/reqless.lua +2545 -0
- data/lib/reqless/lua_script.rb +90 -0
- data/lib/reqless/middleware/requeue_exceptions.rb +94 -0
- data/lib/reqless/middleware/retry_exceptions.rb +72 -0
- data/lib/reqless/middleware/sentry.rb +66 -0
- data/lib/reqless/middleware/timeout.rb +63 -0
- data/lib/reqless/queue.rb +189 -0
- data/lib/reqless/queue_priority_pattern.rb +16 -0
- data/lib/reqless/server/static/css/bootstrap-responsive.css +686 -0
- data/lib/reqless/server/static/css/bootstrap-responsive.min.css +12 -0
- data/lib/reqless/server/static/css/bootstrap.css +3991 -0
- data/lib/reqless/server/static/css/bootstrap.min.css +689 -0
- data/lib/reqless/server/static/css/codemirror.css +112 -0
- data/lib/reqless/server/static/css/docs.css +839 -0
- data/lib/reqless/server/static/css/jquery.noty.css +105 -0
- data/lib/reqless/server/static/css/noty_theme_twitter.css +137 -0
- data/lib/reqless/server/static/css/style.css +200 -0
- data/lib/reqless/server/static/favicon.ico +0 -0
- data/lib/reqless/server/static/img/glyphicons-halflings-white.png +0 -0
- data/lib/reqless/server/static/img/glyphicons-halflings.png +0 -0
- data/lib/reqless/server/static/js/bootstrap-alert.js +94 -0
- data/lib/reqless/server/static/js/bootstrap-scrollspy.js +125 -0
- data/lib/reqless/server/static/js/bootstrap-tab.js +130 -0
- data/lib/reqless/server/static/js/bootstrap-tooltip.js +270 -0
- data/lib/reqless/server/static/js/bootstrap-typeahead.js +285 -0
- data/lib/reqless/server/static/js/bootstrap.js +1726 -0
- data/lib/reqless/server/static/js/bootstrap.min.js +6 -0
- data/lib/reqless/server/static/js/codemirror.js +2972 -0
- data/lib/reqless/server/static/js/jquery.noty.js +220 -0
- data/lib/reqless/server/static/js/mode/javascript.js +360 -0
- data/lib/reqless/server/static/js/theme/cobalt.css +18 -0
- data/lib/reqless/server/static/js/theme/eclipse.css +25 -0
- data/lib/reqless/server/static/js/theme/elegant.css +10 -0
- data/lib/reqless/server/static/js/theme/lesser-dark.css +45 -0
- data/lib/reqless/server/static/js/theme/monokai.css +28 -0
- data/lib/reqless/server/static/js/theme/neat.css +9 -0
- data/lib/reqless/server/static/js/theme/night.css +21 -0
- data/lib/reqless/server/static/js/theme/rubyblue.css +21 -0
- data/lib/reqless/server/static/js/theme/xq-dark.css +46 -0
- data/lib/reqless/server/views/_job.erb +259 -0
- data/lib/reqless/server/views/_job_list.erb +8 -0
- data/lib/reqless/server/views/_pagination.erb +7 -0
- data/lib/reqless/server/views/about.erb +130 -0
- data/lib/reqless/server/views/completed.erb +11 -0
- data/lib/reqless/server/views/config.erb +14 -0
- data/lib/reqless/server/views/failed.erb +48 -0
- data/lib/reqless/server/views/failed_type.erb +18 -0
- data/lib/reqless/server/views/job.erb +17 -0
- data/lib/reqless/server/views/layout.erb +451 -0
- data/lib/reqless/server/views/overview.erb +137 -0
- data/lib/reqless/server/views/queue.erb +125 -0
- data/lib/reqless/server/views/queues.erb +45 -0
- data/lib/reqless/server/views/tag.erb +6 -0
- data/lib/reqless/server/views/throttles.erb +38 -0
- data/lib/reqless/server/views/track.erb +75 -0
- data/lib/reqless/server/views/worker.erb +34 -0
- data/lib/reqless/server/views/workers.erb +14 -0
- data/lib/reqless/server.rb +549 -0
- data/lib/reqless/subscriber.rb +74 -0
- data/lib/reqless/test_helpers/worker_helpers.rb +55 -0
- data/lib/reqless/throttle.rb +57 -0
- data/lib/reqless/version.rb +5 -0
- data/lib/reqless/worker/base.rb +237 -0
- data/lib/reqless/worker/forking.rb +215 -0
- data/lib/reqless/worker/serial.rb +41 -0
- data/lib/reqless/worker.rb +5 -0
- data/lib/reqless.rb +309 -0
- metadata +399 -0
@@ -0,0 +1,130 @@
|
|
1
|
+
/* ========================================================
|
2
|
+
* bootstrap-tab.js v2.0.2
|
3
|
+
* http://twitter.github.com/bootstrap/javascript.html#tabs
|
4
|
+
* ========================================================
|
5
|
+
* Copyright 2012 Twitter, Inc.
|
6
|
+
*
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
* you may not use this file except in compliance with the License.
|
9
|
+
* You may obtain a copy of the License at
|
10
|
+
*
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
*
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
* See the License for the specific language governing permissions and
|
17
|
+
* limitations under the License.
|
18
|
+
* ======================================================== */
|
19
|
+
|
20
|
+
|
21
|
+
!function( $ ){
|
22
|
+
|
23
|
+
"use strict"
|
24
|
+
|
25
|
+
/* TAB CLASS DEFINITION
|
26
|
+
* ==================== */
|
27
|
+
|
28
|
+
var Tab = function ( element ) {
|
29
|
+
this.element = $(element)
|
30
|
+
}
|
31
|
+
|
32
|
+
Tab.prototype = {
|
33
|
+
|
34
|
+
constructor: Tab
|
35
|
+
|
36
|
+
, show: function () {
|
37
|
+
var $this = this.element
|
38
|
+
, $ul = $this.closest('ul:not(.dropdown-menu)')
|
39
|
+
, selector = $this.attr('data-target')
|
40
|
+
, previous
|
41
|
+
, $target
|
42
|
+
|
43
|
+
if (!selector) {
|
44
|
+
selector = $this.attr('href')
|
45
|
+
selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
|
46
|
+
}
|
47
|
+
|
48
|
+
if ( $this.parent('li').hasClass('active') ) return
|
49
|
+
|
50
|
+
previous = $ul.find('.active a').last()[0]
|
51
|
+
|
52
|
+
$this.trigger({
|
53
|
+
type: 'show'
|
54
|
+
, relatedTarget: previous
|
55
|
+
})
|
56
|
+
|
57
|
+
$target = $(selector)
|
58
|
+
|
59
|
+
this.activate($this.parent('li'), $ul)
|
60
|
+
this.activate($target, $target.parent(), function () {
|
61
|
+
$this.trigger({
|
62
|
+
type: 'shown'
|
63
|
+
, relatedTarget: previous
|
64
|
+
})
|
65
|
+
})
|
66
|
+
}
|
67
|
+
|
68
|
+
, activate: function ( element, container, callback) {
|
69
|
+
var $active = container.find('> .active')
|
70
|
+
, transition = callback
|
71
|
+
&& $.support.transition
|
72
|
+
&& $active.hasClass('fade')
|
73
|
+
|
74
|
+
function next() {
|
75
|
+
$active
|
76
|
+
.removeClass('active')
|
77
|
+
.find('> .dropdown-menu > .active')
|
78
|
+
.removeClass('active')
|
79
|
+
|
80
|
+
element.addClass('active')
|
81
|
+
|
82
|
+
if (transition) {
|
83
|
+
element[0].offsetWidth // reflow for transition
|
84
|
+
element.addClass('in')
|
85
|
+
} else {
|
86
|
+
element.removeClass('fade')
|
87
|
+
}
|
88
|
+
|
89
|
+
if ( element.parent('.dropdown-menu') ) {
|
90
|
+
element.closest('li.dropdown').addClass('active')
|
91
|
+
}
|
92
|
+
|
93
|
+
callback && callback()
|
94
|
+
}
|
95
|
+
|
96
|
+
transition ?
|
97
|
+
$active.one($.support.transition.end, next) :
|
98
|
+
next()
|
99
|
+
|
100
|
+
$active.removeClass('in')
|
101
|
+
}
|
102
|
+
}
|
103
|
+
|
104
|
+
|
105
|
+
/* TAB PLUGIN DEFINITION
|
106
|
+
* ===================== */
|
107
|
+
|
108
|
+
$.fn.tab = function ( option ) {
|
109
|
+
return this.each(function () {
|
110
|
+
var $this = $(this)
|
111
|
+
, data = $this.data('tab')
|
112
|
+
if (!data) $this.data('tab', (data = new Tab(this)))
|
113
|
+
if (typeof option == 'string') data[option]()
|
114
|
+
})
|
115
|
+
}
|
116
|
+
|
117
|
+
$.fn.tab.Constructor = Tab
|
118
|
+
|
119
|
+
|
120
|
+
/* TAB DATA-API
|
121
|
+
* ============ */
|
122
|
+
|
123
|
+
$(function () {
|
124
|
+
$('body').on('click.tab.data-api', '[data-toggle="tab"], [data-toggle="pill"]', function (e) {
|
125
|
+
e.preventDefault()
|
126
|
+
$(this).tab('show')
|
127
|
+
})
|
128
|
+
})
|
129
|
+
|
130
|
+
}( window.jQuery );
|
@@ -0,0 +1,270 @@
|
|
1
|
+
/* ===========================================================
|
2
|
+
* bootstrap-tooltip.js v2.0.2
|
3
|
+
* http://twitter.github.com/bootstrap/javascript.html#tooltips
|
4
|
+
* Inspired by the original jQuery.tipsy by Jason Frame
|
5
|
+
* ===========================================================
|
6
|
+
* Copyright 2012 Twitter, Inc.
|
7
|
+
*
|
8
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
9
|
+
* you may not use this file except in compliance with the License.
|
10
|
+
* You may obtain a copy of the License at
|
11
|
+
*
|
12
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
13
|
+
*
|
14
|
+
* Unless required by applicable law or agreed to in writing, software
|
15
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
16
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
17
|
+
* See the License for the specific language governing permissions and
|
18
|
+
* limitations under the License.
|
19
|
+
* ========================================================== */
|
20
|
+
|
21
|
+
!function( $ ) {
|
22
|
+
|
23
|
+
"use strict"
|
24
|
+
|
25
|
+
/* TOOLTIP PUBLIC CLASS DEFINITION
|
26
|
+
* =============================== */
|
27
|
+
|
28
|
+
var Tooltip = function ( element, options ) {
|
29
|
+
this.init('tooltip', element, options)
|
30
|
+
}
|
31
|
+
|
32
|
+
Tooltip.prototype = {
|
33
|
+
|
34
|
+
constructor: Tooltip
|
35
|
+
|
36
|
+
, init: function ( type, element, options ) {
|
37
|
+
var eventIn
|
38
|
+
, eventOut
|
39
|
+
|
40
|
+
this.type = type
|
41
|
+
this.$element = $(element)
|
42
|
+
this.options = this.getOptions(options)
|
43
|
+
this.enabled = true
|
44
|
+
|
45
|
+
if (this.options.trigger != 'manual') {
|
46
|
+
eventIn = this.options.trigger == 'hover' ? 'mouseenter' : 'focus'
|
47
|
+
eventOut = this.options.trigger == 'hover' ? 'mouseleave' : 'blur'
|
48
|
+
this.$element.on(eventIn, this.options.selector, $.proxy(this.enter, this))
|
49
|
+
this.$element.on(eventOut, this.options.selector, $.proxy(this.leave, this))
|
50
|
+
}
|
51
|
+
|
52
|
+
this.options.selector ?
|
53
|
+
(this._options = $.extend({}, this.options, { trigger: 'manual', selector: '' })) :
|
54
|
+
this.fixTitle()
|
55
|
+
}
|
56
|
+
|
57
|
+
, getOptions: function ( options ) {
|
58
|
+
options = $.extend({}, $.fn[this.type].defaults, options, this.$element.data())
|
59
|
+
|
60
|
+
if (options.delay && typeof options.delay == 'number') {
|
61
|
+
options.delay = {
|
62
|
+
show: options.delay
|
63
|
+
, hide: options.delay
|
64
|
+
}
|
65
|
+
}
|
66
|
+
|
67
|
+
return options
|
68
|
+
}
|
69
|
+
|
70
|
+
, enter: function ( e ) {
|
71
|
+
var self = $(e.currentTarget)[this.type](this._options).data(this.type)
|
72
|
+
|
73
|
+
if (!self.options.delay || !self.options.delay.show) {
|
74
|
+
self.show()
|
75
|
+
} else {
|
76
|
+
self.hoverState = 'in'
|
77
|
+
setTimeout(function() {
|
78
|
+
if (self.hoverState == 'in') {
|
79
|
+
self.show()
|
80
|
+
}
|
81
|
+
}, self.options.delay.show)
|
82
|
+
}
|
83
|
+
}
|
84
|
+
|
85
|
+
, leave: function ( e ) {
|
86
|
+
var self = $(e.currentTarget)[this.type](this._options).data(this.type)
|
87
|
+
|
88
|
+
if (!self.options.delay || !self.options.delay.hide) {
|
89
|
+
self.hide()
|
90
|
+
} else {
|
91
|
+
self.hoverState = 'out'
|
92
|
+
setTimeout(function() {
|
93
|
+
if (self.hoverState == 'out') {
|
94
|
+
self.hide()
|
95
|
+
}
|
96
|
+
}, self.options.delay.hide)
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
, show: function () {
|
101
|
+
var $tip
|
102
|
+
, inside
|
103
|
+
, pos
|
104
|
+
, actualWidth
|
105
|
+
, actualHeight
|
106
|
+
, placement
|
107
|
+
, tp
|
108
|
+
|
109
|
+
if (this.hasContent() && this.enabled) {
|
110
|
+
$tip = this.tip()
|
111
|
+
this.setContent()
|
112
|
+
|
113
|
+
if (this.options.animation) {
|
114
|
+
$tip.addClass('fade')
|
115
|
+
}
|
116
|
+
|
117
|
+
placement = typeof this.options.placement == 'function' ?
|
118
|
+
this.options.placement.call(this, $tip[0], this.$element[0]) :
|
119
|
+
this.options.placement
|
120
|
+
|
121
|
+
inside = /in/.test(placement)
|
122
|
+
|
123
|
+
$tip
|
124
|
+
.remove()
|
125
|
+
.css({ top: 0, left: 0, display: 'block' })
|
126
|
+
.appendTo(inside ? this.$element : document.body)
|
127
|
+
|
128
|
+
pos = this.getPosition(inside)
|
129
|
+
|
130
|
+
actualWidth = $tip[0].offsetWidth
|
131
|
+
actualHeight = $tip[0].offsetHeight
|
132
|
+
|
133
|
+
switch (inside ? placement.split(' ')[1] : placement) {
|
134
|
+
case 'bottom':
|
135
|
+
tp = {top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}
|
136
|
+
break
|
137
|
+
case 'top':
|
138
|
+
tp = {top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}
|
139
|
+
break
|
140
|
+
case 'left':
|
141
|
+
tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth}
|
142
|
+
break
|
143
|
+
case 'right':
|
144
|
+
tp = {top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width}
|
145
|
+
break
|
146
|
+
}
|
147
|
+
|
148
|
+
$tip
|
149
|
+
.css(tp)
|
150
|
+
.addClass(placement)
|
151
|
+
.addClass('in')
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
, setContent: function () {
|
156
|
+
var $tip = this.tip()
|
157
|
+
$tip.find('.tooltip-inner').html(this.getTitle())
|
158
|
+
$tip.removeClass('fade in top bottom left right')
|
159
|
+
}
|
160
|
+
|
161
|
+
, hide: function () {
|
162
|
+
var that = this
|
163
|
+
, $tip = this.tip()
|
164
|
+
|
165
|
+
$tip.removeClass('in')
|
166
|
+
|
167
|
+
function removeWithAnimation() {
|
168
|
+
var timeout = setTimeout(function () {
|
169
|
+
$tip.off($.support.transition.end).remove()
|
170
|
+
}, 500)
|
171
|
+
|
172
|
+
$tip.one($.support.transition.end, function () {
|
173
|
+
clearTimeout(timeout)
|
174
|
+
$tip.remove()
|
175
|
+
})
|
176
|
+
}
|
177
|
+
|
178
|
+
$.support.transition && this.$tip.hasClass('fade') ?
|
179
|
+
removeWithAnimation() :
|
180
|
+
$tip.remove()
|
181
|
+
}
|
182
|
+
|
183
|
+
, fixTitle: function () {
|
184
|
+
var $e = this.$element
|
185
|
+
if ($e.attr('title') || typeof($e.attr('data-original-title')) != 'string') {
|
186
|
+
$e.attr('data-original-title', $e.attr('title') || '').removeAttr('title')
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
, hasContent: function () {
|
191
|
+
return this.getTitle()
|
192
|
+
}
|
193
|
+
|
194
|
+
, getPosition: function (inside) {
|
195
|
+
return $.extend({}, (inside ? {top: 0, left: 0} : this.$element.offset()), {
|
196
|
+
width: this.$element[0].offsetWidth
|
197
|
+
, height: this.$element[0].offsetHeight
|
198
|
+
})
|
199
|
+
}
|
200
|
+
|
201
|
+
, getTitle: function () {
|
202
|
+
var title
|
203
|
+
, $e = this.$element
|
204
|
+
, o = this.options
|
205
|
+
|
206
|
+
title = $e.attr('data-original-title')
|
207
|
+
|| (typeof o.title == 'function' ? o.title.call($e[0]) : o.title)
|
208
|
+
|
209
|
+
title = (title || '').toString().replace(/(^\s*|\s*$)/, "")
|
210
|
+
|
211
|
+
return title
|
212
|
+
}
|
213
|
+
|
214
|
+
, tip: function () {
|
215
|
+
return this.$tip = this.$tip || $(this.options.template)
|
216
|
+
}
|
217
|
+
|
218
|
+
, validate: function () {
|
219
|
+
if (!this.$element[0].parentNode) {
|
220
|
+
this.hide()
|
221
|
+
this.$element = null
|
222
|
+
this.options = null
|
223
|
+
}
|
224
|
+
}
|
225
|
+
|
226
|
+
, enable: function () {
|
227
|
+
this.enabled = true
|
228
|
+
}
|
229
|
+
|
230
|
+
, disable: function () {
|
231
|
+
this.enabled = false
|
232
|
+
}
|
233
|
+
|
234
|
+
, toggleEnabled: function () {
|
235
|
+
this.enabled = !this.enabled
|
236
|
+
}
|
237
|
+
|
238
|
+
, toggle: function () {
|
239
|
+
this[this.tip().hasClass('in') ? 'hide' : 'show']()
|
240
|
+
}
|
241
|
+
|
242
|
+
}
|
243
|
+
|
244
|
+
|
245
|
+
/* TOOLTIP PLUGIN DEFINITION
|
246
|
+
* ========================= */
|
247
|
+
|
248
|
+
$.fn.tooltip = function ( option ) {
|
249
|
+
return this.each(function () {
|
250
|
+
var $this = $(this)
|
251
|
+
, data = $this.data('tooltip')
|
252
|
+
, options = typeof option == 'object' && option
|
253
|
+
if (!data) $this.data('tooltip', (data = new Tooltip(this, options)))
|
254
|
+
if (typeof option == 'string') data[option]()
|
255
|
+
})
|
256
|
+
}
|
257
|
+
|
258
|
+
$.fn.tooltip.Constructor = Tooltip
|
259
|
+
|
260
|
+
$.fn.tooltip.defaults = {
|
261
|
+
animation: true
|
262
|
+
, delay: 0
|
263
|
+
, selector: false
|
264
|
+
, placement: 'top'
|
265
|
+
, trigger: 'hover'
|
266
|
+
, title: ''
|
267
|
+
, template: '<div class="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>'
|
268
|
+
}
|
269
|
+
|
270
|
+
}( window.jQuery );
|
@@ -0,0 +1,285 @@
|
|
1
|
+
/* =============================================================
|
2
|
+
* bootstrap-typeahead.js v2.0.3
|
3
|
+
* http://twitter.github.com/bootstrap/javascript.html#typeahead
|
4
|
+
* =============================================================
|
5
|
+
* Copyright 2012 Twitter, Inc.
|
6
|
+
*
|
7
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
* you may not use this file except in compliance with the License.
|
9
|
+
* You may obtain a copy of the License at
|
10
|
+
*
|
11
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
*
|
13
|
+
* Unless required by applicable law or agreed to in writing, software
|
14
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
* See the License for the specific language governing permissions and
|
17
|
+
* limitations under the License.
|
18
|
+
* ============================================================ */
|
19
|
+
|
20
|
+
|
21
|
+
!function($){
|
22
|
+
|
23
|
+
"use strict"; // jshint ;_;
|
24
|
+
|
25
|
+
|
26
|
+
/* TYPEAHEAD PUBLIC CLASS DEFINITION
|
27
|
+
* ================================= */
|
28
|
+
|
29
|
+
var Typeahead = function (element, options) {
|
30
|
+
this.$element = $(element)
|
31
|
+
this.options = $.extend({}, $.fn.typeahead.defaults, options)
|
32
|
+
this.matcher = this.options.matcher || this.matcher
|
33
|
+
this.sorter = this.options.sorter || this.sorter
|
34
|
+
this.highlighter = this.options.highlighter || this.highlighter
|
35
|
+
this.updater = this.options.updater || this.updater
|
36
|
+
this.$menu = $(this.options.menu).appendTo('body')
|
37
|
+
this.source = this.options.source
|
38
|
+
this.shown = false
|
39
|
+
this.listen()
|
40
|
+
}
|
41
|
+
|
42
|
+
Typeahead.prototype = {
|
43
|
+
|
44
|
+
constructor: Typeahead
|
45
|
+
|
46
|
+
, select: function () {
|
47
|
+
var val = this.$menu.find('.active').attr('data-value')
|
48
|
+
this.$element
|
49
|
+
.val(this.updater(val))
|
50
|
+
.change()
|
51
|
+
return this.hide()
|
52
|
+
}
|
53
|
+
|
54
|
+
, updater: function (item) {
|
55
|
+
return item
|
56
|
+
}
|
57
|
+
|
58
|
+
, show: function () {
|
59
|
+
var pos = $.extend({}, this.$element.position(), {
|
60
|
+
height: this.$element[0].offsetHeight
|
61
|
+
})
|
62
|
+
|
63
|
+
this.$menu.css({
|
64
|
+
top: pos.top + pos.height
|
65
|
+
, left: pos.left
|
66
|
+
})
|
67
|
+
|
68
|
+
this.$menu.show()
|
69
|
+
this.shown = true
|
70
|
+
return this
|
71
|
+
}
|
72
|
+
|
73
|
+
, hide: function () {
|
74
|
+
this.$menu.hide()
|
75
|
+
this.shown = false
|
76
|
+
return this
|
77
|
+
}
|
78
|
+
|
79
|
+
, lookup: function (event) {
|
80
|
+
var that = this
|
81
|
+
, items
|
82
|
+
, q
|
83
|
+
|
84
|
+
this.query = this.$element.val()
|
85
|
+
|
86
|
+
if (!this.query) {
|
87
|
+
return this.shown ? this.hide() : this
|
88
|
+
}
|
89
|
+
|
90
|
+
items = $.grep(this.source, function (item) {
|
91
|
+
return that.matcher(item)
|
92
|
+
})
|
93
|
+
|
94
|
+
items = this.sorter(items)
|
95
|
+
|
96
|
+
if (!items.length) {
|
97
|
+
return this.shown ? this.hide() : this
|
98
|
+
}
|
99
|
+
|
100
|
+
return this.render(items.slice(0, this.options.items)).show()
|
101
|
+
}
|
102
|
+
|
103
|
+
, matcher: function (item) {
|
104
|
+
return ~item.toLowerCase().indexOf(this.query.toLowerCase())
|
105
|
+
}
|
106
|
+
|
107
|
+
, sorter: function (items) {
|
108
|
+
var beginswith = []
|
109
|
+
, caseSensitive = []
|
110
|
+
, caseInsensitive = []
|
111
|
+
, item
|
112
|
+
|
113
|
+
while (item = items.shift()) {
|
114
|
+
if (!item.toLowerCase().indexOf(this.query.toLowerCase())) beginswith.push(item)
|
115
|
+
else if (~item.indexOf(this.query)) caseSensitive.push(item)
|
116
|
+
else caseInsensitive.push(item)
|
117
|
+
}
|
118
|
+
|
119
|
+
return beginswith.concat(caseSensitive, caseInsensitive)
|
120
|
+
}
|
121
|
+
|
122
|
+
, highlighter: function (item) {
|
123
|
+
var query = this.query.replace(/[\-\[\]{}()*+?.,\\\^$|#\s]/g, '\\$&')
|
124
|
+
return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
|
125
|
+
return '<strong>' + match + '</strong>'
|
126
|
+
})
|
127
|
+
}
|
128
|
+
|
129
|
+
, render: function (items) {
|
130
|
+
var that = this
|
131
|
+
|
132
|
+
items = $(items).map(function (i, item) {
|
133
|
+
i = $(that.options.item).attr('data-value', item)
|
134
|
+
i.find('a').html(that.highlighter(item))
|
135
|
+
return i[0]
|
136
|
+
})
|
137
|
+
|
138
|
+
items.first().addClass('active')
|
139
|
+
this.$menu.html(items)
|
140
|
+
return this
|
141
|
+
}
|
142
|
+
|
143
|
+
, next: function (event) {
|
144
|
+
var active = this.$menu.find('.active').removeClass('active')
|
145
|
+
, next = active.next()
|
146
|
+
|
147
|
+
if (!next.length) {
|
148
|
+
next = $(this.$menu.find('li')[0])
|
149
|
+
}
|
150
|
+
|
151
|
+
next.addClass('active')
|
152
|
+
}
|
153
|
+
|
154
|
+
, prev: function (event) {
|
155
|
+
var active = this.$menu.find('.active').removeClass('active')
|
156
|
+
, prev = active.prev()
|
157
|
+
|
158
|
+
if (!prev.length) {
|
159
|
+
prev = this.$menu.find('li').last()
|
160
|
+
}
|
161
|
+
|
162
|
+
prev.addClass('active')
|
163
|
+
}
|
164
|
+
|
165
|
+
, listen: function () {
|
166
|
+
this.$element
|
167
|
+
.on('blur', $.proxy(this.blur, this))
|
168
|
+
.on('keypress', $.proxy(this.keypress, this))
|
169
|
+
.on('keyup', $.proxy(this.keyup, this))
|
170
|
+
|
171
|
+
if ($.browser.webkit || $.browser.msie) {
|
172
|
+
this.$element.on('keydown', $.proxy(this.keypress, this))
|
173
|
+
}
|
174
|
+
|
175
|
+
this.$menu
|
176
|
+
.on('click', $.proxy(this.click, this))
|
177
|
+
.on('mouseenter', 'li', $.proxy(this.mouseenter, this))
|
178
|
+
}
|
179
|
+
|
180
|
+
, keyup: function (e) {
|
181
|
+
switch(e.keyCode) {
|
182
|
+
case 40: // down arrow
|
183
|
+
case 38: // up arrow
|
184
|
+
break
|
185
|
+
|
186
|
+
case 9: // tab
|
187
|
+
case 13: // enter
|
188
|
+
if (!this.shown) return
|
189
|
+
this.select()
|
190
|
+
break
|
191
|
+
|
192
|
+
case 27: // escape
|
193
|
+
if (!this.shown) return
|
194
|
+
this.hide()
|
195
|
+
break
|
196
|
+
|
197
|
+
default:
|
198
|
+
this.lookup()
|
199
|
+
}
|
200
|
+
|
201
|
+
e.stopPropagation()
|
202
|
+
e.preventDefault()
|
203
|
+
}
|
204
|
+
|
205
|
+
, keypress: function (e) {
|
206
|
+
if (!this.shown) return
|
207
|
+
|
208
|
+
switch(e.keyCode) {
|
209
|
+
case 9: // tab
|
210
|
+
case 13: // enter
|
211
|
+
case 27: // escape
|
212
|
+
e.preventDefault()
|
213
|
+
break
|
214
|
+
|
215
|
+
case 38: // up arrow
|
216
|
+
if (e.type != 'keydown') break
|
217
|
+
e.preventDefault()
|
218
|
+
this.prev()
|
219
|
+
break
|
220
|
+
|
221
|
+
case 40: // down arrow
|
222
|
+
if (e.type != 'keydown') break
|
223
|
+
e.preventDefault()
|
224
|
+
this.next()
|
225
|
+
break
|
226
|
+
}
|
227
|
+
|
228
|
+
e.stopPropagation()
|
229
|
+
}
|
230
|
+
|
231
|
+
, blur: function (e) {
|
232
|
+
var that = this
|
233
|
+
setTimeout(function () { that.hide() }, 150)
|
234
|
+
}
|
235
|
+
|
236
|
+
, click: function (e) {
|
237
|
+
e.stopPropagation()
|
238
|
+
e.preventDefault()
|
239
|
+
this.select()
|
240
|
+
}
|
241
|
+
|
242
|
+
, mouseenter: function (e) {
|
243
|
+
this.$menu.find('.active').removeClass('active')
|
244
|
+
$(e.currentTarget).addClass('active')
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
/* TYPEAHEAD PLUGIN DEFINITION
|
251
|
+
* =========================== */
|
252
|
+
|
253
|
+
$.fn.typeahead = function (option) {
|
254
|
+
return this.each(function () {
|
255
|
+
var $this = $(this)
|
256
|
+
, data = $this.data('typeahead')
|
257
|
+
, options = typeof option == 'object' && option
|
258
|
+
if (!data) $this.data('typeahead', (data = new Typeahead(this, options)))
|
259
|
+
if (typeof option == 'string') data[option]()
|
260
|
+
})
|
261
|
+
}
|
262
|
+
|
263
|
+
$.fn.typeahead.defaults = {
|
264
|
+
source: []
|
265
|
+
, items: 8
|
266
|
+
, menu: '<ul class="typeahead dropdown-menu"></ul>'
|
267
|
+
, item: '<li><a href="#"></a></li>'
|
268
|
+
}
|
269
|
+
|
270
|
+
$.fn.typeahead.Constructor = Typeahead
|
271
|
+
|
272
|
+
|
273
|
+
/* TYPEAHEAD DATA-API
|
274
|
+
* ================== */
|
275
|
+
|
276
|
+
$(function () {
|
277
|
+
$('body').on('focus.typeahead.data-api', '[data-provide="typeahead"]', function (e) {
|
278
|
+
var $this = $(this)
|
279
|
+
if ($this.data('typeahead')) return
|
280
|
+
e.preventDefault()
|
281
|
+
$this.typeahead($this.data())
|
282
|
+
})
|
283
|
+
})
|
284
|
+
|
285
|
+
}(window.jQuery);
|