@@ -110,6 +110,8 @@ function createApplicationTable(subject) {
110110
111111function createApplicationEntry ( subject , origin , appModes , updateTable ) {
112112 var trustedApplicationState = { origin, appModes, formElements : { modes : [ ] } }
113+ var profile = subject . doc ( )
114+
113115 return createContainer ( 'tr' , [
114116 createContainer ( 'td' , [
115117 createElement ( 'input' , {
@@ -145,71 +147,55 @@ function createApplicationEntry(subject, origin, appModes, updateTable) {
145147 ] )
146148
147149 function addOrEditApplication ( ) {
148- let origin
150+ var origin
149151 try {
150152 origin = $rdf . sym ( trustedApplicationState . formElements . origin . value )
151153 } catch ( err ) {
152154 return alert ( 'Please provide an application URL you want to trust' )
153155 }
154156
155- // remove existing statements on same origin - if it exists
156- kb . statementsMatching ( null , ns . acl ( 'origin' ) , origin ) . forEach ( st => {
157- kb . removeStatements ( [ ...kb . statementsMatching ( null , ns . acl ( 'trustedApp' ) , st . subject ) ] )
158- kb . removeStatements ( [ ...kb . statementsMatching ( st . subject ) ] )
159- } )
160-
161- // add new triples
162- const application = new $rdf . BlankNode ( )
163- kb . add ( subject , ns . acl ( 'trustedApp' ) , application , subject )
164- kb . add ( application , ns . acl ( 'origin' ) , origin , subject )
165- trustedApplicationState . formElements . modes
166- . filter ( checkbox => checkbox . checked )
167- . map ( checkbox => $rdf . sym ( checkbox . value ) )
168- . forEach ( mode => {
169- kb . add ( application , ns . acl ( 'mode' ) , mode )
170- } )
171-
172- save ( )
157+ var deletions = getStatementsToDelete ( origin )
158+ var additions = getStatementsToAdd ( origin )
159+ kb . updater . update ( deletions , additions , handleUpdateResponse )
173160 }
174161
175162 function removeApplication ( ) {
176- let origin
163+ var origin
177164 try {
178165 origin = $rdf . sym ( trustedApplicationState . formElements . origin . value )
179166 } catch ( err ) {
180- return alert ( 'Please provide an application URL you want to trust' )
167+ return alert ( 'Please provide an application URL you want to remove trust from ' )
181168 }
182169
183- // remove existing statements on same origin - if it exists
184- kb . statementsMatching ( null , ns . acl ( 'origin' ) , origin ) . forEach ( st => {
185- kb . removeStatements ( [ ...kb . statementsMatching ( null , ns . acl ( 'trustedApp' ) , st . subject ) ] )
186- kb . removeStatements ( [ ...kb . statementsMatching ( st . subject ) ] )
187- } )
170+ var deletions = getStatementsToDelete ( origin )
171+ kb . updater . update ( deletions , null , handleUpdateResponse )
172+ }
188173
189- save ( )
174+ function getStatementsToAdd ( origin ) {
175+ var application = new $rdf . BlankNode ( `bn_${ generateRandomString ( ) } ` )
176+ return [
177+ $rdf . st ( subject , ns . acl ( 'trustedApp' ) , application , profile ) ,
178+ $rdf . st ( application , ns . acl ( 'origin' ) , origin , profile ) ,
179+ ...trustedApplicationState . formElements . modes
180+ . filter ( checkbox => checkbox . checked )
181+ . map ( checkbox => $rdf . sym ( checkbox . value ) )
182+ . map ( mode => $rdf . st ( application , ns . acl ( 'mode' ) , mode , profile ) )
183+ ]
190184 }
191185
192- function save ( ) {
193- // remove response triples - this should not be necessary, but do not know how to turn it off
194- kb . statementsMatching ( null , ns . link ( 'response' ) ) . forEach ( st => {
195- kb . removeStatements ( [ ...kb . statementsMatching ( st . subject ) ] )
196- kb . removeStatements ( [ ...kb . statementsMatching ( st . object ) ] )
197- } )
186+ function getStatementsToDelete ( origin ) {
187+ var applicationStatements = kb . statementsMatching ( null , ns . acl ( 'origin' ) , origin )
188+ return applicationStatements . reduce ( ( memo , st ) => memo
189+ . concat ( kb . statementsMatching ( subject , ns . acl ( 'trustedApp' ) , st . subject ) )
190+ . concat ( kb . statementsMatching ( st . subject ) ) ,
191+ [ ] )
192+ }
198193
199- // serialize data
200- $rdf . serialize ( null , kb , subject . uri , 'text/turtle' , ( err , data ) => {
201- if ( err ) {
202- alert ( 'Something went wrong when preparing data for the server. Try again.' )
203- return
204- }
205- // save data to POD
206- kb . fetcher . webOperation ( 'PUT' , subject . uri , {
207- data,
208- saveMetadata : false ,
209- contentType : 'text/turtle'
210- } )
211- . then ( ( ) => updateTable ( ) )
212- } )
194+ function handleUpdateResponse ( uri , success , errorBody ) {
195+ if ( success ) {
196+ return updateTable ( )
197+ }
198+ console . error ( uri , errorBody )
213199 }
214200}
215201
@@ -253,6 +239,10 @@ function createModesInput({ appModes, formElements }) {
253239 } )
254240}
255241
242+ function generateRandomString ( ) {
243+ return Math . random ( ) . toString ( 36 ) . substring ( 7 )
244+ }
245+
256246if ( nodeMode ) {
257247 module . exports = thisPane
258248} else {
0 commit comments