]> git.proxmox.com Git - extjs.git/blame - extjs/examples/classic/ticket-app/app/SimData.js
add extjs 6.0.1 sources
[extjs.git] / extjs / examples / classic / ticket-app / app / SimData.js
CommitLineData
6527f429
DM
1Ext.define('Ticket.EntitySimlet', {\r
2 extend: 'Ext.ux.ajax.JsonSimlet',\r
3 alias: 'simlet.entity',\r
4 \r
5 doPost: function(ctx) {\r
6 var result = this.callParent(arguments),\r
7 o = this.processData(Ext.decode(ctx.xhr.body)),\r
8 item = this.getById(this.data, o.id, true),\r
9 key;\r
10 \r
11 for (key in o) {\r
12 item[key] = o[key];\r
13 }\r
14 \r
15 result.responseText = Ext.encode(item);\r
16 return result;\r
17 },\r
18 \r
19 processData: Ext.identityFn,\r
20\r
21 getData: function (ctx) {\r
22 var params = ctx.params;\r
23\r
24 if ('id' in params) {\r
25 return this.getById(this.data, params.id);\r
26 }\r
27\r
28 delete this.currentOrder;\r
29 return this.callParent(arguments);\r
30 },\r
31 \r
32 getById: function(data, id) {\r
33 var len = data.length,\r
34 i, item;\r
35 \r
36 for (i = 0; i < len; ++i) {\r
37 item = data[i];\r
38 if (item.id === id) {\r
39 return item;\r
40 }\r
41 }\r
42 return null;\r
43 }\r
44});\r
45\r
46Ext.define('Ticket.SimData', {\r
47 requires: [\r
48 'Ext.ux.ajax.*'\r
49 ],\r
50\r
51 singleton: true,\r
52\r
53 dateFormat: 'Y-m-d\\TH:i:s\\Z',\r
54\r
55 words: 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum.'.replace(/[,\.]/g, '').split(' '),\r
56\r
57 /**\r
58 * We have our own "random" method because we need consistency (for testing).\r
59 */\r
60 random: (function () {\r
61 var modulus = 0x80000000, // 2^31\r
62 multiplier = 1664525,\r
63 increment = 1013904223,\r
64 seed = 1103515245;\r
65 // simple LCG\r
66\r
67 return function (min, max) {\r
68 seed = (multiplier * seed + increment) % modulus;\r
69 var x = seed / (modulus - 1); // [0, 1]\r
70\r
71 return Math.floor(x * (max - min + 1) + min);\r
72 };\r
73 }()),\r
74\r
75 sentence: function (min, max) {\r
76 var length = this.random(Ext.isDefined(min) ? min : 10, max || 30),\r
77 words = this.words,\r
78 description = Ext.String.capitalize(words[this.random(0, words.length - 1)]);\r
79\r
80 while (length--) {\r
81 description += ' ';\r
82 description += words[this.random(0, words.length - 1)];\r
83 }\r
84\r
85 description += '.';\r
86 return description;\r
87 },\r
88\r
89 paragraph: function (count) {\r
90 var length = count || this.random(2, 5),\r
91 ret = '';\r
92\r
93 while (length--) {\r
94 if (ret) {\r
95 ret += ' ';\r
96 }\r
97 ret += this.sentence();\r
98 }\r
99\r
100 return ret;\r
101 },\r
102\r
103 essay: function (count) {\r
104 var length = count || this.random(1, 4),\r
105 ret = '';\r
106\r
107 while (length--) {\r
108 if (ret) {\r
109 ret += '\n\n';\r
110 }\r
111 ret += this.paragraph();\r
112 }\r
113\r
114 return ret;\r
115 },\r
116\r
117 minDate: +Ext.Date.subtract(new Date(), Ext.Date.MONTH, 6),\r
118 maxDate: (+new Date()),\r
119\r
120 MILLIDAY: 60 * 60 * 24 * 1000,\r
121\r
122 randomDate: function (maxDays) {\r
123 maxDays = maxDays || 180;\r
124 var time = 1000 * this.random(1, maxDays * this.MILLIDAY / 1000);\r
125 return new Date(this.minDate + time);\r
126 },\r
127\r
128 nextDate: function (date, scale) {\r
129 scale = scale || (2 / 3);\r
130 var time = date.getTime(),\r
131 remaining = this.maxDate - time;\r
132\r
133 return new Date(time + 1000 * this.random(1, remaining * scale / 1000));\r
134 },\r
135\r
136 init: function () {\r
137 var me = this,\r
138 dateFormat = me.dateFormat,\r
139 comments = [],\r
140 organizations = [],\r
141 projects = [],\r
142 tickets = [],\r
143 users = [],\r
144 groups = [],\r
145 groupNames = ['Admins', 'Development', 'QA', 'Support', 'Sales'],\r
146 groupsByUserId = {},\r
147 usersByGroupId = {},\r
148 usersByKey = {},\r
149 data = {\r
150 Sencha: {\r
151 SDK: 'Don,Alex,Ben,Evan,Kevin,Nige,Phil,Pierre,Ross,Tommy',\r
152 IT: 'Len,Ian,Mike,Ryan'\r
153 }\r
154 };\r
155\r
156 Ext.Object.each(data, function (organizationName, projectsUsers) {\r
157 var organizationId = organizations.length + 1;\r
158 organizations.push({\r
159 id: organizationId,\r
160 name: organizationName\r
161 });\r
162\r
163 Ext.each(groupNames, function (name) {\r
164 groups.push({\r
165 id: groups.length + 1,\r
166 name: name,\r
167 organizationId: organizationId\r
168 })\r
169 });\r
170\r
171 Ext.Object.each(projectsUsers, function (projectName, projectUsers) {\r
172 var projectId = projects.length + 1,\r
173 firstUserId = users.length + 1,\r
174 project = {\r
175 id: projectId,\r
176 name: projectName,\r
177 organizationId: 1,\r
178 leadId: firstUserId\r
179 },\r
180 projectDate = me.randomDate(20);\r
181\r
182 projects.push(project);\r
183\r
184 Ext.Array.forEach(projectUsers.split(','), function (userName) {\r
185 var id = users.length + 1,\r
186 user = {\r
187 id: id,\r
188 name: userName,\r
189 projectId: projectId,\r
190 organizationId: organizationId\r
191 };\r
192 \r
193 users.push(user);\r
194 usersByKey[id] = user;\r
195 });\r
196\r
197 for (var count = me.random(100, 200); count-- > 0; ) {\r
198 projectDate = me.nextDate(projectDate, 0.03);\r
199\r
200 var ticketId = tickets.length + 1,\r
201 date = projectDate,\r
202 modified = Ext.Date.add(date, Ext.Date.MINUTE, me.random(30, 7200)), // 5 days\r
203 creatorId = me.random(firstUserId, users.length),\r
204 assigneeId = me.random(firstUserId, users.length);\r
205\r
206 \r
207 tickets.push({\r
208 id: ticketId,\r
209 title: me.sentence(5, 15),\r
210 description: me.essay(),\r
211 projectId: project.id,\r
212 creatorId: creatorId,\r
213 creator: Ext.apply({}, usersByKey[creatorId]),\r
214 assigneeId: assigneeId,\r
215 assignee: Ext.apply({}, usersByKey[assigneeId]),\r
216 created: Ext.Date.format(date, dateFormat),\r
217 modified: Ext.Date.format(modified, dateFormat),\r
218 status: me.random(1, 3)\r
219 });\r
220\r
221 for (var n = me.random(0, 3); n-- > 0; ) {\r
222 date = me.nextDate(date);\r
223 \r
224 var userId = me.random(firstUserId, users.length);\r
225\r
226 comments.push({\r
227 id: comments.length + 1,\r
228 text: me.paragraph(),\r
229 ticketId: ticketId,\r
230 userId: userId,\r
231 user: Ext.apply({}, usersByKey[userId]),\r
232 created: Ext.Date.format(date, dateFormat)\r
233 });\r
234 }\r
235 }\r
236 });\r
237 \r
238 Ext.Array.forEach(users, function(user) {\r
239 var all = groupsByUserId[user.id] = [],\r
240 totalGroups = groups.length,\r
241 numGroups = me.random(1, 3),\r
242 group;\r
243 \r
244 while (all.length < numGroups) {\r
245 group = groups[me.random(0, totalGroups - 1)];\r
246 if (Ext.Array.indexOf(all, group) === -1) {\r
247 all.push(group);\r
248 (usersByGroupId[group.id] || (usersByGroupId[group.id] = [])).push(user);\r
249 }\r
250 }\r
251 });\r
252 });\r
253 function makeSim (data) {\r
254 return {\r
255 type: 'entity',\r
256 data: data\r
257 };\r
258 }\r
259 function makeMatrixFilter (members) {\r
260 var map = Ext.Array.toMap(members, 'id');\r
261 return function (r) {\r
262 return r.id in map;\r
263 }\r
264 }\r
265\r
266 Ext.ux.ajax.SimManager.init().\r
267 register({\r
268 '/organization': makeSim(organizations),\r
269\r
270 '/group': Ext.apply({\r
271 processFilters: function(filters) {\r
272 // User/Groups is a Many-to-many so Group does not have a field\r
273 // to get groups by userId so we have to look in our internal\r
274 // structure to provide this.\r
275 Ext.each(filters, function (filter, index) {\r
276 if (filter.property === 'userId') {\r
277 filters[index] = makeMatrixFilter(groupsByUserId[filter.value]);\r
278 }\r
279 });\r
280\r
281 return this.self.prototype.processFilters.call(this, filters);\r
282 }\r
283 }, makeSim(groups)),\r
284\r
285 '/project': makeSim(projects),\r
286\r
287 '/comment': makeSim(comments),\r
288\r
289 '/ticket': Ext.apply({\r
290 processData: function(data) {\r
291 data.modified = Ext.Date.format(new Date(), dateFormat);\r
292 return data;\r
293 },\r
294 \r
295 processFilters: function(filters) {\r
296 var status = Ext.Array.findBy(filters, function(filter) {\r
297 return filter.property === 'status';\r
298 });\r
299 \r
300 var assignee = Ext.Array.findBy(filters, function(filter) {\r
301 return filter.property === 'assigneeId';\r
302 });\r
303 \r
304 if (status && status.value === -1) {\r
305 Ext.Array.remove(filters, status);\r
306 }\r
307 \r
308 if (assignee) {\r
309 assignee.exactMatch = true;\r
310 }\r
311 \r
312 return filters;\r
313 }\r
314 }, makeSim(tickets)),\r
315\r
316 '/ticketStatusSummary': {\r
317 type: 'json',\r
318 data: function(ctx) {\r
319 var project = Ext.decode(ctx.params.filter)[0].value,\r
320 data = [],\r
321 totals = {};\r
322 \r
323 Ext.Array.forEach(tickets, function(ticket) {\r
324 var status;\r
325 if (ticket.projectId === project) {\r
326 status = ticket.status;\r
327 if (!totals.hasOwnProperty(status)) {\r
328 totals[status] = 0;\r
329 }\r
330 totals[status] += 1;\r
331 }\r
332 });\r
333 Ext.Object.each(totals, function(key, value) {\r
334 data.push({\r
335 status: parseInt(key, 10),\r
336 total: value\r
337 });\r
338 });\r
339 // Prevent the filters from running in the sim\r
340 ctx.params.filter = null;\r
341 return data;\r
342 }\r
343 },\r
344\r
345 '/ticketOpenSummary': {\r
346 type: 'json',\r
347 data: function(ctx) {\r
348 var project = Ext.decode(ctx.params.filter)[0].value,\r
349 eDate = Ext.Date,\r
350 now = eDate.clearTime(new Date(), true),\r
351 min = eDate.subtract(now, eDate.MONTH, 1),\r
352 data = [],\r
353 totals = {};\r
354 \r
355 Ext.Array.forEach(tickets, function(ticket) {\r
356 var created, key;\r
357 \r
358 if (ticket.projectId === project) {\r
359 created = Ext.Date.parse(ticket.created, 'c');\r
360 if (created >= min) {\r
361 key = Ext.Date.format(created, 'Y-m-d');\r
362 if (!totals.hasOwnProperty(key)) {\r
363 totals[key] = 0;\r
364 }\r
365 totals[key] += 1;\r
366 }\r
367 }\r
368 });\r
369 Ext.Object.each(totals, function(key, value) {\r
370 data.push({\r
371 id: project + key,\r
372 date: key,\r
373 total: value\r
374 });\r
375 });\r
376 // Prevent the filters from running in the sim\r
377 ctx.params.filter = null;\r
378 return data;\r
379 }\r
380 },\r
381\r
382 '/user': Ext.apply({\r
383 processFilters: function(filters) {\r
384 // User/Groups is a Many-to-many so User does not have a field\r
385 // to get users by groupId so we have to look in our internal\r
386 // structure to provide this.\r
387 Ext.each(filters, function (filter, index) {\r
388 if (filter.property === 'groupId') {\r
389 filters[index] = makeMatrixFilter(usersByGroupId[filter.value]);\r
390 }\r
391 });\r
392\r
393 return this.self.prototype.processFilters.call(this, filters);\r
394 }\r
395 }, makeSim(users)),\r
396\r
397 '/authenticate': {\r
398 type: 'json',\r
399 data: function(ctx) {\r
400 var userName = ctx.params.username,\r
401 user = Ext.Array.findBy(users, function(item) {\r
402 return item.name === userName;\r
403 }) || users[0];\r
404 \r
405 return Ext.apply({}, user);\r
406 }\r
407 }\r
408 });\r
409 }\r
410});\r