OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
3 Use of this source code is governed by a BSD-style license that can be | |
4 found in the LICENSE file. | |
5 */ | |
6 | |
7 /** | |
8 * @fileoverview Common methods for performance-plotting Javascript. | |
alias of yukishiino
2013/01/31 10:57:11
I'd recommend a different file name for this file.
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Because it's a fork of dashboard/ui/js/common.js,
| |
9 */ | |
10 | |
11 /** | |
12 * Fetches a URL asynchronously and invokes a callback when complete. | |
13 * | |
14 * @param {string} url URL to fetch. | |
15 * @param {Function(string, string)} callback The function to invoke when the | |
alias of yukishiino
2013/01/31 10:57:11
{Function(string, ?string)}
since |error| can be n
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Done.
| |
16 * results of the URL fetch are complete. The function should accept two | |
17 * strings representing the URL data, and any errors, respectively. | |
18 */ | |
19 function Fetch(url, callback) { | |
20 var r = new XMLHttpRequest(); | |
21 r.open('GET', url, true); | |
22 r.setRequestHeader('pragma', 'no-cache'); | |
23 r.setRequestHeader('cache-control', 'no-cache'); | |
24 | |
25 r.onreadystatechange = function() { | |
26 if (r.readyState == 4) { | |
27 var text = r.responseText; | |
28 var error; | |
alias of yukishiino
2013/01/31 10:57:11
Better to initialize |error| with null, otherwise
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Done.
| |
29 if (r.status != 200) | |
30 error = url + ': ' + r.status + ': ' + r.statusText; | |
31 else if (!text) | |
32 error = url + ': null response'; | |
33 callback(text, error); | |
34 } | |
35 } | |
36 | |
37 r.send(null); | |
38 } | |
39 | |
40 /** | |
41 * Parses the parameters of the current page's URL. | |
42 * | |
43 * @return {Object} An object with properties given by the parameters specified | |
alias of yukishiino
2013/01/31 10:57:11
{Object.<string, (string|number)>}
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Done.
| |
44 * in the URL's query string. | |
45 */ | |
46 function ParseParams() { | |
47 var result = new Object(); | |
alias of yukishiino
2013/01/31 10:57:11
var result = {};
is the same in effect, and more o
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Done.
| |
48 | |
49 var query = window.location.search.substring(1) | |
alias of yukishiino
2013/01/31 10:57:11
missing semi-colon.
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Done.
| |
50 if (query.charAt(query.length - 1) == '/') | |
51 query = query.substring(0, query.length - 1) // Strip trailing slash. | |
alias of yukishiino
2013/01/31 10:57:11
if (query.slice(-1) == '/') {
query = query.slic
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Done.
| |
52 var s = query.split('&'); | |
53 | |
54 for (i = 0; i < s.length; ++i) { | |
55 var v = s[i].split('='); | |
alias of yukishiino
2013/01/31 10:57:11
This code does not handle '=' in the value correct
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Hmm, I thought to fix it, but stopped it not to ha
| |
56 var key = v[0]; | |
57 var value = unescape(v[1]); | |
alias of yukishiino
2013/01/31 10:57:11
escape/unescape are obsolete.
Use encodeURICompone
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Stopped to fix it in the same reason above.
| |
58 result[key] = value; | |
59 } | |
60 | |
61 if ('history' in result) { | |
62 result['history'] = parseInt(result['history']); | |
63 result['history'] = Math.max(result['history'], 2); | |
64 } | |
65 if ('rev' in result) { | |
66 result['rev'] = parseInt(result['rev']); | |
67 result['rev'] = Math.max(result['rev'], -1); | |
alias of yukishiino
2013/01/31 10:57:11
Why -1? Can rev be negative?
By the way, if 'rev
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
I'm not sure. What do you think, Dennis?
dennis_jeffrey
2013/02/05 02:48:28
I don't think "rev" should ever be negative from t
Dai Mikurube (NOT FULLTIME)
2013/02/05 06:31:49
Removed the statement.
| |
68 } | |
69 | |
70 return result; | |
71 } | |
72 | |
73 /** | |
74 * Creates the URL constructed from the current pathname and the given params. | |
75 * | |
76 * @param {Object} An object containing parameters for a URL query string. | |
alias of yukishiino
2013/01/31 10:57:11
{Object.<string, (string|number)>}
alias of yukishiino
2013/01/31 10:57:11
Missing parameter name.
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Done.
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
Done.
| |
77 * @return {string} The URL constructed from the given params. | |
78 */ | |
79 function MakeURL(params) { | |
80 var url = window.location.pathname; | |
81 var sep = '?'; | |
82 for (p in params) { | |
83 if (!p) | |
alias of yukishiino
2013/01/31 10:57:11
Shouldn't reach here.
For "for (p in params)", p m
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
We may want to remove it. What do you think, Denn
dennis_jeffrey
2013/02/05 02:48:28
Yes, if it's true that p will never be null or und
Dai Mikurube (NOT FULLTIME)
2013/02/05 06:31:49
Applied the change below.
| |
84 continue; | |
85 url += sep + p + '=' + params[p]; | |
alias of yukishiino
2013/01/31 10:57:11
You'd better use encodeURIComponent() for values.
Dai Mikurube (NOT FULLTIME)
2013/02/01 06:14:13
What do you think, Dennis?
dennis_jeffrey
2013/02/05 02:48:28
I think this sounds good, as long as we make sure
Dai Mikurube (NOT FULLTIME)
2013/02/05 06:31:49
Replaced with the code by Yuki.
| |
86 sep = '&'; | |
87 } | |
88 return url; | |
89 } | |
OLD | NEW |