Fossil SCM
Add routines for standardized loading of javascript resources.
Commit
01d96c6b45b0aebf3837583fe10fc0d218bbf48b7243c73c93367ee4fbc7f7b8
Parent
956d2f8db953490…
1 file changed
+62
+62
| --- src/builtin.c | ||
| +++ src/builtin.c | ||
| @@ -211,5 +211,67 @@ | ||
| 211 | 211 | etag_check(0,0); |
| 212 | 212 | } |
| 213 | 213 | blob_init(&out, zTxt, -1); |
| 214 | 214 | cgi_set_content(&out); |
| 215 | 215 | } |
| 216 | + | |
| 217 | +/* Variables controlling the JS cache. | |
| 218 | +*/ | |
| 219 | +static struct { | |
| 220 | + int aReq[30]; /* Indexes of all requested built-in JS files */ | |
| 221 | + int nReq; /* Number of slots in aReq[] currently used */ | |
| 222 | + int nSent; /* Number of slots in aReq[] fulfilled */ | |
| 223 | +} builtin; | |
| 224 | + | |
| 225 | +/* | |
| 226 | +** The caller wants the Javascript file named by zFilename to be | |
| 227 | +** included in the generated page. Add the file to the queue of | |
| 228 | +** requested javascript resources, if it is not there already. | |
| 229 | +** | |
| 230 | +** The current implementation queues the file to be included in the | |
| 231 | +** output later. However, the caller should not depend on that | |
| 232 | +** behavior. In the future, this routine might decide to insert | |
| 233 | +** the requested javascript inline, immedaitely, or to insert | |
| 234 | +** a <script src=..> element to reference the javascript as a | |
| 235 | +** separate resource. The exact behavior might change in the future | |
| 236 | +** so pages that use this interface must not rely on any particular | |
| 237 | +** behavior. | |
| 238 | +** | |
| 239 | +** All this routine guarantees is that the named javascript file | |
| 240 | +** will be requested by the browser at some point. This routine | |
| 241 | +** does not guarantee when the javascript will be included, and it | |
| 242 | +** does not guarantee whether the javascript will be added inline or | |
| 243 | +** delivered as a separate resource. | |
| 244 | +*/ | |
| 245 | +void builtin_request_js(const char *zFilename){ | |
| 246 | + int i = builtin_file_index(zFilename); | |
| 247 | + int j; | |
| 248 | + if( i<0 ){ | |
| 249 | + fossil_panic("unknown javascript file: \"%s\"", zFilename); | |
| 250 | + } | |
| 251 | + for(j=0; j<builtin.nReq; j++){ | |
| 252 | + if( builtin.aReq[j]==i ) return; /* Already queued or sent */ | |
| 253 | + } | |
| 254 | + if( builtin.nReq>=count(builtin.aReq) ){ | |
| 255 | + fossil_panic("too many javascript files requested"); | |
| 256 | + } | |
| 257 | + builtin.aReq[builtin.nReq++] = i; | |
| 258 | +} | |
| 259 | + | |
| 260 | +/* | |
| 261 | +** Fulfill all pending requests for javascript files. | |
| 262 | +** | |
| 263 | +** The current implementation delivers all javascript in-line. However, | |
| 264 | +** the caller should not depend on this. Future changes to this routine | |
| 265 | +** might choose to deliver javascript as separate resources. | |
| 266 | +*/ | |
| 267 | +void builtin_fulfill_js_requests(void){ | |
| 268 | + if( builtin.nSent<builtin.nReq ){ | |
| 269 | + CX("<script nonce='%h'>\n",style_nonce); | |
| 270 | + do{ | |
| 271 | + int i = builtin.aReq[builtin.nSent++]; | |
| 272 | + cgi_append_content((const char*)aBuiltinFiles[i].pData, | |
| 273 | + aBuiltinFiles[i].nByte); | |
| 274 | + }while( builtin.nSent<builtin.nReq ); | |
| 275 | + CX("</script>\n"); | |
| 276 | + } | |
| 277 | +} | |
| 216 | 278 |
| --- src/builtin.c | |
| +++ src/builtin.c | |
| @@ -211,5 +211,67 @@ | |
| 211 | etag_check(0,0); |
| 212 | } |
| 213 | blob_init(&out, zTxt, -1); |
| 214 | cgi_set_content(&out); |
| 215 | } |
| 216 |
| --- src/builtin.c | |
| +++ src/builtin.c | |
| @@ -211,5 +211,67 @@ | |
| 211 | etag_check(0,0); |
| 212 | } |
| 213 | blob_init(&out, zTxt, -1); |
| 214 | cgi_set_content(&out); |
| 215 | } |
| 216 | |
| 217 | /* Variables controlling the JS cache. |
| 218 | */ |
| 219 | static struct { |
| 220 | int aReq[30]; /* Indexes of all requested built-in JS files */ |
| 221 | int nReq; /* Number of slots in aReq[] currently used */ |
| 222 | int nSent; /* Number of slots in aReq[] fulfilled */ |
| 223 | } builtin; |
| 224 | |
| 225 | /* |
| 226 | ** The caller wants the Javascript file named by zFilename to be |
| 227 | ** included in the generated page. Add the file to the queue of |
| 228 | ** requested javascript resources, if it is not there already. |
| 229 | ** |
| 230 | ** The current implementation queues the file to be included in the |
| 231 | ** output later. However, the caller should not depend on that |
| 232 | ** behavior. In the future, this routine might decide to insert |
| 233 | ** the requested javascript inline, immedaitely, or to insert |
| 234 | ** a <script src=..> element to reference the javascript as a |
| 235 | ** separate resource. The exact behavior might change in the future |
| 236 | ** so pages that use this interface must not rely on any particular |
| 237 | ** behavior. |
| 238 | ** |
| 239 | ** All this routine guarantees is that the named javascript file |
| 240 | ** will be requested by the browser at some point. This routine |
| 241 | ** does not guarantee when the javascript will be included, and it |
| 242 | ** does not guarantee whether the javascript will be added inline or |
| 243 | ** delivered as a separate resource. |
| 244 | */ |
| 245 | void builtin_request_js(const char *zFilename){ |
| 246 | int i = builtin_file_index(zFilename); |
| 247 | int j; |
| 248 | if( i<0 ){ |
| 249 | fossil_panic("unknown javascript file: \"%s\"", zFilename); |
| 250 | } |
| 251 | for(j=0; j<builtin.nReq; j++){ |
| 252 | if( builtin.aReq[j]==i ) return; /* Already queued or sent */ |
| 253 | } |
| 254 | if( builtin.nReq>=count(builtin.aReq) ){ |
| 255 | fossil_panic("too many javascript files requested"); |
| 256 | } |
| 257 | builtin.aReq[builtin.nReq++] = i; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | ** Fulfill all pending requests for javascript files. |
| 262 | ** |
| 263 | ** The current implementation delivers all javascript in-line. However, |
| 264 | ** the caller should not depend on this. Future changes to this routine |
| 265 | ** might choose to deliver javascript as separate resources. |
| 266 | */ |
| 267 | void builtin_fulfill_js_requests(void){ |
| 268 | if( builtin.nSent<builtin.nReq ){ |
| 269 | CX("<script nonce='%h'>\n",style_nonce); |
| 270 | do{ |
| 271 | int i = builtin.aReq[builtin.nSent++]; |
| 272 | cgi_append_content((const char*)aBuiltinFiles[i].pData, |
| 273 | aBuiltinFiles[i].nByte); |
| 274 | }while( builtin.nSent<builtin.nReq ); |
| 275 | CX("</script>\n"); |
| 276 | } |
| 277 | } |
| 278 |