Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: sitescripts/reports/static/reports.js

Issue 8625042: Reports - user usefullness (Closed)
Patch Set: Reports - user usefulness Created Oct. 23, 2012, 2:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 if (typeof localStorage == "undefined") 1 if (typeof localStorage == "undefined")
2 window.localStorage = {}; 2 window.localStorage = {};
3 var templates; 3 var templates;
4 4
5 function migrateCookies() 5 function migrateCookies()
6 { 6 {
7 var cookies = document.cookie.split(/\s*;\s*/); 7 var cookies = document.cookie.split(/\s*;\s*/);
8 for (var i = 0; i < cookies.length; i++) 8 for (var i = 0; i < cookies.length; i++)
9 { 9 {
10 if (/^(.*?)=(.*)/.test(cookies[i])) 10 if (/^(.*?)=(.*)/.test(cookies[i]))
(...skipping 11 matching lines...) Expand all
22 document.cookie = encodeURIComponent(key) + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT"; 22 document.cookie = encodeURIComponent(key) + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
23 } 23 }
24 } 24 }
25 } 25 }
26 26
27 function escapeHTML(value) 27 function escapeHTML(value)
28 { 28 {
29 return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;" ).replace(/"/g, "&quot;"); 29 return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;" ).replace(/"/g, "&quot;");
30 } 30 }
31 31
32 function setRadio(radio, value)
33 {
34 for (var i = 0; i < radio.length; i++)
35 {
36 if (radio[i].value == value)
37 radio[i].checked = true;
38 else
39 radio[i].checked = false;
40 }
41 }
42
43 function setRadioListener(radio, listener)
44 {
45 for (var i = 0; i < radio.length; i++)
46 radio[i].addEventListener("click", listener, false);
47 }
48
49 function getCheckedRadio(radio)
50 {
51 for (var i = 0; i < radio.length; i++)
52 {
53 if (radio[i].checked)
54 return radio[i];
55 }
56 return undefined;
57 }
58
32 function saveSecret(guid, secret) 59 function saveSecret(guid, secret)
33 { 60 {
34 var secrets = localStorage.secrets; 61 var secrets = localStorage.secrets;
35 if (secrets) 62 if (secrets)
36 secrets = JSON.parse(secrets); 63 secrets = JSON.parse(secrets);
37 else 64 else
38 secrets = {}; 65 secrets = {};
39 secrets[guid] = {value: secret, expiration: new Date().getTime() + 1000*60*60* 24*30}; 66 secrets[guid] = {value: secret, expiration: new Date().getTime() + 1000*60*60* 24*30};
40 localStorage.secrets = JSON.stringify(secrets); 67 localStorage.secrets = JSON.stringify(secrets);
41 } 68 }
42 69
43 function setSecret(guid, secret) 70 function setSecret(guid, secret)
44 { 71 {
45 saveSecret(guid, secret); 72 saveSecret(guid, secret);
46 73
47 var status = ""; 74 var status = "";
48 var statusCell = document.getElementById("statusCell"); 75 var statusCell = document.getElementById("statusCell");
49 if (statusCell) 76 if (statusCell)
50 status = statusCell.textContent; 77 status = statusCell.textContent;
51 78
52 var div = document.createElement("div"); 79 var div = document.createElement("div");
53 div.className = "updateLink" 80 div.className = "updateLink"
54 var link = document.createElement("a"); 81 var link = document.createElement("a");
55 link.setAttribute("href", "javascript:void(0);"); 82 link.setAttribute("href", "javascript:void(0);");
56 link.textContent = "Update status" 83 link.textContent = "Update status"
57 div.appendChild(link); 84 div.appendChild(link);
58 85
59 if (statusCell) 86 if (statusCell)
60 statusCell.appendChild(div); 87 statusCell.appendChild(div);
61 else 88 else
62 document.body.insertBefore(div, document.body.firstChild); 89 document.body.insertBefore(div, document.body.firstChild);
63 90
64 link.onclick = function() 91 link.onclick = function()
65 { 92 {
66 var notifyBox = ""; 93 var notifyBox = "";
67 if (document.getElementById("emailCell")) 94 if (document.getElementById("emailCell"))
68 notifyBox = '<span id="notifyField"><input type="checkbox" id="notify" nam e="notify" value="1" /> <label for="notify">Notify user</label></span>'; 95 notifyBox = '<span id="notifyField"><input type="checkbox" id="notify" nam e="notify" value="1" /> <label for="notify">Notify user</label></span>';
69 96
70 div.innerHTML = '<form action="/updateReport" method="POST">' + 97 div.innerHTML = '<form id="updateForm" action="/updateReport" method="POST"> ' +
71 '<input type="hidden" name="secret" value="' + escapeHTML(secret) + '" />' + 98 '<input type="hidden" name="secret" value="' + escapeHTML(secret) + '" />' +
72 '<input type="hidden" name="guid" value="' + escapeHTML(guid) + '" />' + 99 '<input type="hidden" name="guid" value="' + escapeHTML(guid) + '" />' +
73 '<p>' + 100 '<p>' +
74 'Status templates: <br />' + 101 'Status templates: <br />' +
75 '<select id="templatesField"><option value="" selected="selected" disabl ed="true">(select one)</option></select>' + 102 '<select id="templatesField"><option value="" selected="selected" disabl ed="true">(select one)</option></select>' +
76 '</p>' + 103 '</p>' +
77 '<p>' + 104 '<p>' +
78 'Enter new status:' + notifyBox + '<br />' + 105 'Enter new status:' + notifyBox + '<br />' +
79 '<textarea id="statusField" name="status" oninput="updateTemplateButtons ();"></textarea>' + 106 '<textarea id="statusField" name="status" oninput="updateTemplateButtons ();"></textarea>' +
107 '<br />Usefulness: <input type="radio" name="usefulness" value="0" check ed="checked" /> not established <input type="radio" name="usefulness" value="1" /> useful <input type="radio" name="usefulness" value="-1" /> useless' +
80 '</p>' + 108 '</p>' +
81 '<div>' + 109 '<div>' +
82 '<button id="addTemplateButton" type="button" onclick="addTemplate();">A dd as template</button>' + 110 '<button id="addTemplateButton" type="button" onclick="addTemplate();">A dd as template</button>' +
83 '<button id="removeTemplateButton" type="button" onclick="removeTemplate ();">Remove template</button>' + 111 '<button id="removeTemplateButton" type="button" onclick="removeTemplate ();">Remove template</button>' +
84 '<input type="submit" value="Change status"/>' + 112 '<input type="submit" value="Change status"/>' +
85 '</div>' + 113 '</div>' +
86 '</form>'; 114 '</form>';
115 var radios = document.getElementById("updateForm").elements.usefulness;
87 document.getElementById("templatesField").addEventListener("change", functio n() 116 document.getElementById("templatesField").addEventListener("change", functio n()
88 { 117 {
89 if (this.selectedIndex > 0) 118 if (this.selectedIndex > 0)
90 { 119 {
91 document.getElementById("statusField").value = this.options[this.selecte dIndex].value; 120 » var template = this.options[this.selectedIndex].value.split("\1");
121 var displayText = template[template.length - 1];
122 if (template.length > 1)
123 setRadio(radios, template[0]);
124 document.getElementById("statusField").value = displayText;
92 var notifyField = document.getElementById("notify"); 125 var notifyField = document.getElementById("notify");
93 if (notifyField) 126 if (notifyField)
94 notifyField.checked = true; 127 notifyField.checked = true;
95 updateTemplateButtons(); 128 updateTemplateButtons();
96 } 129 }
97 }, false); 130 }, false);
131
98 var statusField = document.getElementById("statusField"); 132 var statusField = document.getElementById("statusField");
99 statusField.value = status; 133 statusField.value = status;
134
135 var usefulness = document.getElementById("usefulnessCell").getAttribute("val ue");
136 setRadio(radios, usefulness);
137 setRadioListener(radios, updateTemplates);
138
100 updateTemplates(); 139 updateTemplates();
101 statusField.focus(); 140 statusField.focus();
102 } 141 }
103 } 142 }
104 143
105 function updateTemplates() 144 function updateTemplates()
106 { 145 {
107 var templatesField = document.getElementById("templatesField"); 146 var templatesField = document.getElementById("templatesField");
108 while (templatesField.options.length > 1) 147 while (templatesField.options.length > 1)
109 templatesField.remove(1); 148 templatesField.remove(1);
110 for (var i = 0; i < templates.length; i++) 149 for (var i = 0; i < templates.length; i++)
111 { 150 {
112 var displayText = templates[i]; 151 var template = templates[i].split("\1");
152 var displayText = template[template.length - 1];
113 if (displayText.length > 150) 153 if (displayText.length > 150)
114 displayText = displayText.substr(0, 75) + "..." + displayText.substr(displ ayText.length - 75, displayText.length); 154 displayText = displayText.substr(0, 75) + "..." + displayText.substr(displ ayText.length - 75, displayText.length);
155 if (template.length > 1)
156 {
157 usefulnessText = template[0] > 0 ? "useful" : template[0] < 0 ? "useless" : null;
158 if (usefulnessText != null)
159 displayText = "[" + usefulnessText + "] " + displayText;
160 }
115 templatesField.add(new Option(displayText, templates[i], false, false), null ); 161 templatesField.add(new Option(displayText, templates[i], false, false), null );
116 } 162 }
117 updateTemplateButtons(); 163 updateTemplateButtons();
118 } 164 }
119 165
120 function updateTemplateButtons() 166 function updateTemplateButtons()
121 { 167 {
122 var currentText = document.getElementById("statusField").value; 168 var currentText = document.getElementById("statusField").value;
169 var usefulnessValue = "0";
170 var usefulnessButton = getCheckedRadio(document.getElementById("updateForm").e lements.usefulness);
171 if (usefulnessButton)
172 usefulnessValue = usefulnessButton.value;
173
123 var options = document.getElementById("templatesField").options; 174 var options = document.getElementById("templatesField").options;
124 for (var i = 1; i < options.length; i++) 175 for (var i = 1; i < options.length; i++)
125 { 176 {
126 if (options[i].value == currentText) 177 var template = options[i].value.split("\1");
178 var text = template[template.length - 1];
179 var usefulness = template.length > 1 ? template[0] : "0";
180
181 if (text == currentText && usefulness == usefulnessValue)
127 { 182 {
128 document.getElementById("templatesField").selectedIndex = i; 183 document.getElementById("templatesField").selectedIndex = i;
129 document.getElementById("addTemplateButton").style.display = "none"; 184 document.getElementById("addTemplateButton").style.display = "none";
130 document.getElementById("removeTemplateButton").style.display = ""; 185 document.getElementById("removeTemplateButton").style.display = "";
131 return; 186 return;
132 } 187 }
133 } 188 }
134 189
135 document.getElementById("templatesField").selectedIndex = 0; 190 document.getElementById("templatesField").selectedIndex = 0;
136 document.getElementById("addTemplateButton").disabled = !/\S/.test(currentText ) || currentText == "unknown"; 191 document.getElementById("addTemplateButton").disabled = !/\S/.test(currentText ) || currentText == "unknown";
137 document.getElementById("addTemplateButton").style.display = ""; 192 document.getElementById("addTemplateButton").style.display = "";
138 document.getElementById("removeTemplateButton").style.display = "none"; 193 document.getElementById("removeTemplateButton").style.display = "none";
139 } 194 }
140 195
141 function addTemplate() 196 function addTemplate()
142 { 197 {
143 templates.push(document.getElementById("statusField").value); 198 var usefulnessValue = "0";
199 var usefulnessButton = getCheckedRadio(document.getElementById("updateForm").e lements.usefulness);
200 if (usefulnessButton)
201 usefulnessValue = usefulnessButton.value;
202
203 var value = usefulnessValue + "\1" + document.getElementById("statusField").v alue;
204 templates.push(value);
144 templates.sort(); 205 templates.sort();
145 localStorage.templates = templates.join("\0"); 206 localStorage.templates = templates.join("\0");
146 updateTemplates(); 207 updateTemplates();
147 } 208 }
148 209
149 function removeTemplate() 210 function removeTemplate()
150 { 211 {
151 var currentText = document.getElementById("statusField").value; 212 var currentText = document.getElementById("statusField").value;
213 var usefulnessValue = "0";
214 var usefulnessButton = getCheckedRadio(document.getElementById("updateForm").e lements.usefulness);
215 if (usefulnessButton)
216 usefulnessValue = usefulnessButton.value;
152 for (var i = 0; i < templates.length; i++) 217 for (var i = 0; i < templates.length; i++)
153 if (templates[i] == currentText) 218 {
219 var template = templates[i].split("\1");
220 var text = template[template.length - 1];
221 var usefulness = template.length > 1 ? template[0] : "0";
222 if (text == currentText && usefulness == usefulnessValue)
154 templates.splice(i--, 1); 223 templates.splice(i--, 1);
224 }
155 localStorage.templates = templates.join("\0"); 225 localStorage.templates = templates.join("\0");
156 updateTemplates(); 226 updateTemplates();
157 } 227 }
158 228
159 function selectTab(tab) 229 function selectTab(tab)
160 { 230 {
161 document.documentElement.setAttribute("selectedTab", tab); 231 document.documentElement.setAttribute("selectedTab", tab);
162 window.location.replace(window.location.href.replace(/#.*|$/, "#tab=" + tab)); 232 window.location.replace(window.location.href.replace(/#.*|$/, "#tab=" + tab));
163 } 233 }
164 234
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 var spans = nodes[j].getElementsByTagName("span"); 387 var spans = nodes[j].getElementsByTagName("span");
318 for (var k = 0; k < spans.length; k++) 388 for (var k = 0; k < spans.length; k++)
319 if (spans[k].hasAttribute("title")) 389 if (spans[k].hasAttribute("title"))
320 spans[k].setAttribute("title", toLocalDate(spans[k].getAttribute("titl e"))); 390 spans[k].setAttribute("title", toLocalDate(spans[k].getAttribute("titl e")));
321 } 391 }
322 } 392 }
323 } 393 }
324 394
325 window.addEventListener("DOMContentLoaded", initTables, false); 395 window.addEventListener("DOMContentLoaded", initTables, false);
326 window.addEventListener("DOMContentLoaded", changeDates, false); 396 window.addEventListener("DOMContentLoaded", changeDates, false);
OLDNEW

Powered by Google App Engine
This is Rietveld