added sandbox for testing plugins + random record generator
[myslice.git] / manifold / static / js / record_generator.js
1 /* Buffered DOM updates */
2 var RecordGenerator = Class.extend({
3
4     init: function(query, generators, number)
5     {
6         this._query      = query;
7         this._generators = generators;
8         this._number     = number;
9     },
10
11     random_int: function(options)
12     {
13         var default_options = {
14             max: 1000
15         }
16
17         if (typeof options == 'object')
18             options = $.extend(default_options, options);
19         else
20             options = default_options;
21
22         return Math.floor(Math.random()*(options.max+1));
23     },
24
25     random_string: function()
26     {
27         var default_options = {
28             possible: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
29             len:      this.random_int({max: 15})
30         }
31
32         if (typeof options == 'object')
33             options = $.extend(default_options, options);
34         else
35             options = default_options;
36
37         var text = "";
38
39         for( var i=0; i < options.len; i++ )
40             text += options.possible.charAt(Math.floor(Math.random() * options.possible.length));
41
42         return text;
43
44     },
45
46     generate_record: function()
47     {
48         var self = this;
49         var record = {};
50
51         $.each(this._query.fields, function(i, field) {
52             record[field] = self[self._generators[field]]();
53         });
54
55         // Publish records
56         manifold.raise_record_event(self._query.query_uuid, NEW_RECORD, record);
57         
58     },
59
60     run: function()
61     {
62         var record;
63         manifold.raise_record_event(this._query.query_uuid, CLEAR_RECORDS);
64         for (var i = 0; i < this._number; i++) {
65             record = this.generate_record();
66             /* XXX publish record */
67         }
68         manifold.raise_record_event(this._query.query_uuid, DONE);
69
70     }
71 });