1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-09-29 23:40:45 +00:00

Merge branch 'tm-http-request-message' into geospatial-plugin

This commit is contained in:
jeremy@jermolene.com 2023-05-02 17:19:23 +01:00
commit b8b29aa035
13 changed files with 225 additions and 72 deletions

View File

@ -0,0 +1,11 @@
title: $:/core/images/network-activity
tags: $:/tags/Image
<svg width="22pt" height="22pt" class="tc-image-network-activity tc-image-button" viewBox="0 0 128 128"><g class={{{ [{$:/state/http-requests}match[0]then[]else[tc-network-activity-background]] }}}>
<$list filter="[{$:/state/http-requests}match[0]]" variable="ignore">
<path d="M64.043 45.153a4.002 4.002 0 0 1 4.367 2.21l.084.188 30.403 73.4a4 4 0 0 1-7.307 3.25l-.084-.188-3.103-7.49-8.898 8.899a3.985 3.985 0 0 1-2.624 1.166l-.205.005a3.987 3.987 0 0 1-2.828-1.171l-9.849-9.848-9.847 9.848a3.985 3.985 0 0 1-2.624 1.166l-.204.005a3.987 3.987 0 0 1-2.829-1.171l-8.899-8.9-3.102 7.491a4 4 0 1 1-7.391-3.062l30.403-73.4a4.001 4.001 0 0 1 4.495-2.39l.042-.008Zm13.636 56.74-8.023 8.024 7.02 7.019 8.023-8.022-7.02-7.02Zm-27.353.008-7.019 7.019 8.016 8.016 7.019-7.02-8.016-8.015Zm13.68-13.68-8.023 8.023 8.016 8.016 8.023-8.023-8.016-8.016Zm-8.971-8.971-4.687 11.315 8.001-8.001-3.314-3.314Zm17.933.009-3.305 3.305 7.979 7.979-4.674-11.284ZM64 57.607l-5.666 13.68c.096.072.188.15.278.232l.133.126 5.261 5.262 5.262-5.262c.128-.127.261-.244.4-.35L64 57.607Zm0-34.69a8 8 0 1 1 0 16 8 8 0 0 1 0-16Z"/>
</$list>
<$list filter="[{$:/state/http-requests}!match[0]]" variable="ignore">
<path d="M109.395.952a4.002 4.002 0 0 1 3.787 2.708C117.529 11.62 120 20.753 120 30.462c0 15.186-6.044 28.96-15.858 39.047a4 4 0 1 1-6.47-4.626l-.12-.094C106.466 56.074 112 43.914 112 30.462c0-8.492-2.205-16.469-6.074-23.39l.054-.036a4 4 0 0 1 3.415-6.084Zm-90.762 0a4 4 0 0 1 3.072 6.562l.093.06A47.786 47.786 0 0 0 16 30.463c0 13.315 5.42 25.363 14.176 34.058l-.01.007a4 4 0 1 1-6.312 4.863l-.063.05C14.017 59.359 8 45.613 8 30.462c0-9.77 2.502-18.956 6.9-26.952A4.002 4.002 0 0 1 18.634.952Z"/><path d="M64.043 44.698a4.002 4.002 0 0 1 4.367 2.21l.084.188 30.403 73.4a4 4 0 0 1-7.307 3.25l-.084-.188-3.103-7.49-8.898 8.9a3.985 3.985 0 0 1-2.624 1.166l-.205.005a3.987 3.987 0 0 1-2.828-1.172l-9.849-9.848-9.847 9.848a3.985 3.985 0 0 1-2.624 1.167l-.204.005a3.987 3.987 0 0 1-2.829-1.172l-8.899-8.899-3.102 7.49a4 4 0 0 1-7.391-3.061l30.403-73.4a4.001 4.001 0 0 1 4.495-2.39l.042-.009ZM77.68 101.44l-8.023 8.023 7.02 7.019 8.023-8.022-7.02-7.02Zm-27.353.007-7.019 7.019 8.016 8.016 7.019-7.019-8.016-8.016Zm13.68-13.68-8.023 8.023 8.016 8.016 8.023-8.023-8.016-8.016Zm-8.971-8.971L50.348 90.11l8.001-8.001-3.314-3.314Zm17.933.009-3.305 3.305 7.979 7.979-4.674-11.284ZM64 57.152l-5.666 13.68c.096.073.188.15.278.232l.133.127 5.261 5.261 5.262-5.261c.128-.128.261-.244.4-.351L64 57.152ZM38.503 1.058a4 4 0 0 1 2.7 6.952l.17-.175C35.582 13.625 32 21.625 32 30.462c0 8.838 3.582 16.838 9.374 22.629a4 4 0 0 1-5.659 5.658l-.01.01C28.473 51.52 24 41.526 24 30.485 24 19.567 28.374 9.67 35.466 2.453a3.995 3.995 0 0 1 3.037-1.395ZM89.369.952c1.14 0 2.17.478 2.899 1.244l.005-.006C99.518 9.43 104 19.434 104 30.485c0 10.826-4.3 20.648-11.287 27.85a4 4 0 1 1-6.054-5.213l-.032-.032C92.418 47.299 96 39.299 96 30.462c0-8.73-3.496-16.643-9.164-22.416A4 4 0 0 1 89.368.952Zm-39.282 11.14a4 4 0 0 1 2.59 7.048l.01.009A15.95 15.95 0 0 0 48 30.462a15.95 15.95 0 0 0 4.687 11.315l-.01.01a4 4 0 1 1-5.82 5.47l.173.177A23.925 23.925 0 0 1 40 30.462a23.925 23.925 0 0 1 7.03-16.97l.01.01a3.991 3.991 0 0 1 3.047-1.41Zm27.895.07a3.99 3.99 0 0 1 2.984 1.336l.006-.005A23.925 23.925 0 0 1 88 30.463a23.92 23.92 0 0 1-6.707 16.642l-.3.305a4 4 0 1 1-5.679-5.632v-.002A15.95 15.95 0 0 0 80 30.462a15.95 15.95 0 0 0-4.685-11.312 4.012 4.012 0 0 1-1.333-2.987 4 4 0 0 1 4-4ZM64 22.463a8 8 0 1 1 0 16 8 8 0 0 1 0-16Z"/>
</$list>
</g></svg>

