-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor
1094 lines (916 loc) · 38.3 KB
/
editor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:rdfe="http://redfoot.net/3.0/rdf#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:code="http://redfoot.net/3.0/code#"
xmlns:redfoot="http://redfoot.net/3.0/redfoot#"
xmlns:server="http://redfoot.net/3.0/server#"
xmlns:template="http://redfoot.net/3.0/template#"
xmlns:kid='http://redfoot.net/3.0/kid#'
xmlns:aspect="http://redfoot.net/3.0/aspect#"
>
<rdfe:Namespace rdf:about="#">
<rdfs:label>Editor</rdfs:label>
<rdfs:comment>The Redfoot editor namespace.</rdfs:comment>
</rdfe:Namespace>
<aspect:Aspect rdf:ID="aspect">
<rdfs:label>Editor</rdfs:label>
<aspect:item rdf:resource="#editor"/>
<aspect:item rdf:resource="#subjects"/>
<aspect:item rdf:resource="#assertions"/>
<aspect:item rdf:resource="#assert"/>
</aspect:Aspect>
<template:Section rdf:ID="editor">
<aspect:location>/editor/</aspect:location>
<rdfs:label>Editor</rdfs:label>
<rdfs:comment></rdfs:comment>
<rdfs:member rdf:resource="http://redfoot.net/3.0/comment#exclude"/>
<template:default_section_rank>55.0</template:default_section_rank>
<server:allow rdf:resource="#Admin"/>
<template:head_content>
<kid:PagePartHandler rdf:ID="editor_head">
<kid:template rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
<div xmlns="http://www.w3.org/1999/xhtml" xmlns:kid="http://purl.org/kid/ns#" kid:strip="">
<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.2.0/build/reset/reset.css"/>
<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.2.0/build/fonts/fonts.css"/>
<!-- <link type="text/css" rel="stylesheet" href="./css/examples.css"/>-->
<script type="text/javascript">
${redfoot.value(URIRef("#json_script", base=__uri__), RDF.value)}
</script>
<style type="text/css">
#uriresult {position:relative; display:inline;}
#uriautocomplete {position:relative;width:25em;margin-bottom:1em;}/* set width of widget here*/
#uriautocomplete {z-index:9000} /* for IE z-index of absolute divs inside relative divs issue */
#uriinput { dipslay:none; _position:absolute;width:100%;height:1.4em;z-index:0;} /* abs for ie quirks */
#uricontainer {position:absolute;top:1.7em;width:100%}
#uricontainer .yui-ac-content {position:absolute;width:100%;border:1px solid #404040;background:#fff;overflow:hidden;z-index:9050;}
#uricontainer .yui-ac-shadow {position:absolute;margin:.3em;width:100%;background:#a0a0a0;z-index:9049;}
#uricontainer ul {padding:5px 0;width:100%;}
#uricontainer li {padding:0 5px;cursor:default;white-space:nowrap;}
#uricontainer li.yui-ac-highlight {background:#ff0;}
#uricontainer li.yui-ac-prehighlight {background:#FFFFCC;}
.resource {padding: 1em;}
.label {}
.uri { color: gray;} /* */
x.uri { position: absolute; top: 1em; left: 0em; width: 0%} /* */
x.uri { display: none;} /* */
.undefined:first-child { content: 'undefined';}
.type { display: none;}
.subject { display: inline;}
.subject .label {font-size: 2em;}
.subject .uri {font-size: 0.8em;}
.assertions {margin-left: 0em; padding-left: 0em;}
.assertion {display: block; padding-bottom: 1em; clear: both;}
.assertion .label {font-size: 1.2em;}
.assertion .uri {font-size: 0.6em;}
.predicate {display: inline; float: left; padding-left: 1em;}
.object {display: inline; float: left; padding-left: 1em;}
.assertion .action {display: inline; float: left;}
x.subject:before {content: 'Subject:';}
x.predicate:before {content: 'Predicate:';}
x.predicate:after {content: ':';}
x.object:before {content: 'Object:';}
#actions {margin-top: 1em; clear: both;}
</style>
</div>
]]>
</kid:template>
</kid:PagePartHandler>
</template:head_content>
<template:content>
<kid:PagePartHandler rdf:ID="Editor">
<kid:template rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
<div xmlns="http://www.w3.org/1999/xhtml" xmlns:kid="http://purl.org/kid/ns#">
<div class="resource">
<div class="undefined"></div>
<div class="subject" onclick="resourceEditor.editSubject(this);">
<div class="type">URIRef</div>
<div class="label">Pick a Subject</div>
<div class="uri" onclick="resourceEditor.viewSubject(this);"></div>
</div>
<ul class="assertions"></ul>
<div id="actions">Actions: [ <span onclick="resourceEditor.addAssertion(this);">add assertion</span> ]</div>
<div id="grab_bag" style="display: none">
<div id="new_assertion">
<li class="assertion">
<div class="action"><span onclick="resourceEditor.cloneAssertion(this)">+</span> <span onclick="resourceEditor.removeAssertion(this)">-</span></div>
<div class="predicate" onclick="resourceEditor.edit(this);">
<div class="type">URIRef</div>
<div class="label">Pick Predicate</div>
<div class="uri"></div>
</div>
<div class="object" onclick="resourceEditor.edit(this);">
<div class="type">URIRef</div>
<div class="label">Pick Object</div>
<div class="uri"></div>
</div>
</li>
</div>
<form id="uriEditor" onsubmit="return resourceEditor.updateValue();">
<div id="uriautocomplete">
<input id="uriinput"></input>
<div id="uricontainer"></div>
</div>
</form>
<textarea id="textEditor" onblur="resourceEditor.updateValue();"></textarea>
</div>
</div>
<!-- Libary begins -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.0/build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.0/build/dom/dom-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.0/build/event/event-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.0/build/animation/animation-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.0/build/connection/connection-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.2.0/build/autocomplete/autocomplete-min.js"></script>
<!--
<script type="text/javascript" src="/yui/build/yahoo/yahoo-debug.js"></script>
<script type="text/javascript" src="/yui/build/dom/dom-debug.js"></script>
<script type="text/javascript" src="/yui/build/event/event-debug.js"></script>
<script type="text/javascript" src="/yui/build/animation/animation-debug.js"></script>
<script type="text/javascript" src="/yui/build/connection/connection-debug.js"></script>
<script type="text/javascript" src="/yui/build/autocomplete/autocomplete-debug.js"></script>
-->
<!-- Library ends -->
<script type="text/javascript">
${redfoot.value(URIRef("#script", base=__uri__), RDF.value)}
</script>
</div>
]]>
</kid:template>
</kid:PagePartHandler>
</template:content>
</template:Section>
<rdf:Description rdf:ID="script">
<rdfs:label>Editor script</rdfs:label>
<rdf:value rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
function Node(element) {
this._element = element;
}
Node.prototype._getValue = function(name) {
var e = (YAHOO.util.Dom.getElementsByClassName(name, null, this._element))[0];
return e.innerHTML;
}
Node.prototype._setValue = function(name, s) {
var e = (YAHOO.util.Dom.getElementsByClassName(name, null, this._element))[0];
e.innerHTML = s
}
Node.prototype.getData = function() {
element = this._element;
var type_element = (YAHOO.util.Dom.getElementsByClassName("type", null, element))[0];
var type = type_element.innerHTML;
var data;
if (type=="URIRef") {
var label_element = (YAHOO.util.Dom.getElementsByClassName("label", null, element))[0];
var uri_element = (YAHOO.util.Dom.getElementsByClassName("uri", null, element))[0];
data = {"type": type, "label": label_element.innerHTML, "uri": uri_element.innerHTML};
} else if (type=="BNode") {
var label_element = (YAHOO.util.Dom.getElementsByClassName("label", null, element))[0];
var bnode_element = (YAHOO.util.Dom.getElementsByClassName("bnode", null, element))[0];
data = {"type": type, "label": label_element.innerHTML, "bnode": bnode_element.innerHTML};
} else if (type=="Literal") {
var value_element = (YAHOO.util.Dom.getElementsByClassName("value", null, element))[0];
var datatype_element = (YAHOO.util.Dom.getElementsByClassName("datatype", null, element))[0];
var language_element = (YAHOO.util.Dom.getElementsByClassName("language", null, element))[0];
data = {"type": type, "value": value_element.innerHTML,
"datatype": datatype_element.innerHTML,
"language": language_element.innerHTML};
} else {
alert ("Unexpected type of: " + type)
}
return data;
}
Node.prototype.setData = function(data) {
var e = this._element
while (e.firstChild) {
e.removeChild(e.firstChild);
};
if (data["type"]=="URIRef") {
e.innerHTML = "<div class='type'>URIRef</div><div class='label'>"+ data["label"] + "</div><div class='uri' onclick='resourceEditor.viewSubject(this);'>" + data["uri"] + "</div>";
}
else if (data["type"]=="BNode") {
e.innerHTML = "<div class='type'>BNode</div><div class='label'>"+ data["label"] + "</div><div class='bnode'>" + data["bnode"] + "</div>";
}
else if (data["type"]=="Literal") {
e.innerHTML = "<div class='type'>Literal</div><pre class='value'>"+ data["value"] + "</pre><div class='datatype'>" + data["datatype"] + "</div><div class='language'>" + data["language"] + "</div>";
} else {
alert ("Unexpected type of:" + data["type"]);
}
}
// TODO: 'subclass'?
/* TODO: turn uri and label into properties: http://www.webfx.nu/dhtml/mozInnerHTML/mozInnerHtml.html */
Node.prototype.getURI = function() {
return this._getValue("uri")
}
Node.prototype.setURI = function(uri) {
var data = {"type": "URIRef", "uri": uri, "label": this.getLabel()};
this.setData(data)
//return this._setValue("uri", uri)
}
Node.prototype.getLabel = function() {
return this._getValue("label")
}
Node.prototype.setLabel = function(label) {
return this._setValue("label", label)
}
function Assertion(element) {
this._element = element;
}
Assertion.prototype.setData = function(data) {
element = this._element;
predicate_element = (YAHOO.util.Dom.getElementsByClassName("predicate", null, element))[0];
var p = new Node(predicate_element);
p.setData(data["predicate"])
object_element = (YAHOO.util.Dom.getElementsByClassName("object", null, element))[0];
var n = new Node(object_element);
n.setData(data["object"]);
}
function URIEditor(resourceEditor) {
this.resourceEditor = resourceEditor
var uriEditor = this;
YAHOO.widget.AutoComplete.prototype._origUpdateValue = YAHOO.widget.AutoComplete.prototype._updateValue
YAHOO.widget.AutoComplete.prototype._updateValue = function(oItem) {
uriEditor.target.setLabel(oItem._oResultData[0]);
uriEditor.target.setURI(oItem._oResultData[1]);
this._origUpdateValue(oItem);
}
}
URIEditor.prototype.init = function() {
// Instantiate second AutoComplete
var oACDS;
// Instantiate second JS Array DataSource
//oACDS = new YAHOO.widget.DS_JSArray(subjectsArray);
oACDS = new YAHOO.widget.DS_XHR("/subjects/", ["Result", "label","uri"]);
oACDS.queryMatchContains = true;
this.oAutoComp = new YAHOO.widget.AutoComplete('uriinput','uricontainer', oACDS);
this.oAutoComp.minQueryLength = 2;
this.oAutoComp.queryDelay = 0.5;
this.oAutoComp.prehighlightClassName = "yui-ac-prehighlight";
this.oAutoComp.typeAhead = false;
this.oAutoComp.allowBrowserAutocomplete = false;
this.oAutoComp.useShadow = true;
this.oAutoComp.forceSelection = false;
this.oAutoComp.autoHighlight = false;
this.oAutoComp.maxResultsDisplayed = 20;
this.oAutoComp.formatResult = function(oResultItem, sQuery) {
var sMarkup = oResultItem[0] + " (" + oResultItem[1] + ")";
return (sMarkup);
};
var uriEditor = this;
this.oAutoComp.textboxBlurEvent.subscribe(function(type, args, me) {uriEditor.doneEditing()});
var form = document.getElementById("uriEditor");
this.form = form;
this.form.parentNode.removeChild(this.form);
this.text_box = document.getElementById("textEditor");
}
URIEditor.prototype.edit = function(element) {
target = new Node(element);
var data = target.getData();
if (data["type"]=="URIRef") {
target.setURI(""); // TODO
this.target = target;
var container = target._element.parentNode;
this.form.className = target._element.className;
container.replaceChild(this.form, target._element)
//container.insertBefore(this.form, target._element);
//this.form.style.display = '';// TODO: do this via className
//target._element.style.display = 'none';
this.oAutoComp._oTextbox.value = target.getLabel();
//this.oAutoComp._oTextbox.value = data["label"];
this.oAutoComp._oTextbox.select();
this.oAutoComp._oTextbox.focus();
} else if (data["type"]=="Literal") {
this.text_box.value = data["value"];
this.target = target;
var container = target._element.parentNode;
this.form.className = target._element.className;
container.replaceChild(this.text_box, target._element)
//this.text_box.value = "the value";
this.text_box.select();
this.text_box.focus();
} else {
alert("Unexpected type: ", data["type"]);
}
return false;
}
URIEditor.prototype.doneEditing = function() {
var editor;
var data = this.target.getData();
if (data["type"]=="URIRef") {
editor = this.form;
} else if (data["type"]=="Literal") {
editor = this.text_box;
} else {
alert("Unexpected type: ", data["type"]);
}
var container = editor.parentNode;
if (container) {
container.replaceChild(this.target._element, editor);
}
}
URIEditor.prototype.updateValue = function() {
var data = this.target.getData();
if (data["type"]=="URIRef") {
this.target.setLabel(this.oAutoComp._oTextbox.value);
// TODO:
this.oAutoComp._oTextbox.blur();
} else if (data["type"]=="Literal") {
this.target.setData({"type": "Literal", "value": this.text_box.value, "datatype": "", "language": ""});
} else {
alert("Unexpected type: ", data["type"]);
}
this.doneEditing();
return false;
}
function ResourceEditor() {
this.element = YAHOO.util.Dom.getElementsByClassName("resource", null, document.body)[0];
this.uriEditor = new URIEditor(this);
this.uriEditor.init();
}
ResourceEditor.prototype.getSubject = function() {
subject_element = (YAHOO.util.Dom.getElementsByClassName("subject", null, this.element))[0];
return new Node(subject_element).getData();
}
ResourceEditor.prototype.setSubject = function(label, uri) {
if (uri) {
this.addAssertions();
} else {
var value = "http://" + document.location.host + "/instances#" + encodeURIComponent(label.replace(/ /g, "_"));
var new_uri = window.prompt("create new resource with uri:", value);
subject_element = (YAHOO.util.Dom.getElementsByClassName("subject", null, this.element))[0];
var i = new Node(subject_element);
i.setURI(new_uri);
// TODO: support for literals, bnodes
// TODO: add callback to assert so we could have it call addAssertions
server.assert([["add", [{"type": "URIRef", "uri": new_uri}, {"type": "URIRef", "uri": "http://www.w3.org/2000/01/rdf-schema#label"}, {"type": "Literal", "value": label, datatype: "", language: ""}]]]);
this.addAssertions();
}
}
ResourceEditor.prototype.getPredicate = function(element) {
predicate_element = (YAHOO.util.Dom.getElementsByClassName("predicate", null, element))[0];
return new Node(predicate_element).getData();
}
ResourceEditor.prototype.getObject = function(element) {
object_element = (YAHOO.util.Dom.getElementsByClassName("object", null, element))[0];
return new Node(object_element).getData();
}
ResourceEditor.prototype.addAssertions = function() {
var resourceEditor = this;
var e = this.element;
while (e.className!='resource') {
e = e.parentNode;
}
var resource = e;
var assertions = resource.getElementsByTagName("ul")[0]; // TODO: make more robust
na = document.getElementById("new_assertion").getElementsByTagName("li")[0].cloneNode(true);
while (assertions.firstChild) {
assertions.removeChild(assertions.firstChild);
};
var handleSuccess = function(o) {
if(o.responseText !== undefined) {
v = o.responseText.parseJSON();
for (var i = 0; i < v.length; i++) {
resourceEditor.addAssertion(resourceEditor.element, v[i]);
}
//var tmp = document.createElement("div")
//tmp.innerHTML = "<pre>" + o.responseText + "</pre>"
//assertions.appendChild(tmp)
//div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
//div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
//div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
//div.innerHTML += "<li>HTTP headers: <ul>" + o.getAllResponseHeaders + "</ul></li>";
//div.innerHTML += "<li>Server response: " + o.responseText + "</li>";
//div.innerHTML += "<li>Argument object: Object ( [foo] => " + o.argument.foo +
//" [bar] => " + o.argument.bar +" )</li>";
}
}
var handleFailure = function(o){
if(o.responseText !== undefined) {
alert("failed to get assertions:" + o.status + " " + o.statusText);
//div.innerHTML = "<li>Transaction id: " + o.tId + "</li>";
//div.innerHTML += "<li>HTTP status: " + o.status + "</li>";
//div.innerHTML += "<li>Status code message: " + o.statusText + "</li>";
}
}
var callback =
{
success:handleSuccess,
failure: handleFailure,
argument: { foo:"foo", bar:"bar" }
};
var sUrl = "assertions/?subject=" + encodeURIComponent(this.getSubject().toJSONString());
var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, callback);
}
ResourceEditor.prototype.addAssertion = function(me, data) {
var e = me; //why is this not me.target like for edit_link??
while (e.className!='resource') {
e = e.parentNode;
}
var resource = e;
var assertions = resource.getElementsByTagName("ul")[0]; // TODO: make more robust
na = document.getElementById("new_assertion").getElementsByTagName("li")[0].cloneNode(true);
a = new Assertion(na)
if (data) {
a.setData(data)
}
assertions.appendChild(na);
}
ResourceEditor.prototype.cloneAssertion = function(me) {
var e = me;
while (e.className!='assertion') {
e = e.parentNode;
}
var na = e.cloneNode(true);
var a = new Assertion(na);
a.setData({"predicate": this.getPredicate(e), "object": {"type": "URIRef", "label": "----", "uri": ""}});
var assertions = this.element.getElementsByTagName("ul")[0]; // TODO: make more robust
assertions.appendChild(na);
}
ResourceEditor.prototype.removeAssertion = function(element) {
while (element.className!='assertion') {
element = element.parentNode;
}
var statement = [this.getSubject(),
this.getPredicate(element),
this.getObject(element)];
var cb = function() {
element.parentNode.removeChild(element);
}
server.assert([["remove", statement]], cb);
}
ResourceEditor.prototype.editSubject = function(element) {
this.currentStatement = null;
var result = this.uriEditor.edit(element);
return result;
}
ResourceEditor.prototype.viewSubject = function(element) {
uri = element.innerHTML;
document.location = uri.replace("#", "/");
return false;
}
ResourceEditor.prototype.edit = function(element) {
this.currentStatement = [this.getSubject(),
this.getPredicate(element.parentNode),
this.getObject(element.parentNode)];
var result = this.uriEditor.edit(element);
return result;
}
/**
* Some value has changed. Figure out what statement we want to ask
* the server to add or what statement we want the server to modify.
*/
ResourceEditor.prototype.updateValue = function() {
result = this.uriEditor.updateValue();
var target = this.uriEditor.target;
var newStatement = null;
var which = target._element.className;
if (which=="subject") {
this.currentStatement = null;
this.setSubject(target.getLabel(), target.getURI());
} else if (which=="predicate") {
var s = this.getSubject();
var o = this.getObject(target._element.parentNode);
if (s && o) {
newStatement = [s, target.getData(), o];
}
} else if (which=="object") {
var s = this.getSubject();
var p = this.getPredicate(target._element.parentNode);
if (s && p) {
var o = target.getData();
if (o && o["uri"]) {
;
} else {
var label = target.getLabel();
var new_uri = "http://" + document.location.host + "/instances#" + encodeURIComponent(label.replace(/ /g, "_"));
target.setURI(new_uri);
}
newStatement = [s, p, o];
}
} else {
alert('me');
}
if (newStatement && newStatement[0]!='' && newStatement[1]!='' && newStatement[2]!='') {
currentStatement = this.currentStatement
if (currentStatement) {
if(newStatement[0]!=currentStatement[0] ||
newStatement[1]!=currentStatement[1] ||
newStatement[2]!=currentStatement[2]) {
server.assert([["remove", this.currentStatement], ["add", newStatement]]);
}
} else {
server.assert([["add", newStatement]]);
}
}
return result;
}
function Server() {
}
Server.prototype.assert = function(change, assert_callback) {
var handleSuccess = function(o) {
if(o.responseText !== undefined) {
}
if (assert_callback) {
assert_callback()
}
}
var handleFailure = function(o){
if(o.responseText !== undefined) {
alert("failed to send assertion to server: " + o.status + " " + o.statusText);
}
}
var callback =
{
success:handleSuccess,
failure: handleFailure,
argument: { assert_callback:assert_callback, bar:"bar" }
};
var sUrl = "assert/"
var postData = "change=" + change.toJSONString();
var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}
server = new Server();
init = function() {
resourceEditor = new ResourceEditor();
// TODO: get value of uri query parameter and call resource.setResource
}
YAHOO.util.Event.addListener(this,'load',init);
]]>
</rdf:value>
</rdf:Description>
<server:Page rdf:ID="subjects">
<aspect:location>/subjects/</aspect:location>
<rdfs:label>Subjects</rdfs:label>
<server:allow rdf:resource="#Admin"/>
<rdfs:comment>Returns all the subject label uri pairs in JSON for the editor</rdfs:comment>
<rdfs:subClassOf rdf:resource="redfoot#Resource"/>
<server:supported_content_types>application/json</server:supported_content_types>
<server:page_handler>
<server:PageHandler rdf:ID="SubjectsPageHandler">
<rdfs:label>Subjects Page Handler</rdfs:label>
<server:content_type>application/json</server:content_type>
<code:python rdf:datatype="http://redfoot.net/3.0/redfoot#Python">
from rdflib import RDFS
import simplejson
query = request.parameters.get("query")
pairs = []
if query:
for s, p, o in redfoot.text_index.search(query, RDFS.label):
if o is not None: # bug workaround, I think. TODO: check if text_index is not being properly updated
pairs.append({"label": o, "uri": s})
#for s in set(redfoot.subjects(None, None)):
# label = redfoot.label(s)
# if label:
#pairs.append([label, s])
# pairs.append({"label": label, "uri": s})
response.write(simplejson.dumps({"Result": pairs}))
</code:python>
</server:PageHandler>
</server:page_handler>
</server:Page>
<server:Page rdf:ID="assertions">
<aspect:location>/editor/assertions/</aspect:location>
<rdfs:label>Assertions</rdfs:label>
<server:allow rdf:resource="#Admin"/>
<rdfs:comment>Returns all the subject label uri pairs in JSON for the editor</rdfs:comment>
<rdfs:subClassOf rdf:resource="redfoot#Resource"/>
<server:supported_content_types>application/json</server:supported_content_types>
<server:page_handler>
<server:PageHandler rdf:ID="AssertionsPageHandler">
<rdfs:label>Assertions Page Handler</rdfs:label>
<server:content_type>application/json</server:content_type>
<code:python rdf:datatype="http://redfoot.net/3.0/redfoot#Python">
# TODO: move this somewhere we can import it... we're already using it in two places.
def node(data):
if data["type"]=="URIRef":
return URIRef(data["uri"])
elif data["type"]=="BNode":
return BNode(data["bnode"])
elif data["type"]=="Literal":
datatype = data["datatype"] or None
language = data["language"] or None
return Literal(data["value"], lang=language, datatype=datatype)
else:
raise Exception("Unexpected type: '%s'" % data["type"])
from rdflib import RDFS
import simplejson
assertions = []
subject = request.parameters.get("subject")
if subject:
# TODO: support for BNodes
data = simplejson.loads(subject)
subject = node(data)
if (subject, RDF.type, None) not in redfoot:
redfoot.add((subject, RDF.type, RDFS.Resource))
initial_predicates = [RDFS.label, RDFS.comment, RDF.type]
remaining_predicates = []
for p in set(redfoot.predicates(subject, None)):
if p not in initial_predicates:
remaining_predicates.append((redfoot.label(p), p))
remaining_predicates.sort()
predicates = []
for p in initial_predicates:
predicates.append((redfoot.label(p), p))
predicates.extend(remaining_predicates)
for label, p in predicates:
for o in redfoot.objects(subject, p):
if isinstance(o, URIRef):
value = {"type": "URIRef", "label": redfoot.label(o), "uri": o}
elif isinstance(o, BNode):
value = {"type": "BNode", "label": redfoot.label(o), "bnode": o}
elif isinstance(o, Literal):
value = {"type": "Literal", "value": o, "datatype": o.datatype or "", "language": o.language or ""}
else:
raise Exception("unexpected type: %s" % type(o))
assertions.append({"predicate": {"type": "URIRef", "label": label, "uri": p},
"object": value})
response.write(simplejson.dumps(assertions))
</code:python>
</server:PageHandler>
</server:page_handler>
</server:Page>
<server:Page rdf:ID="assert">
<aspect:location>/editor/assert/</aspect:location>
<rdfs:label>Assert</rdfs:label>
<server:allow rdf:resource="#Admin"/>
<rdfs:comment>Returns all the subject label uri pairs in JSON for the editor</rdfs:comment>
<rdfs:subClassOf rdf:resource="redfoot#Resource"/>
<server:supported_content_types>application/json</server:supported_content_types>
<server:page_handler>
<server:PageHandler rdf:ID="AssertPageHandler">
<rdfs:label>Assert Page Handler</rdfs:label>
<server:content_type>application/json</server:content_type>
<code:python rdf:datatype="http://redfoot.net/3.0/redfoot#Python">
_logger = redfoot.getLogger(__uri__)
from rdflib import RDFS
from urllib import quote
import simplejson
assertions = []
change = request.parameters.get("change")
_logger.info("got change '%s'" % change)
def node(data):
_logger.info("data: '%s'" % repr(data))
if data["type"]=="URIRef":
if data["uri"]:
return URIRef(data["uri"])
else:
label = data["label"]
nuri = URIRef("%s/instances#%s" % (request.host, quote(label.replace(" ", "_"))))
_logger.info("coining: '%s'" % nuri)
redfoot.add((nuri, RDFS.label, Literal(data["label"])))
return nuri
elif data["type"]=="BNode":
return BNode(data["bnode"])
elif data["type"]=="Literal":
datatype = data["datatype"] or None
language = data["language"] or None
return Literal(data["value"], lang=language, datatype=datatype)
else:
raise Exception("Unexpected type: '%s'" % data["type"])
if change:
change = simplejson.loads(change)
for action, triple in change:
s, p, o = triple
s, p, o = node(s), node(p), node(o)
if action=="add":
redfoot.add((s, p, o))
elif action=="remove":
redfoot.remove((s, p, o))
else:
_logger.warning("Unknown action: '%s'" % action)
response.write("thank you for your support")
</code:python>
</server:PageHandler>
</server:page_handler>
</server:Page>
<rdf:Description rdf:ID="json_script">
<rdfs:label>json script</rdfs:label>
<rdf:value rdf:datatype="http://www.w3.org/2001/XMLSchema#string">
<![CDATA[
/*
json.js
2007-03-20
Public Domain
This file adds these methods to JavaScript:
array.toJSONString()
boolean.toJSONString()
date.toJSONString()
number.toJSONString()
object.toJSONString()
string.toJSONString()
These methods produce a JSON text from a JavaScript value.
It must not contain any cyclical references. Illegal values
will be excluded.
The default conversion for dates is to an ISO string. You can
add a toJSONString method to any date object to get a different
representation.
string.parseJSON(filter)
This method parses a JSON text to produce an object or
array. It can throw a SyntaxError exception.
The optional filter parameter is a function which can filter and
transform the results. It receives each of the keys and values, and
its return value is used instead of the original value. If it
returns what it received, then structure is not modified. If it
returns undefined then the member is deleted.
Example:
// Parse the text. If a key contains the string 'date' then
// convert the value to a date.
myData = text.parseJSON(function (key, value) {
return key.indexOf('date') >= 0 ? new Date(value) : value;
});
It is expected that these methods will formally become part of the
JavaScript Programming Language in the Fourth Edition of the
ECMAScript standard in 2008.
This file will break programs with improper for..in loops. See
http://yuiblog.com/blog/2006/09/26/for-in-intrigue/
This is a reference implementation. You are free to copy, modify, or
redistribute.
Use your own copy. It is extremely unwise to load untrusted third party
code into your pages.
*/
// Augment the basic prototypes if they have not already been augmented.
if (!Object.prototype.toJSONString) {
Array.prototype.toJSONString = function () {
var a = ['['], // The array holding the text fragments.
b, // A boolean indicating that a comma is required.
i, // Loop counter.
l = this.length,
v; // The value to be stringified.
function p(s) {
// p accumulates text fragments in an array. It inserts a comma before all
// except the first fragment.
if (b) {
a.push(',');
}
a.push(s);
b = true;
}
// For each value in this array...
for (i = 0; i < l; i += 1) {
v = this[i];
switch (typeof v) {
// Serialize a JavaScript object value. Ignore objects thats lack the
// toJSONString method. Due to a specification error in ECMAScript,
// typeof null is 'object', so watch out for that case.
case 'object':
if (v) {
if (typeof v.toJSONString === 'function') {
p(v.toJSONString());
}
} else {
p("null");
}
break;
// Otherwise, serialize the value.
case 'string':
case 'number':
case 'boolean':
p(v.toJSONString());
// Values without a JSON representation are ignored.
}
}
// Join all of the fragments together and return.
a.push(']');
return a.join('');
};
Boolean.prototype.toJSONString = function () {
return String(this);
};
Date.prototype.toJSONString = function () {
// Ultimately, this method will be equivalent to the date.toISOString method.
function f(n) {
// Format integers to have at least two digits.
return n < 10 ? '0' + n : n;
}
return '"' + this.getFullYear() + '-' +
f(this.getMonth() + 1) + '-' +
f(this.getDate()) + 'T' +
f(this.getHours()) + ':' +
f(this.getMinutes()) + ':' +
f(this.getSeconds()) + '"';
};
Number.prototype.toJSONString = function () {
// JSON numbers must be finite. Encode non-finite numbers as null.
return isFinite(this) ? String(this) : "null";
};
Object.prototype.toJSONString = function () {
var a = ['{'], // The array holding the text fragments.
b, // A boolean indicating that a comma is required.
k, // The current key.
v; // The current value.
function p(s) {
// p accumulates text fragment pairs in an array. It inserts a comma before all
// except the first fragment pair.
if (b) {
a.push(',');
}
a.push(k.toJSONString(), ':', s);
b = true;
}
// Iterate through all of the keys in the object, ignoring the proto chain.
for (k in this) {
if (this.hasOwnProperty(k)) {
v = this[k];
switch (typeof v) {
// Serialize a JavaScript object value. Ignore objects that lack the
// toJSONString method. Due to a specification error in ECMAScript,
// typeof null is 'object', so watch out for that case.
case 'object':
if (v) {
if (typeof v.toJSONString === 'function') {
p(v.toJSONString());
}
} else {
p("null");
}
break;
case 'string':
case 'number':
case 'boolean':
p(v.toJSONString());
// Values without a JSON representation are ignored.
}
}
}
// Join all of the fragments together and return.
a.push('}');
return a.join('');
};
(function (s) {