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. 29, 2012, 12:35 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
« no previous file with comments | « sitescripts/reports/static/lib/reportsview.js ('k') | sitescripts/reports/template/digest.html » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 = 0;
136 var usefulnessCell = document.getElementById("usefulnessCell");
137 if (usefulnessCell)
138 usefulness = usefulnessCell.getAttribute("value");
139 setRadio(radios, usefulness);
140 setRadioListener(radios, updateTemplates);
141
100 updateTemplates(); 142 updateTemplates();
101 statusField.focus(); 143 statusField.focus();
102 } 144 }
103 } 145 }
104 146
105 function updateTemplates() 147 function updateTemplates()
106 { 148 {
107 var templatesField = document.getElementById("templatesField"); 149 var templatesField = document.getElementById("templatesField");
108 while (templatesField.options.length > 1) 150 while (templatesField.options.length > 1)
109 templatesField.remove(1); 151 templatesField.remove(1);
110 for (var i = 0; i < templates.length; i++) 152 for (var i = 0; i < templates.length; i++)
111 { 153 {
112 var displayText = templates[i]; 154 var template = templates[i].split("\1");
155 var displayText = template[template.length - 1];
113 if (displayText.length > 150) 156 if (displayText.length > 150)
114 displayText = displayText.substr(0, 75) + "..." + displayText.substr(displ ayText.length - 75, displayText.length); 157 displayText = displayText.substr(0, 75) + "..." + displayText.substr(displ ayText.length - 75, displayText.length);
158 if (template.length > 1)
159 {
160 usefulnessText = template[0] > 0 ? "useful" : template[0] < 0 ? "useless" : null;
161 if (usefulnessText != null)
162 displayText = "[" + usefulnessText + "] " + displayText;
163 }
115 templatesField.add(new Option(displayText, templates[i], false, false), null ); 164 templatesField.add(new Option(displayText, templates[i], false, false), null );
116 } 165 }
117 updateTemplateButtons(); 166 updateTemplateButtons();
118 } 167 }
119 168
120 function updateTemplateButtons() 169 function updateTemplateButtons()
121 { 170 {
122 var currentText = document.getElementById("statusField").value; 171 var currentText = document.getElementById("statusField").value;
172 var usefulnessValue = "0";
173 var usefulnessButton = getCheckedRadio(document.getElementById("updateForm").e lements.usefulness);
174 if (usefulnessButton)
175 usefulnessValue = usefulnessButton.value;
176
123 var options = document.getElementById("templatesField").options; 177 var options = document.getElementById("templatesField").options;
124 for (var i = 1; i < options.length; i++) 178 for (var i = 1; i < options.length; i++)
125 { 179 {
126 if (options[i].value == currentText) 180 var template = options[i].value.split("\1");
181 var text = template[template.length - 1];
182 var usefulness = template.length > 1 ? template[0] : "0";
183
184 if (text == currentText && usefulness == usefulnessValue)
127 { 185 {
128 document.getElementById("templatesField").selectedIndex = i; 186 document.getElementById("templatesField").selectedIndex = i;
129 document.getElementById("addTemplateButton").style.display = "none"; 187 document.getElementById("addTemplateButton").style.display = "none";
130 document.getElementById("removeTemplateButton").style.display = ""; 188 document.getElementById("removeTemplateButton").style.display = "";
131 return; 189 return;
132 } 190 }
133 } 191 }
134 192
135 document.getElementById("templatesField").selectedIndex = 0; 193 document.getElementById("templatesField").selectedIndex = 0;
136 document.getElementById("addTemplateButton").disabled = !/\S/.test(currentText ) || currentText == "unknown"; 194 document.getElementById("addTemplateButton").disabled = !/\S/.test(currentText ) || currentText == "unknown";
137 document.getElementById("addTemplateButton").style.display = ""; 195 document.getElementById("addTemplateButton").style.display = "";
138 document.getElementById("removeTemplateButton").style.display = "none"; 196 document.getElementById("removeTemplateButton").style.display = "none";
139 } 197 }
140 198
141 function addTemplate() 199 function addTemplate()
142 { 200 {
143 templates.push(document.getElementById("statusField").value); 201 var usefulnessValue = "0";
202 var usefulnessButton = getCheckedRadio(document.getElementById("updateForm").e lements.usefulness);
203 if (usefulnessButton)
204 usefulnessValue = usefulnessButton.value;
205
206 var value = usefulnessValue + "\1" + document.getElementById("statusField").v alue;
207 templates.push(value);
144 templates.sort(); 208 templates.sort();
145 localStorage.templates = templates.join("\0"); 209 localStorage.templates = templates.join("\0");
146 updateTemplates(); 210 updateTemplates();
147 } 211 }
148 212
149 function removeTemplate() 213 function removeTemplate()
150 { 214 {
151 var currentText = document.getElementById("statusField").value; 215 var currentText = document.getElementById("statusField").value;
216 var usefulnessValue = "0";
217 var usefulnessButton = getCheckedRadio(document.getElementById("updateForm").e lements.usefulness);
218 if (usefulnessButton)
219 usefulnessValue = usefulnessButton.value;
152 for (var i = 0; i < templates.length; i++) 220 for (var i = 0; i < templates.length; i++)
153 if (templates[i] == currentText) 221 {
222 var template = templates[i].split("\1");
223 var text = template[template.length - 1];
224 var usefulness = template.length > 1 ? template[0] : "0";
225 if (text == currentText && usefulness == usefulnessValue)
154 templates.splice(i--, 1); 226 templates.splice(i--, 1);
227 }
155 localStorage.templates = templates.join("\0"); 228 localStorage.templates = templates.join("\0");
156 updateTemplates(); 229 updateTemplates();
157 } 230 }
158 231
159 function selectTab(tab) 232 function selectTab(tab)
160 { 233 {
161 document.documentElement.setAttribute("selectedTab", tab); 234 document.documentElement.setAttribute("selectedTab", tab);
162 window.location.replace(window.location.href.replace(/#.*|$/, "#tab=" + tab)); 235 window.location.replace(window.location.href.replace(/#.*|$/, "#tab=" + tab));
163 } 236 }
164 237
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
317 var spans = nodes[j].getElementsByTagName("span"); 390 var spans = nodes[j].getElementsByTagName("span");
318 for (var k = 0; k < spans.length; k++) 391 for (var k = 0; k < spans.length; k++)
319 if (spans[k].hasAttribute("title")) 392 if (spans[k].hasAttribute("title"))
320 spans[k].setAttribute("title", toLocalDate(spans[k].getAttribute("titl e"))); 393 spans[k].setAttribute("title", toLocalDate(spans[k].getAttribute("titl e")));
321 } 394 }
322 } 395 }
323 } 396 }
324 397
325 window.addEventListener("DOMContentLoaded", initTables, false); 398 window.addEventListener("DOMContentLoaded", initTables, false);
326 window.addEventListener("DOMContentLoaded", changeDates, false); 399 window.addEventListener("DOMContentLoaded", changeDates, false);
OLDNEW
« no previous file with comments | « sitescripts/reports/static/lib/reportsview.js ('k') | sitescripts/reports/template/digest.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld