Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 <!-- | |
| 2 This in an HTML Import-able file that contains the definition | |
| 3 of the following elements: | |
| 4 | |
| 5 <pageable-data> | |
| 6 | |
| 7 The pageable-data element allows for easy paging of data from the swarming | |
| 8 server. It displays a button to the user to go fetch more data, which will | |
| 9 be appended to the output, rather than replaced, which is typical in | |
| 10 elements like iron-ajax. | |
| 11 | |
| 12 Typical usage is | |
| 13 | |
| 14 this.$.page.clear(); | |
| 15 this.$.page.load("/api/frobulator?alpha=beta", this.auth_headers, 20); | |
| 16 | |
| 17 which initializes the url to query and goes to fetch the first 20 records | |
| 18 from the api. The user can then click the button to get the next page of | |
| 19 data. This can be done programatically with page(). | |
| 20 | |
| 21 Properties: | |
| 22 // input | |
| 23 label: String, what to label the button to page for more data. | |
| 24 parse: Function, This function takes the JSON object from the server and | |
| 25 returns an array of objects. Preprocessing should be done in this | |
| 26 function, if necessary. | |
| 27 | |
| 28 // output | |
| 29 busy: Boolean, if a request is in flight. Calls to page() while busy will | |
| 30 be ignored. | |
| 31 output: Array<Object> the accumulated values from the server. | |
| 32 | |
| 33 Methods: | |
| 34 clear(): Reset the element, emptying output. page() will not work until | |
| 35 a call to load() has been made. | |
| 36 load(url, headers, pageSize): Initialize the element and make a call to | |
|
stephana
2016/09/28 13:25:23
empty line between the method descriptions and pro
kjlubick
2016/09/28 13:50:36
Done.
| |
| 37 the server for the first batch of data. | |
| 38 page(): Must be called after a call to load(). Fetch the next batch of | |
| 39 data from the server. | |
| 40 | |
| 41 Events: | |
| 42 None. | |
| 43 --> | |
| 44 | |
| 45 <link rel="import" href="/res/imp/common/swarming-app.html"> | |
| 46 | |
| 47 <dom-module id="pageable-data"> | |
| 48 <template> | |
| 49 <style include="swarming-app-style"> | |
| 50 </style> | |
| 51 | |
| 52 <button | |
| 53 on-tap="page" | |
| 54 disabled$="[[_noMore(_cursor)]]"> | |
| 55 [[label]] | |
| 56 </button> | |
| 57 | |
| 58 </template> | |
| 59 <script> | |
| 60 (function(){ | |
| 61 var END = "END"; | |
| 62 Polymer({ | |
| 63 is: 'pageable-data', | |
| 64 | |
| 65 properties: { | |
| 66 // input | |
| 67 label: { | |
| 68 type: String, | |
| 69 value: "Show More", | |
| 70 }, | |
| 71 parse: { | |
| 72 type: Function, | |
| 73 value: function(){ | |
| 74 return function(a){ | |
| 75 return a; | |
| 76 }; | |
| 77 }, | |
| 78 }, | |
| 79 | |
| 80 // output | |
| 81 busy: { | |
| 82 type: Boolean, | |
| 83 value: false, | |
| 84 notify: true, | |
| 85 }, | |
| 86 output: { | |
| 87 type: Array, | |
| 88 notify: true, | |
| 89 }, | |
| 90 | |
| 91 _cursor: { | |
| 92 type: String, | |
| 93 }, | |
| 94 _url: { | |
| 95 type: String, | |
| 96 }, | |
| 97 _headers: { | |
| 98 type: Object, | |
| 99 }, | |
| 100 _page_size: { | |
| 101 type: Number, | |
| 102 } | |
| 103 }, | |
| 104 | |
| 105 clear: function() { | |
| 106 this.set("output", []); | |
| 107 this._cursor = undefined; | |
|
stephana
2016/09/28 13:25:23
I would either delete the _cursor member or set it
kjlubick
2016/09/28 13:50:36
Done.
| |
| 108 }, | |
| 109 | |
| 110 load: function(url, headers, pageSize) { | |
| 111 if (!url) { | |
| 112 console.log("url can't be blank"); | |
|
stephana
2016/09/28 13:25:23
Same as below. Assume that the client of this elem
kjlubick
2016/09/28 13:50:37
Done.
| |
| 113 return; | |
| 114 } | |
| 115 if (url.indexOf("?") === -1) { | |
| 116 url += "?"; | |
| 117 } | |
| 118 this._url = url; | |
| 119 this._headers = headers; | |
| 120 this._page_size = pageSize || 50; | |
| 121 this.page(); | |
| 122 }, | |
| 123 | |
| 124 page: function() { | |
| 125 if (this.busy || this._cursor === END) { | |
| 126 // ignore pages while we are loading or are at the end of the data. | |
| 127 return; | |
| 128 } | |
| 129 if (!this._url) { | |
| 130 console.log("You must first call load() before calling page()"); | |
|
stephana
2016/09/28 13:25:23
console.log IMO is mostly for debugging purposes a
kjlubick
2016/09/28 13:50:36
Done. I made it a throw just to be safe.
| |
| 131 return; | |
| 132 } | |
| 133 this.set("busy", true); | |
| 134 | |
| 135 var url = this._url + "&limit=" + this._page_size; | |
| 136 if (this._cursor !== undefined) { | |
|
stephana
2016/09/28 13:25:23
I would make _cursor well defined in the sense, th
kjlubick
2016/09/28 13:50:37
Done.
| |
| 137 url += "&cursor=" + this._cursor; | |
| 138 } | |
| 139 | |
| 140 sk.request("GET", url, "", this._headers).then(JSON.parse).then(function (json){ | |
| 141 var vals = this.parse(json); | |
| 142 if (!this.output) { | |
| 143 this.set("output", vals); | |
| 144 } else { | |
| 145 this.push("output", ...vals); | |
| 146 } | |
| 147 this.set("_cursor", json.cursor || END); | |
| 148 this.set("busy", false); | |
| 149 }.bind(this)).catch(function(reason){ | |
| 150 console.log("Reason for failure of request to " + this._url, reason); | |
| 151 this.set("busy", false); | |
|
stephana
2016/09/28 13:25:23
There should be a notification of failure shown to
kjlubick
2016/09/28 13:50:37
Done.
| |
| 152 }.bind(this)); | |
| 153 }, | |
| 154 | |
| 155 _noMore: function(cursor) { | |
| 156 return cursor === END; | |
| 157 } | |
| 158 }); | |
| 159 })(); | |
| 160 </script> | |
| 161 </dom-module> | |
| OLD | NEW |