forked from fex-team/kityminder-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchBox.directive.js
147 lines (115 loc) · 5.16 KB
/
searchBox.directive.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
139
140
141
142
143
144
145
146
147
angular.module('kityminderEditor')
.directive('searchBox', function() {
return {
restrict: 'A',
templateUrl: 'ui/directive/searchBox/searchBox.html',
scope: {
minder: '='
},
replace: true,
controller: function ($scope) {
var minder = $scope.minder;
var editor = window.editor;
$scope.handleKeyDown = handleKeyDown;
$scope.doSearch = doSearch;
$scope.exitSearch = exitSearch;
$scope.showTip = false;
$scope.showSearch = false;
// 处理输入框按键事件
function handleKeyDown(e) {
if (e.keyCode == 13) {
var direction = e.shiftKey ? 'prev' : 'next';
doSearch($scope.keyword, direction);
}
if (e.keyCode == 27) {
exitSearch();
}
}
function exitSearch() {
$('#search-input').blur();
$scope.showSearch = false;
minder.fire('hidenoterequest');
editor.receiver.selectAll();
}
function enterSearch() {
$scope.showSearch = true;
setTimeout(function() {
$('#search-input').focus();
}, 10);
if ($scope.keyword) {
$('#search-input')[0].setSelectionRange(0, $scope.keyword.length);
}
}
$('body').on('keydown', function(e) {
if (e.keyCode == 70 && (e.ctrlKey || e.metaKey) && !e.shiftKey) {
enterSearch();
$scope.$apply();
e.preventDefault();
}
});
minder.on('searchNode', function() {
enterSearch();
});
var nodeSequence = [];
var searchSequence = [];
minder.on('contentchange', makeNodeSequence);
makeNodeSequence();
function makeNodeSequence() {
nodeSequence = [];
minder.getRoot().traverse(function(node) {
nodeSequence.push(node);
});
}
function makeSearchSequence(keyword) {
searchSequence = [];
for (var i = 0; i < nodeSequence.length; i++) {
var node = nodeSequence[i];
var text = node.getText().toLowerCase();
if (text.indexOf(keyword) != -1) {
searchSequence.push({node:node});
}
var note = node.getData('note');
if (note && note.toLowerCase().indexOf(keyword) != -1) {
searchSequence.push({node: node, keyword: keyword});
}
}
}
function doSearch(keyword, direction) {
$scope.showTip = false;
minder.fire('hidenoterequest');
if (!keyword || !/\S/.exec(keyword)) {
$('#search-input').focus();
return;
}
// 当搜索不到节点时候默认的选项
$scope.showTip = true;
$scope.curIndex = 0;
$scope.resultNum = 0;
keyword = keyword.toLowerCase();
var newSearch = doSearch.lastKeyword != keyword;
doSearch.lastKeyword = keyword;
if (newSearch) {
makeSearchSequence(keyword);
}
$scope.resultNum = searchSequence.length;
if (searchSequence.length) {
var curIndex = newSearch ? 0 : (direction === 'next' ? doSearch.lastIndex + 1 : doSearch.lastIndex - 1) || 0;
curIndex = (searchSequence.length + curIndex) % searchSequence.length;
setSearchResult(searchSequence[curIndex].node, searchSequence[curIndex].keyword);
doSearch.lastIndex = curIndex;
$scope.curIndex = curIndex + 1;
function setSearchResult(node, previewKeyword) {
minder.execCommand('camera', node, 50);
setTimeout(function () {
minder.select(node, true);
if (!node.isExpanded()) minder.execCommand('expand', true);
if (previewKeyword) {
minder.fire('shownoterequest', {node: node, keyword: previewKeyword});
}
}, 60);
}
}
}
}
}
});