example-datasources.js
3.88 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
164
165
166
167
var navCategories = [
"Data Management",
"Editors",
"Layout",
"Data Visualization",
"Diagramming",
"Geo Visualization",
"Scheduling",
"Navigation",
"Interactivity & UX",
"Framework",
"Mobile"
];
var navDataSource = new kendo.data.DataSource({
transport: {
read: {
url: NAV_JSON_URL,
dataType: "json"
}
},
schema: {
model: {
id: "name"
}
},
change: function(e) {
this.view().sort(function(a, b) {
var indexA = $.inArray(a.value, navCategories);
var indexB = $.inArray(b.value, navCategories);
return indexA > indexB ? 1 : -1;
});
},
filter: { field: "disableInMobile", operator: "neq", value: true },
group: { field: "category" }
});
var detailNavDataSource = new kendo.data.DataSource({
schema: {
model: {
id: "url"
}
}
});
var searchDataSource = new kendo.data.DataSource({
group: { field: "section" }
});
function onlineExamples(section, product, reject) {
return $.grep(section.items, function (item) {
item.section = section.text;
if (reject(item)) {
return false;
}
var packages = item.packages || section.packages;
if (!packages) {
return true;
}
var invert = false, match = false;
for (var i = 0; i < packages.length; i++) {
var packageName = packages[i];
if (packageName === "!" + product) {
return false;
}
if (packageName[0] === "!") {
invert = true;
}
if (packageName === product) {
match = true;
}
if (packageName === "offline") {
return false;
}
}
var result = (!invert && match) || (invert && !match);
return result;
});
}
function mobileExamples(section) {
return onlineExamples(section, navProduct, function(item) {
return false;
});
}
function desktopExamples(section) {
return onlineExamples(section, navProduct, function(item) {
return false;
});
}
function populateSearchDataSource(filter) {
navDataSource.fetch(function() {
var items = [];
var data = navDataSource.data();
for (var i = 0; i < data.length; i ++) {
items = items.concat(filter(data[i]));
}
searchDataSource.data(items);
});
}
function searchExamplesFor(value, product) {
function titleContains(value) {
return function (title) {
var text = "";
if (title) {
text = title[product] || title["kendo-ui"];
}
return text.indexOf(value) >= 0;
};
}
if (value.length < 3) {
searchDataSource.filter(null);
} else {
var filter = { logic: "and", filters: []};
var words = value.split(" ");
for (var i = 0; i < words.length; i ++) {
var word = words[i];
filter.filters.push({
logic: "or",
filters: [
{ field: "section", operator: "contains", value: word },
{ field: "text", operator: "contains", value: word },
{ field: "title", operator: titleContains(word) }
]
});
}
searchDataSource.filter(filter);
}
}
kendo.ui.plugin(kendo.ui.AutoComplete.extend({
init: function(element, options) {
kendo.ui.AutoComplete.fn.init.call(this,
element,
$.extend(true, { dataSource: searchDataSource }, options)
);
this.wrapper.append('<span class="k-uniE600" />');
},
options: {
name: "ExampleSearch"
},
_filterSource: function() {
searchExamplesFor(this.value(), this.options.product);
}
}));