View File

@ -67,6 +67,8 @@ More/Caption: more
More/Hint: More actions
NewHere/Caption: new here
NewHere/Hint: Create a new tiddler tagged with this one
NetworkActivity/Caption: network activity
NetworkActivity/Hint: Cancel all network activity
NewJournal/Caption: new journal
NewJournal/Hint: Create a new journal tiddler
NewJournalHere/Caption: new journal here

View File

@ -39,9 +39,10 @@ Command.prototype.execute = function() {
$tw.utils.each(tiddlers,function(title) {
var parser = wiki.parseTiddler(templatetitle),
newFields = {},
tiddler = wiki.getTiddler(title);
tiddler = wiki.getTiddler(title),
currentValue = tiddler ? (tiddler.fields[fieldname] || "") : "";
if(parser) {
var widgetNode = wiki.makeWidget(parser,{variables: {currentTiddler: title}});
var widgetNode = wiki.makeWidget(parser,{variables: {currentTiddler: title, currentValue: currentValue}});
var container = $tw.fakeDocument.createElement("div");
widgetNode.render(container,null);
newFields[fieldname] = rendertype === "text/html" ? container.innerHTML : container.textContent;

View File

@ -22,8 +22,34 @@ exports.synchronous = true;
exports.startup = function() {
// Install the HTTP client event handler
$tw.httpClient = new $tw.utils.HttpClient();
var getPropertiesWithPrefix = function(properties,prefix) {
var result = Object.create(null);
$tw.utils.each(properties,function(value,name) {
if(name.indexOf(prefix) === 0) {
result[name.substring(prefix.length)] = properties[name];
}
});
return result;
};
$tw.rootWidget.addEventListener("tm-http-request",function(event) {
$tw.httpClient.handleHttpRequest(event);
var params = event.paramObject || {};
$tw.httpClient.initiateHttpRequest({
wiki: event.widget.wiki,
url: params.url,
method: params.method,
oncompletion: params.oncompletion,
onprogress: params.onprogress,
bindStatus: params["bind-status"],
bindProgress: params["bind-progress"],
variables: getPropertiesWithPrefix(params,"var-"),
headers: getPropertiesWithPrefix(params,"header-"),
passwordHeaders: getPropertiesWithPrefix(params,"password-header-"),
queryStrings: getPropertiesWithPrefix(params,"query-"),
passwordQueryStrings: getPropertiesWithPrefix(params,"password-query-")
});
});
$tw.rootWidget.addEventListener("tm-http-cancel-all-requests",function(event) {
$tw.httpClient.cancelAllHttpRequests();
});
// Install the modal message mechanism
$tw.modal = new $tw.utils.Modal($tw.wiki);

View File

@ -13,81 +13,149 @@ HTTP support
"use strict";
/*
Manage tm-http-request events. Options are:
wiki - the wiki object to use
Manage tm-http-request events. Options include:
wiki: Reference to the wiki to be used for state tiddler tracking
stateTrackerTitle: Title of tiddler to be used for state tiddler tracking
*/
function HttpClient(options) {
options = options || {};
this.nextId = 1;
this.wiki = options.wiki || $tw.wiki;
this.stateTrackerTitle = options.stateTrackerTitle || "$:/state/http-requests";
this.requests = []; // Array of {id: string,request: HttpClientRequest}
this.updateRequestTracker();
}
HttpClient.prototype.handleHttpRequest = function(event) {
console.log("Making an HTTP request",event)
/*
Return the index into this.requests[] corresponding to a given ID. Returns null if not found
*/
HttpClient.prototype.getRequestIndex = function(targetId) {
var targetIndex = null;
$tw.utils.each(this.requests,function(requestInfo,index) {
if(requestInfo.id === targetId) {
targetIndex = index;
}
});
return targetIndex;
};
/*
Update the state tiddler that is tracking the outstanding requests
*/
HttpClient.prototype.updateRequestTracker = function() {
this.wiki.addTiddler({title: this.stateTrackerTitle, text: "" + this.requests.length});
};
HttpClient.prototype.initiateHttpRequest = function(options) {
var self = this,
id = this.nextId,
request = new HttpClientRequest(options);
this.nextId += 1;
this.requests.push({id: id, request: request});
this.updateRequestTracker();
request.send(function(err) {
var targetIndex = self.getRequestIndex(id);
if(targetIndex !== null) {
self.requests.splice(targetIndex,1);
self.updateRequestTracker();
}
});
return id;
};
HttpClient.prototype.cancelAllHttpRequests = function() {
var self = this;
$tw.utils.each(this.requests,function(requestInfo,index) {
requestInfo.request.cancel();
});
this.requests = [];
this.updateRequestTracker();
};
HttpClient.prototype.cancelHttpRequest = function(targetId) {
var targetIndex = this.getRequestIndex(targetId);
if(targetIndex !== null) {
this.requests[targetIndex].request.cancel();
this.requests.splice(targetIndex,1);
this.updateRequestTracker();
}
};
/*
Initiate an HTTP request. Options:
wiki: wiki to be used for executing action strings
url: URL for request
method: method eg GET, POST
body: text of request body
oncompletion: action string to be invoked on completion
onprogress: action string to be invoked on progress updates
bindStatus: optional title of tiddler to which status ("pending", "complete", "error") should be written
bindProgress: optional title of tiddler to which the progress of the request (0 to 100) should be bound
variables: hashmap of variable name to string value passed to action strings
headers: hashmap of header name to header value to be sent with the request
passwordHeaders: hashmap of header name to password store name to be sent with the request
queryStrings: hashmap of query string parameter name to parameter value to be sent with the request
passwordQueryStrings: hashmap of query string parameter name to password store name to be sent with the request
*/
function HttpClientRequest(options) {
var self = this;
console.log("Initiating an HTTP request",options)
this.wiki = options.wiki;
this.completionActions = options.oncompletion;
this.progressActions = options.onprogress;
this.bindStatus = options["bind-status"];
this.bindProgress = options["bind-progress"];
this.method = options.method || "GET";
this.body = options.body || "";
this.variables = options.variables;
var url = options.url;
$tw.utils.each(options.queryStrings,function(value,name) {
url = $tw.utils.setQueryStringParameter(url,name,value);
});
$tw.utils.each(options.passwordQueryStrings,function(value,name) {
url = $tw.utils.setQueryStringParameter(url,name,$tw.utils.getPassword(value) || "");
});
this.url = url;
this.requestHeaders = {};
$tw.utils.each(options.headers,function(value,name) {
self.requestHeaders[name] = value;
});
$tw.utils.each(options.passwordHeaders,function(value,name) {
self.requestHeaders[name] = $tw.utils.getPassword(value) || "";
});
}
HttpClientRequest.prototype.send = function(callback) {
var self = this,
wiki = event.widget.wiki,
paramObject = event.paramObject || {},
url = paramObject.url,
completionActions = paramObject.oncompletion || "",
progressActions = paramObject.onprogress || "",
bindStatus = paramObject["bind-status"],
bindProgress = paramObject["bind-progress"],
method = paramObject.method || "GET",
HEADER_PARAMETER_PREFIX = "header-",
QUERY_PARAMETER_PREFIX = "query-",
PASSWORD_HEADER_PARAMETER_PREFIX = "password-header-",
PASSWORD_QUERY_PARAMETER_PREFIX = "password-query-",
CONTEXT_VARIABLE_PARAMETER_PREFIX = "var-",
requestHeaders = {},
contextVariables = {},
setBinding = function(title,text) {
if(title) {
wiki.addTiddler(new $tw.Tiddler({title: title, text: text}));
this.wiki.addTiddler(new $tw.Tiddler({title: title, text: text}));
}
};
if(url) {
setBinding(bindStatus,"pending");
setBinding(bindProgress,"0");
$tw.utils.each(paramObject,function(value,name) {
// Look for query- parameters
if(name.substr(0,QUERY_PARAMETER_PREFIX.length) === QUERY_PARAMETER_PREFIX) {
url = $tw.utils.setQueryStringParameter(url,name.substr(QUERY_PARAMETER_PREFIX.length),value);
}
// Look for header- parameters
if(name.substr(0,HEADER_PARAMETER_PREFIX.length) === HEADER_PARAMETER_PREFIX) {
requestHeaders[name.substr(HEADER_PARAMETER_PREFIX.length)] = value;
}
// Look for password-header- parameters
if(name.substr(0,PASSWORD_QUERY_PARAMETER_PREFIX.length) === PASSWORD_QUERY_PARAMETER_PREFIX) {
url = $tw.utils.setQueryStringParameter(url,name.substr(PASSWORD_QUERY_PARAMETER_PREFIX.length),$tw.utils.getPassword(value) || "");
}
// Look for password-query- parameters
if(name.substr(0,PASSWORD_HEADER_PARAMETER_PREFIX.length) === PASSWORD_HEADER_PARAMETER_PREFIX) {
requestHeaders[name.substr(PASSWORD_HEADER_PARAMETER_PREFIX.length)] = $tw.utils.getPassword(value) || "";
}
// Look for var- parameters
if(name.substr(0,CONTEXT_VARIABLE_PARAMETER_PREFIX.length) === CONTEXT_VARIABLE_PARAMETER_PREFIX) {
contextVariables[name.substr(CONTEXT_VARIABLE_PARAMETER_PREFIX.length)] = value;
}
});
if(this.url) {
setBinding(this.bindStatus,"pending");
setBinding(this.bindProgress,"0");
// Set the request tracker tiddler
var requestTrackerTitle = wiki.generateNewTitle("$:/temp/HttpRequest");
wiki.addTiddler({
var requestTrackerTitle = this.wiki.generateNewTitle("$:/temp/HttpRequest");
this.wiki.addTiddler({
title: requestTrackerTitle,
tags: "$:/tags/HttpRequest",
text: JSON.stringify({
url: url,
type: method,
url: this.url,
type: this.method,
status: "inprogress",
headers: requestHeaders,
data: paramObject.body
headers: this.requestHeaders,
data: this.body
})
});
$tw.utils.httpRequest({
url: url,
type: method,
headers: requestHeaders,
data: paramObject.body,
this.xhr = $tw.utils.httpRequest({
url: this.url,
type: this.method,
headers: this.requestHeaders,
data: this.body,
callback: function(err,data,xhr) {
var success = (xhr.status >= 200 && xhr.status < 300) ? "complete" : "error",
var hasSucceeded = xhr.status >= 200 && xhr.status < 300,
completionCode = hasSucceeded ? "complete" : "error",
headers = {};
$tw.utils.each(xhr.getAllResponseHeaders().split("\r\n"),function(line) {
var pos = line.indexOf(":");
@ -95,27 +163,27 @@ HttpClient.prototype.handleHttpRequest = function(event) {
headers[line.substr(0,pos)] = line.substr(pos + 1).trim();
}
});
setBinding(bindStatus,success);
setBinding(bindProgress,"100");
var results = {
setBinding(self.bindStatus,completionCode);
setBinding(self.bindProgress,"100");
var resultVariables = {
status: xhr.status.toString(),
statusText: xhr.statusText,
error: (err || "").toString(),
data: (data || "").toString(),
headers: JSON.stringify(headers)
};
// Update the request tracker tiddler
wiki.addTiddler(new $tw.Tiddler(wiki.getTiddler(requestTrackerTitle),{
status: success,
self.wiki.addTiddler(new $tw.Tiddler(self.wiki.getTiddler(requestTrackerTitle),{
status: completionCode,
}));
wiki.invokeActionString(completionActions,undefined,$tw.utils.extend({},contextVariables,results),{parentWidget: $tw.rootWidget});
self.wiki.invokeActionString(self.completionActions,undefined,$tw.utils.extend({},self.variables,resultVariables),{parentWidget: $tw.rootWidget});
callback(hasSucceeded ? null : xhr.statusText);
// console.log("Back!",err,data,xhr);
},
progress: function(lengthComputable,loaded,total) {
if(lengthComputable) {
setBinding(bindProgress,"" + Math.floor((loaded/total) * 100))
}
wiki.invokeActionString(progressActions,undefined,{
self.wiki.invokeActionString(self.progressActions,undefined,{
lengthComputable: lengthComputable ? "yes" : "no",
loaded: loaded,
total: total
@ -125,6 +193,12 @@ HttpClient.prototype.handleHttpRequest = function(event) {
}
};
HttpClientRequest.prototype.cancel = function() {
if(this.xhr) {
this.xhr.abort();
}
};
exports.HttpClient = HttpClient;
/*

View File

@ -54,6 +54,7 @@ modal-footer-background: #f5f5f5
modal-footer-border: #dddddd
modal-header-border: #eeeeee
muted-foreground: #bbb
network-activity-foreground: #448844
notification-background: #ffffdd
notification-border: #999999
page-background: #f4f4f4

View File

@ -0,0 +1,16 @@
title: $:/core/ui/Buttons/network-activity
tags: $:/tags/PageControls
caption: {{$:/core/images/network-activity}} {{$:/language/Buttons/NetworkActivity/Caption}}
description: {{$:/language/Buttons/NetworkActivity/Hint}}
\whitespace trim
<$button message="tm-http-cancel-all-requests" tooltip={{$:/language/Buttons/NetworkActivity/Hint}} aria-label={{$:/language/Buttons/NetworkActivity/Caption}} class=<<tv-config-toolbar-class>>>
<$list filter="[<tv-config-toolbar-icons>match[yes]]">
{{$:/core/images/network-activity}}
</$list>
<$list filter="[<tv-config-toolbar-text>match[yes]]">
<span class="tc-btn-text">
<$text text={{$:/language/Buttons/NetworkActivity/Caption}}/>
</span>
</$list>
</$button>

View File

@ -13,6 +13,7 @@ core/ui/Buttons/language: hide
core/ui/Buttons/tag-manager: hide
core/ui/Buttons/manager: hide
core/ui/Buttons/more-page-actions: hide
core/ui/Buttons/network-activity: hide
core/ui/Buttons/new-journal: hide
core/ui/Buttons/new-image: hide
core/ui/Buttons/palette: hide

View File

@ -1,2 +1,2 @@
title: $:/tags/PageControls
list: [[$:/core/ui/Buttons/home]] [[$:/core/ui/Buttons/close-all]] [[$:/core/ui/Buttons/fold-all]] [[$:/core/ui/Buttons/unfold-all]] [[$:/core/ui/Buttons/permaview]] [[$:/core/ui/Buttons/new-tiddler]] [[$:/core/ui/Buttons/new-journal]] [[$:/core/ui/Buttons/new-image]] [[$:/core/ui/Buttons/import]] [[$:/core/ui/Buttons/export-page]] [[$:/core/ui/Buttons/control-panel]] [[$:/core/ui/Buttons/advanced-search]] [[$:/core/ui/Buttons/manager]] [[$:/core/ui/Buttons/tag-manager]] [[$:/core/ui/Buttons/language]] [[$:/core/ui/Buttons/palette]] [[$:/core/ui/Buttons/theme]] [[$:/core/ui/Buttons/layout]] [[$:/core/ui/Buttons/storyview]] [[$:/core/ui/Buttons/encryption]] [[$:/core/ui/Buttons/timestamp]] [[$:/core/ui/Buttons/full-screen]] [[$:/core/ui/Buttons/print]] [[$:/core/ui/Buttons/save-wiki]] [[$:/core/ui/Buttons/refresh]] [[$:/core/ui/Buttons/more-page-actions]]
list: [[$:/core/ui/Buttons/home]] [[$:/core/ui/Buttons/close-all]] [[$:/core/ui/Buttons/fold-all]] [[$:/core/ui/Buttons/unfold-all]] [[$:/core/ui/Buttons/permaview]] [[$:/core/ui/Buttons/new-tiddler]] [[$:/core/ui/Buttons/new-journal]] [[$:/core/ui/Buttons/new-image]] [[$:/core/ui/Buttons/import]] [[$:/core/ui/Buttons/export-page]] [[$:/core/ui/Buttons/control-panel]] [[$:/core/ui/Buttons/advanced-search]] [[$:/core/ui/Buttons/manager]] [[$:/core/ui/Buttons/tag-manager]] [[$:/core/ui/Buttons/language]] [[$:/core/ui/Buttons/palette]] [[$:/core/ui/Buttons/theme]] [[$:/core/ui/Buttons/layout]] [[$:/core/ui/Buttons/storyview]] [[$:/core/ui/Buttons/encryption]] [[$:/core/ui/Buttons/timestamp]] [[$:/core/ui/Buttons/full-screen]] [[$:/core/ui/Buttons/print]] [[$:/core/ui/Buttons/save-wiki]] [[$:/core/ui/Buttons/refresh]] [[$:/core/ui/Buttons/network-activity]] [[$:/core/ui/Buttons/more-page-actions]]

View File

@ -0,0 +1,12 @@
caption: tm-http-cancel-all-requests
created: 20230429161453032
modified: 20230429161453032
tags: Messages
title: WidgetMessage: tm-http-cancel-all-requests
type: text/vnd.tiddlywiki
The ''tm-http-cancel-all-requests'' message is used to cancel all outstanding HTTP requests initiated with [[WidgetMessage: tm-http-request]].
Note that the state tiddler $:/state/http-requests contains a number representing the number of outstanding HTTP requests in progress.
It does not take any parameters.

View File

@ -81,13 +81,16 @@ https://api.zotero.org/groups/{{$:/config/zotero-group}}/items/
<$macrocall $name="zotero-get-items" start="0" limit="50"/>
\end
<<select-zotero-group>>
<$button actions=<<zotero-actions>>>
Start import from Zotero group
</$button>
<$button message="tm-http-cancel-all-requests">
Cancel all HTTP requests
</$button> Outstanding requests: {{$:/state/http-requests}}
<$list filter="[tag[$:/tags/ZoteroImport]limit[1]]" variable="ignore">
!! Imported Tiddlers

View File

@ -1,6 +1,6 @@
caption: tm-http-request
created: 20220908161746341
modified: 20220908161746341
created: 20230429161453032
modified: 20230429161453032
tags: Messages
title: WidgetMessage: tm-http-request
type: text/vnd.tiddlywiki
@ -44,6 +44,8 @@ The following variables are passed to the progress handler:
|loaded |Number of bytes loaded so far |
|total |Total number bytes to be loaded |
Note that the state tiddler $:/state/http-requests contains a number representing the number of outstanding HTTP requests in progress.
!! Examples
* [[Zotero's|https://www.zotero.org/]] API for retrieving reference items: [[WidgetMessage: tm-http-request Example - Zotero]]

View File

@ -3156,6 +3156,10 @@ select {
fill: <<colour background>>;
}
.tc-network-activity-background {
fill: <<colour network-activity-foreground>>;
}
/*
** Test Cases
*/