|
| 1 | +<link rel="import" href="../../polymer/polymer.html"> |
| 2 | +<link rel="import" href="mostly-resource.html"> |
| 3 | + |
| 4 | +<!-- |
| 5 | +`mostly-search` allows managing saved searches on a Nuxeo server. |
| 6 | +
|
| 7 | + <mostly-search></mostly-search> |
| 8 | +
|
| 9 | +Example - Get all saved searches accessible by the current user: |
| 10 | +
|
| 11 | + <mostly-search auto searches="{{searches}}"></mostly-search> |
| 12 | +
|
| 13 | +Example - Get a saved search by id: |
| 14 | +
|
| 15 | + <mostly-search auto search-id="[[id]]" search="{{search}}"></mostly-search> |
| 16 | +
|
| 17 | +Example - Execute a saved search: |
| 18 | +
|
| 19 | + <mostly-search auto search-id="[[id]]" results="{{results}}"></mostly-search> |
| 20 | +
|
| 21 | +You can trigger a method explicitly by calling `get`, `post`, `put`, `remove` or `execute on the |
| 22 | +element. |
| 23 | +
|
| 24 | +@group Mostly Elements |
| 25 | +@element mostly-search |
| 26 | +--> |
| 27 | + |
| 28 | +<dom-module id="mostly-search"> |
| 29 | + <template> |
| 30 | + <style> |
| 31 | + :host { |
| 32 | + display: none; |
| 33 | + } |
| 34 | + </style> |
| 35 | + |
| 36 | + <mostly-resource id="resource" connection-id="[[connectionId]]" method="[[method]]" |
| 37 | + path="[[path]]" data="[[data]]" headers="[[headers]]" params="[[params]]"> |
| 38 | + </mostly-resource> |
| 39 | + |
| 40 | + </template> |
| 41 | + <script> |
| 42 | + Polymer({ |
| 43 | + is: 'mostly-search', |
| 44 | + |
| 45 | + properties: { |
| 46 | + |
| 47 | + /** |
| 48 | + * Id of a saved search to be used on requests. |
| 49 | + */ |
| 50 | + searchId: { |
| 51 | + type: String, |
| 52 | + value: '' |
| 53 | + }, |
| 54 | + |
| 55 | + /** |
| 56 | + * Body to be used on POST/PUT requests. |
| 57 | + */ |
| 58 | + data: Object, |
| 59 | + |
| 60 | + /** |
| 61 | + * A single saved search as a result of a GET request with a given id. |
| 62 | + */ |
| 63 | + search: { |
| 64 | + type: Object, |
| 65 | + notify: true |
| 66 | + }, |
| 67 | + |
| 68 | + /** |
| 69 | + * Saved searches accessible by the current user. |
| 70 | + * Result of a GET request when an id is not specified. |
| 71 | + */ |
| 72 | + searches: { |
| 73 | + type: Array, |
| 74 | + notify: true |
| 75 | + }, |
| 76 | + |
| 77 | + /** |
| 78 | + * Results when executing a given saved search. |
| 79 | + */ |
| 80 | + results: { |
| 81 | + type: Array, |
| 82 | + notify: true |
| 83 | + }, |
| 84 | + |
| 85 | + /** |
| 86 | + * Computed path of the search endpoint to use. |
| 87 | + */ |
| 88 | + path: { |
| 89 | + type: String, |
| 90 | + computed: '_computePath(auto, searchId)' |
| 91 | + }, |
| 92 | + |
| 93 | + /** |
| 94 | + * Search parameters. |
| 95 | + */ |
| 96 | + params: { |
| 97 | + type: Object |
| 98 | + }, |
| 99 | + |
| 100 | + /** |
| 101 | + * The id of a mostly-connection to use. |
| 102 | + */ |
| 103 | + connectionId: String, |
| 104 | + |
| 105 | + /** |
| 106 | + * If true, automatically execute the search request. |
| 107 | + */ |
| 108 | + auto: { |
| 109 | + type: Boolean, |
| 110 | + value: false, |
| 111 | + observer: '_autoChanged' |
| 112 | + }, |
| 113 | + |
| 114 | + /** |
| 115 | + * The HTTP method to use ('GET', 'POST', 'PUT', or 'DELETE'). Default is 'GET' |
| 116 | + */ |
| 117 | + method: { |
| 118 | + type: String, |
| 119 | + value: 'get' |
| 120 | + }, |
| 121 | + |
| 122 | + /** |
| 123 | + * The headers of the request. |
| 124 | + */ |
| 125 | + headers: Object, |
| 126 | + |
| 127 | + /** |
| 128 | + * The delay in milliseconds to debounce the auto fetch call. |
| 129 | + */ |
| 130 | + autoDelay: { |
| 131 | + type: Number, |
| 132 | + value: 300 |
| 133 | + }, |
| 134 | + |
| 135 | + /** |
| 136 | + * True while requests are in flight. |
| 137 | + */ |
| 138 | + loading: { |
| 139 | + type: Boolean, |
| 140 | + notify: true, |
| 141 | + readOnly: true |
| 142 | + } |
| 143 | + }, |
| 144 | + |
| 145 | + /** |
| 146 | + * Gets the list of saved searches or the current search search if `search-id` is set. |
| 147 | + */ |
| 148 | + get: function() { |
| 149 | + this.method = 'get'; |
| 150 | + return this.$.resource.execute().then((response) => { |
| 151 | + if (this.searchId) { |
| 152 | + this.search = response; |
| 153 | + } else { |
| 154 | + this.searches = response.data; |
| 155 | + } |
| 156 | + return response; |
| 157 | + }); |
| 158 | + }, |
| 159 | + |
| 160 | + /** |
| 161 | + * Saves the search. |
| 162 | + */ |
| 163 | + post: function() { |
| 164 | + this.method = 'post'; |
| 165 | + return this.$.resource.execute(); |
| 166 | + }, |
| 167 | + |
| 168 | + /** |
| 169 | + * Updates the saved search. |
| 170 | + */ |
| 171 | + put: function() { |
| 172 | + this.method = 'put'; |
| 173 | + return this.$.resource.execute(); |
| 174 | + }, |
| 175 | + |
| 176 | + /** |
| 177 | + * Deletes the saved search. |
| 178 | + */ |
| 179 | + remove: function() { |
| 180 | + this.method = 'delete'; |
| 181 | + return this.$.resource.execute(); |
| 182 | + }, |
| 183 | + |
| 184 | + /** |
| 185 | + * Executes the saved search. |
| 186 | + */ |
| 187 | + execute: function() { |
| 188 | + if (this.searchId) { |
| 189 | + this.method = 'get'; |
| 190 | + this.$.resource.path += '/execute'; |
| 191 | + this.$.resource.execute().then((response) => { |
| 192 | + this.results = response.data; |
| 193 | + }); |
| 194 | + } |
| 195 | + }, |
| 196 | + |
| 197 | + _computePath: function() { |
| 198 | + var path = '/search/saved'; |
| 199 | + if (this.searchId) { |
| 200 | + path += '/' + this.searchId; |
| 201 | + } |
| 202 | + return path; |
| 203 | + }, |
| 204 | + |
| 205 | + _autoChanged: function() { |
| 206 | + if (this.auto && this.path) { |
| 207 | + this.debounce('_auto', () => { |
| 208 | + this.get().then(() => { |
| 209 | + this.execute(); |
| 210 | + }); |
| 211 | + }, this.autoDelay); |
| 212 | + } |
| 213 | + }, |
| 214 | + |
| 215 | + ready: function() { |
| 216 | + this.$.resource.addEventListener('loading-changed', () => { |
| 217 | + this._setLoading(this.$.resource.loading); |
| 218 | + }); |
| 219 | + } |
| 220 | + }); |
| 221 | + </script> |
| 222 | +</dom-module> |
0 commit comments