-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
228 lines (195 loc) · 7.54 KB
/
popup.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var $jq = jQuery.noConflict();
$jq(document).ready(function()
{
var tabUrl;
// leverage for stripping out everything but the domain
function url_domain(data)
{
var a = document.createElement('a');
a.href = data;
return a.hostname;
}
function hasRespondedError(results){
// clear previous results
$jq('#query-results').html('');
if (results == null || results.error)
{
var tableHtml = '<table id="errorTable"><tr>';
tableHtml += '<td colspan="2" align="center" style="font-size:medium">Search Failed.</td></tr><tr>';
tableHtml += '<td> <b> Error </b> <br/>' + results.response.responseJSON[0].errorCode+'</td>';
tableHtml += '<td> <b> Message </b> <br/>' + results.response.responseJSON[0].message+'</td></tr></table>';
$jq('#errors').html(tableHtml);
$jq('#errorTable').css({"font-family": "arial, sans-serif" , "border-collapse": "collapse","color":"white"});
$jq('#errorTable td').css({"border": "1px solid #dddddd", "padding": "8px", "text-align": "left"});
return true;
}
return false;
}
function buildSOQLResults(results)
{
if(hasRespondedError(results) == true) return ;
else
{
$jq('#errors').html('');
// build table header
if (results.queryResults.records.length > 0)
{
// TODO - need to get the pretty labels
var fields = Object.keys(results.queryResults.records[0]);
// get rid of attributes, its not an actual field
fields.splice(0, 1);
// TODO - need to HTML escape the values
var tableHtml = '<table id="soqlTable"><tr>';
fields.forEach(function(field_name)
{
tableHtml += '<th>' + field_name + '</th>';
});
tableHtml += '</tr>';
results.queryResults.records.forEach(function(record)
{
// TODO - need to more smartly show join fields (right now its an object)
tableHtml += '<tr>';
fields.forEach(function(field_name)
{
tableHtml += '<td>';
if (field_name === 'Id' )
{
// TODO - clean this up
// want to keep the "/" in order to build the URL
var record_id = record['attributes']['url'].substring(record['attributes']['url'].lastIndexOf('/'));
tableHtml += '<a href="' + tabUrl + record_id + '" target="_blank" style="color:greenyellow">' + record[field_name] + '</a>';
}
else
{
tableHtml += (record[field_name]!=null)?record[field_name]:"" ;
}
tableHtml + '</td>';
});
tableHtml += '</tr>';
});
tableHtml += '</table>';
$jq('#query-results').html(tableHtml);
$jq('#soqlTable').css({"font-family": "arial, sans-serif" , "border-collapse": "collapse","color":"white"});
$jq('#soqlTable td, th').css({"border": "1px solid #dddddd", "padding": "8px", "text-align": "left"});
}
}
}
function buildObjectFieldTable(results){
if(hasRespondedError(results) == true) return ;
else
{
$jq('#errors').html('');
var allowedCols = [{
"name": "Label",
"val": "label"
},
{
"name": "API Name",
"val": "name"
},
{
"name": "Type",
"val": "type"
}
];
// build table header
if (results.queryResults.fields.length > 0)
{
var fieldKeys = Object.keys(results.queryResults.fields);
var tableHtml = '<table id="fieldTable" style="border:1px solid black;"><tr>';
var headerVal = results.queryResults.fields[0];
for(val in allowedCols){
tableHtml += '<th align="right">' + String(allowedCols[val].name).trim()+ '</th>';
}
tableHtml += '</tr>';
fieldKeys.forEach(function(key)
{
tableHtml += '<tr>';
var item = results.queryResults.fields[key];
for(val in allowedCols){
tableHtml += '<td align="right">' + String(item[allowedCols[val].val]).trim() + '</td>';
}
tableHtml += '</tr>';
});
tableHtml += '</table>';
$jq('#query-results').html(tableHtml);
$jq('#fieldTable').css({"font-family": "arial, sans-serif" , "border-collapse": "collapse","color":"white"});
$jq('#fieldTable td, th').css({"border": "1px solid #dddddd", "padding": "8px", "text-align": "left"});
}
}
}
function queryClient(queryString)
{
chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
{
chrome.tabs.sendMessage(tabs[0].id, {name: $jq('.dropdown').find('input').val(), queryString: queryString}, function(response)
{
if(response === undefined){
setResponseForUndefined(response);
return;
}
if(response.queryString == "forceObjectFields")
buildObjectFieldTable(response);
if(response.queryString == "forcequery")
buildSOQLResults(response);
});
});
};
function setResponseForUndefined(results){
var tableHtml = '<table id="errorTable"><tr>';
tableHtml += '<td colspan="2" align="center" style="font-size:medium">Connection Lost. Please refresh the active chrome tab.</td></tr></table>';
$jq('#errors').html(tableHtml);
$jq('#errorTable').css({"font-family": "arial, sans-serif" , "border-collapse": "collapse","color":"white"});
$jq('#errorTable td').css({"border": "1px solid #dddddd", "padding": "8px", "text-align": "left"});
}
// build salesforce domain for hyperlinks later
chrome.tabs.getSelected(null, function(tab)
{
tabUrl = 'https://' + url_domain(tab.url);
});
var jsonData = {
data: [{
"name": "SOQL",
"val": "forcequery"
},
{
"name": "Fields of an Object",
"val": "forceObjectFields"
}]
};
$jq.each(jsonData.data, function (key, value) {
//$jq("#dropDownList").append($jq('<option></option>').val(value.val).html(value.name));
$jq("#dropDownList").append($jq('<li id="'+value.val+'"></li>').html(value.name));
});
$jq('#submit-query').click(function(event)
{
$jq('#errors').html('');
var query = $jq('#query').val();
var optionSelected = $jq('.dropdown').find('input').val();
if(query == null || query.trim() == "" || optionSelected == null || optionSelected.trim() == "") {
var tableHtml = '<table id="errorTable"><tr>';
tableHtml += '<td colspan="2" align="center" style="font-size:medium">Please select an option & Enter search data!</td></tr></table>';
$jq('#errors').html(tableHtml);
$jq('#errorTable').css({"font-family": "arial, sans-serif" , "border-collapse": "collapse","color":"white"});
$jq('#errorTable td').css({"border": "1px solid #dddddd", "padding": "8px", "text-align": "left"});
return;
}
queryClient(query);
});
/*Dropdown Menu*/
$jq('.dropdown').click(function () {
$jq('#errors').html('');
$jq(this).attr('tabindex', 1).focus();
$jq(this).toggleClass('active');
$jq(this).find('.dropdown-menu').slideToggle(300);
});
$jq('.dropdown').focusout(function () {
$jq(this).removeClass('active');
$jq(this).find('.dropdown-menu').slideUp(300);
});
$jq('.dropdown .dropdown-menu li').click(function () {
$jq(this).parents('.dropdown').find('span').text($jq(this).text());
$jq(this).parents('.dropdown').find('input').attr('value', $jq(this).attr('id'));
});
/*End Dropdown Menu*/
});