| 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 |
| 37 load(url, headers, pageSize): Initialize the element and make a call to |
| 38 the server for the first batch of data. |
| 39 |
| 40 page(): Must be called after a call to load(). Fetch the next batch of |
| 41 data from the server. |
| 42 |
| 43 Events: |
| 44 None. |
| 45 --> |
| 46 |
| 47 <link rel="import" href="/res/imp/common/swarming-app.html"> |
| 48 |
| 49 <dom-module id="pageable-data"> |
| 50 <template> |
| 51 <style include="swarming-app-style"> |
| 52 </style> |
| 53 |
| 54 <button |
| 55 on-tap="page" |
| 56 disabled$="[[_noMore(_cursor)]]"> |
| 57 [[label]] |
| 58 </button> |
| 59 |
| 60 </template> |
| 61 <script> |
| 62 (function(){ |
| 63 var END = "END"; |
| 64 Polymer({ |
| 65 is: 'pageable-data', |
| 66 |
| 67 properties: { |
| 68 // input |
| 69 label: { |
| 70 type: String, |
| 71 value: "Show More", |
| 72 }, |
| 73 parse: { |
| 74 type: Function, |
| 75 value: function(){ |
| 76 return function(a){ |
| 77 return a; |
| 78 }; |
| 79 }, |
| 80 }, |
| 81 |
| 82 // output |
| 83 busy: { |
| 84 type: Boolean, |
| 85 value: false, |
| 86 notify: true, |
| 87 }, |
| 88 output: { |
| 89 type: Array, |
| 90 notify: true, |
| 91 }, |
| 92 |
| 93 _cursor: { |
| 94 type: String, |
| 95 value: null, |
| 96 }, |
| 97 _url: { |
| 98 type: String, |
| 99 }, |
| 100 _headers: { |
| 101 type: Object, |
| 102 }, |
| 103 _page_size: { |
| 104 type: Number, |
| 105 } |
| 106 }, |
| 107 |
| 108 clear: function() { |
| 109 this.set("output", []); |
| 110 this._cursor = null; |
| 111 }, |
| 112 |
| 113 load: function(url, headers, pageSize) { |
| 114 if (!url) { |
| 115 throw "url can't be blank"; |
| 116 } |
| 117 if (url.indexOf("?") === -1) { |
| 118 url += "?"; |
| 119 } |
| 120 this._url = url; |
| 121 this._headers = headers; |
| 122 this._page_size = pageSize || 50; |
| 123 this.page(); |
| 124 }, |
| 125 |
| 126 page: function() { |
| 127 if (this.busy || this._cursor === END) { |
| 128 // ignore pages while we are loading or are at the end of the data. |
| 129 return; |
| 130 } |
| 131 if (!this._url) { |
| 132 throw "You must first call load() before calling page()"; |
| 133 } |
| 134 this.set("busy", true); |
| 135 |
| 136 var url = this._url + "&limit=" + this._page_size; |
| 137 if (this._cursor) { |
| 138 url += "&cursor=" + this._cursor; |
| 139 } |
| 140 |
| 141 sk.request("GET", url, "", this._headers).then(JSON.parse).then(function
(json){ |
| 142 var vals = this.parse(json); |
| 143 if (!this.output) { |
| 144 this.set("output", vals); |
| 145 } else { |
| 146 this.push("output", ...vals); |
| 147 } |
| 148 this.set("_cursor", json.cursor || END); |
| 149 this.set("busy", false); |
| 150 }.bind(this)).catch(function(reason){ |
| 151 console.log("Reason for failure of request to " + this._url, reason); |
| 152 sk.errorMessage("Could not get next set of results. Try reloading the
page?", 0); |
| 153 this.set("busy", false); |
| 154 }.bind(this)); |
| 155 }, |
| 156 |
| 157 _noMore: function(cursor) { |
| 158 return cursor === END; |
| 159 } |
| 160 }); |
| 161 })(); |
| 162 </script> |
| 163 </dom-module> |
| OLD | NEW |