-
Notifications
You must be signed in to change notification settings - Fork 28
/
sm-search.js
130 lines (109 loc) · 3.65 KB
/
sm-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
(function(app)
{
app
.factory('SemanticSearchLink', ['SemanticUI', SemanticSearchLink])
.directive('smSearchBind', ['SemanticUI', SemanticSearchBind])
.directive('smSearch', ['SemanticSearchLink', SemanticSearch])
;
var BEHAVIORS = {
smSearchQuery: 'query',
smSearchCancelQuery: 'cancel query',
smSearchSearchLocal: 'search local',
smSearchSearchRemote: 'search remote',
smSearchSet: 'set value',
smSearchShowResults: 'show results',
smSearchHideResults: 'hide results',
smSearchDestroy: 'destroy'
};
angular.forEach( BEHAVIORS, function(method, directive)
{
app.directive( directive, ['SemanticUI', function(SemanticUI)
{
return SemanticUI.createBehavior( directive, 'search', method );
}]);
});
function SemanticSearchBind(SemanticUI)
{
return SemanticUI.createBind( 'smSearchBind', 'search' );
}
function SemanticSearch(SemanticSearchLink)
{
return {
restrict: 'E',
replace: true,
scope: {
/* Required */
model: '=',
/* Optional */
text: '=?',
icon: '@',
placeholder: '@',
category: '@',
local: '=',
remote: '@',
settings: '=',
onInit: '=',
/* Events */
onSelect: '=',
onResultsAdd: '=',
onSearchQuery: '=',
onResults: '=',
onResultsOpen: '=',
onResultsClose: '='
},
template: [
'<div class="ui search" ng-class="{category: category}">',
' <div class="ui input" ng-class="{icon: icon}">',
' <input class="prompt" type="text" placeholder="{{ placeholder }}" ng-model="text">',
' <i ng-if="icon" class="{{ icon }} icon"></i>',
' </div>',
' <div class="results"></div>',
'</div>'
].join('\n'),
link: SemanticSearchLink
};
}
function SemanticSearchLink(SemanticUI)
{
var defaultTitle = $.fn.search && $.fn.search.settings && $.fn.search.settings.fields ? $.fn.search.settings.fields.title : '';
return function(scope, element, attributes)
{
var settings = scope.settings || {};
var textProperty = settings.fields && settings.fields.title ? settings.fields.title : defaultTitle;
SemanticUI.linkSettings( scope, element, attributes, 'search' );
if ( scope.local ) settings.source = scope.local;
if ( scope.remote ) settings.apiSettings = { url: scope.remote };
if ( scope.category ) settings.type = 'category';
var modelWatcher = SemanticUI.watcher( scope, 'model',
function(value) {
element.search( 'set value', value && (textProperty in value) ? value[ textProperty ] : value );
}
);
SemanticUI.onEvent( settings, 'onSelect',
function(result, response) {
modelWatcher.set( result );
if ( attributes.text ) {
scope.$evalAsync(function() {
scope.text = result[ textProperty ];
});
}
}
);
SemanticUI.linkEvents( scope, settings, $.fn.search.settings, {
onSelect: 'onSelect',
onResultsAdd: 'onResultsAdd',
onSearchQuery: 'onSearchQuery',
onResults: 'onResults',
onResultsOpen: 'onResultsOpen',
onResultsClose: 'onResultsClose'
});
element.search( settings );
if ( angular.isFunction( scope.onInit ) ) {
scope.onInit( element );
}
if ( scope.model && attributes.text && textProperty in scope.model ) {
scope.text = scope.model[ textProperty ];
}
};
}
})( angular.module('semantic-ui-search', ['semantic-ui-core']) );