-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
154 lines (129 loc) · 4.85 KB
/
app.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
148
149
150
151
152
153
154
jQuery(document).ready(function ($) {
console.debug("Initializing layout...");
// expand overall height
$('.outerContainer').height($(document).height());
$('.chozen').chosen();
$('.tagged').tag();
// text fields sexy effects
$('.advtext, .taglist > .input > input')
.on('focus', function () {
$(this).parentsUntil('.formLayout', 'div').addClass('chzn-container-active');
})
.on('blur', function () {
$(this).parentsUntil('.formLayout', 'div').removeClass('chzn-container-active');
});
// expansion actions
var expanded = false, firsttime = true;
$('.expander').on('click', function () {
if (expanded = !expanded) {
$('.requestForm').addClass('expanded', 200, function () {
if (firsttime) {
}
});
$('.expander').toggleClass('expanded');
} else {
$('.requestForm').removeClass('expanded', 200);
$('.expander').toggleClass('expanded');
}
});
// fetch available buckets
var gotBuckets = function (buckets) {
$.each(buckets, function (i, bucket) {
$('<option value="' + bucket.toString() + '">' + bucket.toString() + '</option>')
.appendTo('#buckets');
});
$("#buckets").trigger("liszt:updated");
};
var getBuckets = function () {
$.getJSON('/riak?buckets=true', function (json) {
gotBuckets(json.buckets);
});
};
getBuckets();
// dynamic query builder
var QueryBuilder = function (context, previewContext) {
console.debug("Initializing query builder...");
this.client = new RiakClient(
"/riak",
"/mapred"
);
var self = this;
context.on('change', ['input', 'select', 'textarea'], function () {
self.fieldAltered(this);
});
this.fieldAltered = function () {
var values = this.getValues();
var query = this.makeQuery(values);
previewContext.text(JSON.stringify(query.request(), null, 4));
};
this.getValues = function () {
var result = {
inputs: $('#buckets option:selected', context).map(function () {
return this.value;
}).get(),
secondaryIndex: $('#secondaryIndex').attr('value'),
secondaryIndexValue: $('#secondaryIndexValue').attr('value')
};
if (result.inputs.length > 1) {
result.secondaryIndex = null;
result.secondaryIndexValue = null;
} else {
result.inputs = result.inputs[0];
}
return result;
};
this.makeQuery = function (args) {
var mapper = new RiakMapper(
this.client,
args.inputs,
args.secondaryIndex ? args.secondaryIndex : null,
args.secondaryIndex ? args.secondaryIndexValue : null
);
mapper.map(
this.makeMapPhase(args.mapPhase ? args.mapPhase : null)
);
if (args.reducePhase) {
mapper.reduce(
this.makeReducePhase(args.reducePhase)
);
}
return mapper;
};
var defaultMapPhase =
function (value, keyData, arg) { return [ Riak.mapValuesJson(value)[0] ]; };
this.makeMapPhase = function (phase) {
var o = {source: defaultMapPhase};
if (phase) {
o.source = [
"function (value, keyData, arg) { ",
"var data = Riak.mapValuesJson(value)[0]; ",
phase.replace(/\n/, '', 'g'),
" }"
];
o.source = o.source.join('');
}
return o;
};
this.makeReducePhase = function (phase) {
var o = {source: [
"function (values, arg) { ",
phase.replace(/\n/, '', 'g'),
"}"
]};
o.source = o.source.join('');
return o;
};
this.submitQuery = function () {
var values = this.getValues(), query = this.makeQuery(values);
console.debug("Running mapreduce query...");
query.run(null, function (status, response) {
console.log(status, response);
});
};
};
var builder = new QueryBuilder($('.formLayout form'), $('#preview'));
$('#submit').on('click', function () {
builder.submitQuery();
return false;
});
});