-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.js
138 lines (119 loc) · 3.72 KB
/
search.js
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
/**
@ngdoc directive
@restrict E
@name bp.directive:bpSearch
@example
<pre>
<bp-search>
<input ng-model="mymodel">
</bp-search>
</pre>
@description A search box that recreates native behavior on both ios and android.
You have to create an `<input>` field within the directive yourself, so you can control the associated model.
*/
angular.module('bp').directive('bpSearch', function(
$compile,
$timeout,
$window,
bpApp) {
return {
restrict: 'E',
link: function(scope, element, attrs) {
var ios = bpApp.platform === 'ios'
var childScope = scope.$new(true)
var $bgLeft, $bgRight, $cancel
if (ios) {
$bgLeft = angular.element('<bp-search-bg-left>')
$bgRight = angular.element('<bp-search-bg-right>')
$cancel = $compile(angular.element('<bp-action>')
.addClass('bp-button')
.attr('ng-click', 'onCancel()')
.text('Cancel'))(childScope)
}
var $placeholder = $compile(angular.element('<bp-search-placeholder>')
.append(
angular.element('<bp-action>')
.addClass('bp-icon bp-icon-search')
)
.append(
angular.element('<span>')
.attr('ng-bind', 'placeholder')
)
)(childScope)
var $tapLayer = $compile(angular.element('<bp-search-tap>')
.attr('ng-click', 'onFocus()'))(childScope)
var $search = element.find('input')
$search.attr({
required: 'required',
type: $search.attr('type') || 'search'
})
childScope.placeholder = $search.attr('placeholder')
if (childScope.placeholder == null) {
childScope.placeholder = 'Search'
}
element
.attr('role', attrs.role || 'search')
.prepend($bgLeft, $bgRight)
.append($placeholder, $cancel, $tapLayer)
if (ios) {
var cancelWidth
$timeout(function() {
var width = element.outerWidth()
cancelWidth = $cancel.outerWidth()
var inputWidth = width - cancelWidth - 6
var iconWidth = $placeholder.find('.bp-icon').outerWidth()
$bgLeft.css('width', inputWidth)
$bgRight.css('width', cancelWidth)
$search.css({
width: inputWidth,
'padding-left': 1.5 * iconWidth
})
}, 50, false)
childScope.onResize = function() {
var inputWidth = element.outerWidth() - cancelWidth
$bgLeft.css('width', inputWidth)
}
childScope.onCancel = function() {
element.removeClass('focus')
$search.val('').trigger('input').trigger('blur', {
programatic: true
})
}
}
childScope.onBlur = function(e, extra) {
if (angular.isUndefined(extra)) {
extra = {}
}
if (!ios) {
element.removeClass('focus')
} else if (!$search.val() && !extra.programatic) {
childScope.onCancel()
}
}
childScope.onFocus = function() {
$search.focus()
$timeout(function() {
element.addClass('focus')
}, 0)
}
childScope.stopPropagation = function(e) {
e.stopPropagation()
e.stopImmediatePropagation()
}
if (ios) {
angular.element($window)
.bind('resize orientationchange', childScope.onResize)
}
$search.bind('blur', childScope.onBlur)
$tapLayer.bind('click touchstart touchmove touchend', childScope.stopPropagation)
scope.$on('$destroy', function() {
childScope.$destroy()
if (ios) {
angular.element($window).unbind('resize orientationchange')
}
$tapLayer.unbind('click touchstart touchmove touchend')
$search.unbind('blur')
})
}
}
})