comparison xul/content/annota.js @ 203:bf945fcf9105

restarting with version control of xul sidebar/toolbar
author luginbue
date Fri, 27 Feb 2004 11:24:53 +0100
parents
children 8e4724b71b67
comparison
equal deleted inserted replaced
202:7501034e54e1 203:bf945fcf9105
1 include ('chrome://jslib/content/io/dir.js');
2 include ('chrome://jslib/content/io/file.js');
3 /*include ('chrome://jslib/content/io/rdfBase.js');
4 include ('chrome://jslib/content/io/rdfResource.js');
5 include ('chrome://jslib/content/io/rdfContainer.js');*/
6 include ('chrome://jslib/content/io/rdf.js');/*
7 include ('chrome://jslib/content/io/rdfFile.js');*/
8 include ('chrome://jslib/content/io/fileUtils.js');
9
10 var slash='/';
11 if (navigator.platform=="Win32"){
12 slash='\\';
13 }
14
15 var directory=slash;
16 var digilib_path=slash;
17
18 getProfile();
19
20 function makePathCompatible(path){
21 if (navigator.platform=="Win32"){
22 // slash durch backslash ersetzten
23 path=path.replace(/\//g,"\\");
24 // nur 2 backslashs am anfang
25 path=path.replace(/^\\{3}/,"\\\\");
26 // vor Laufwerkbuchstaben kein Backslash
27 if (path.indexOf(":")>0){
28 path=path.replace(/^\\/g,"");
29 }
30 // nur ein Slash gibt Absturz
31 path=path.replace(/^\\$/,"c:\\");
32 //alert(path);
33 }
34 return path;
35 }
36
37 function file_open(){
38 var nsIFilePicker = Components.interfaces.nsIFilePicker;
39 var fp = Components.classes["@mozilla.org/filepicker;1"]
40 .createInstance(nsIFilePicker);
41 fp.init(window, "Select a Directory", nsIFilePicker.modeGetFolder);
42
43 // set default direcotry
44 var aLocalFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
45 directory=makePathCompatible(directory);
46 aLocalFile.initWithPath(directory);
47 fp.displayDirectory=aLocalFile;
48
49 var res=fp.show();
50 if (res==nsIFilePicker.returnOK){
51 directory=fp.fileURL.path;
52 directory=makePathCompatible(directory);
53 setTreeDirectory();
54 }
55 }
56
57 function setTreeDirectory(){
58 var t=document.getElementById("file_tree");
59 t.setAttribute("datasources","file://"+directory+'test.rdf');
60 //alert('datasource: '+ t.getAttribute("datasources"));
61 //alert('Directory: file://'+directory+'/test.rdf');
62 }
63
64 function refreshTree(){
65 var t=document.getElementById("file_tree");
66 t.builder.rebuild();
67 }
68
69 function getAttributeOfSelectedNode(attribute) {
70 var tree=document.getElementById("file_tree");
71 try {
72 return tree.view.getCellText(tree.currentIndex,attribute);
73 } catch(e) {
74 return '';
75 }
76 }
77
78 /*
79 function createSequence(completeURN) {
80 if(completeURN.indexOf(slash) != -1) {
81 alert('Recursion: '+completeURN);
82 //lowest sequence
83 var seqNodeContainer = dataSource.getNode(completeURN.substring(0,completeURN.lastIndexOf(":")));
84 seqNodeContainer.makeSeq();
85 //lowest description
86 var rootNodeContainer = dataSource.getNode(completeURN.substring(0,completeURN.lastIndexOf(":")));
87 var fnTemp = completeURN.substring(0,completeURN.lastIndexOf(":"));
88 rootNodeContainer.addTarget("http://echo.unibe.ch/digilib/rdf#fn",completeURN.substring(
89 fnTemp.lastIndexOf(":")+1,completeURN.lastIndexOf(":")));
90
91 completeURN = completeURN.replace(slash,':');
92
93 //next higher sequence
94 var seqNode = dataSource.getNode(completeURN.substring(0,completeURN.lastIndexOf(":")));
95 seqNode.makeSeq();
96 //next higher description
97 var rootNode = dataSource.getNode(completeURN.substring(0,completeURN.lastIndexOf(":")));
98 fnTemp = completeURN.substring(0,completeURN.lastIndexOf(":"));
99 rootNode.addTarget("http://echo.unibe.ch/digilib/rdf#fn",completeURN.substring(
100 fnTemp.lastIndexOf(":")+1,completeURN.lastIndexOf(":")));
101 seqNodeContainer.addChild(rootNode);
102 alert('Recursion end: '+completeURN);
103
104 if(completeURN.lastIndexOf(slash) == -1) {
105 alert('Recursion finished : '+completeURN);
106 var node = dataSource.getNode(completeURN);
107 var arrayParams=window.content.listParameters();
108 for (i=0; i< arrayParams.length; i++){
109 var value=window.content.getParameter(arrayParams[i]);
110 //alert(arrayParams[i]+":"+value);
111 node.addTarget("http://echo.unibe.ch/digilib/rdf#"+arrayParams[i],value);
112 }
113 node.addTarget("http://echo.unibe.ch/digilib/rdf#lv","1");
114 seqNode.addChild(node);
115 }
116 this.createSequence(completeURN);
117 }
118 }
119 */
120
121 /**
122 * @todo isn't complex yet...
123 */
124 function createComplexRDFEntry(completeURN) {
125 var title = window.prompt("Please enter a title for this Annotation",this.getAttributeOfSelectedNode('name'));
126 alert('Title: '+title);
127 var echoContainer = dataSource.getNode("urn:echo");
128 echoContainer.makeSeq();
129
130 echoContainer.addChild(completeURN);
131 var node = dataSource.getNode(completeURN);
132 var arrayParams=window.content.listParameters();
133 for (i=0; i< arrayParams.length; i++){
134 var value=window.content.getParameter(arrayParams[i]);
135 //alert(arrayParams[i]+":"+value);
136 node.addTarget("http://echo.unibe.ch/digilib/rdf#"+arrayParams[i],value);
137 }
138 node.addTarget("http://echo.unibe.ch/digilib/rdf#lv","1");
139 node.addTarget("http://purl.org/dc/elements/1.0/title",title);
140 node.addTarget("http://purl.org/dc/elements/1.0/creator","");
141 node.addTarget("http://purl.org/dc/elements/1.0/date","");
142 node.addTarget("http://www.w3.org/2000/10/annotation-ns#created","");
143 node.addTarget("http://www.w3.org/2000/10/annotation-ns#Annotation","targetof");
144 var node2 = dataSource.getNode("targetof");
145 node2.addTarget("http://echo.unibe.ch/digilib/rdf#lv","1");
146 /*ar subNode = dataSource.getNode('specialised');
147 node.addChild(subNode,true);*/
148 }
149
150 function file_save(){
151
152 // get Digilib-Parameter form Browser
153 //alert(window.content.location.href);
154
155 var documentpath='';
156 try{
157 documentpath=window.content.getParameter('fn');
158 documentpath=documentpath.replace(':',';');
159 }catch (e){
160 documentpath='';
161 }
162 if (documentpath != ''){
163 //documentpath=documentpath.replace(slash,':');
164 //alert(documentpath);
165 var docPath='urn:echo:'+documentpath;
166
167 var t=document.getElementById("file_tree");
168 dataSource = new RDFDataSource(t.getAttribute("datasources"));
169 this.createComplexRDFEntry(docPath);
170
171 /* //Create new Node
172 var node = dataSource.getNode(docPath);
173 var arrayParams=window.content.listParameters();
174 for (i=0; i< arrayParams.length; i++){
175 var value=window.content.getParameter(arrayParams[i]);
176 //alert(arrayParams[i]+":"+value);
177 node.addTarget("http://echo.unibe.ch/digilib/rdf#"+arrayParams[i],value);
178 }
179 node.addTarget("http://echo.unibe.ch/digilib/rdf#lv","1");
180 //Add the node to the Seq
181 var seqNode = dataSource.getNode(docPath.substring(0,docPath.lastIndexOf(":")));
182 if(seqNode.isContainer()) {
183 seqNode.addChild(node);
184 } else {
185 seqNode.makeSeq();
186 seqNode.addChild(node);
187 }
188 */
189 dataSource.save();
190 refreshTree();
191 /*
192 var ds=new RDFDataSource();
193 ds.parseFromString(rdfString,"http://echo.unibe.ch/digilib/rdf/digilib.rdf");
194 var node=ds.getNode(docPath);
195 var arrayParams=window.content.listParameters();
196 for (i=0; i< arrayParams.length; i++){
197 var value=window.content.getParameter(arrayParams[i]);
198 //alert(arrayParams[i]+":"+value);
199 node.addTarget("http://echo.unibe.ch/digilib/rdf#"+arrayParams[i],value);
200 }
201 node.addTarget("http://echo.unibe.ch/digilib/rdf#lv","1");
202 //alert(ds.serializeToString());
203 var nsIFilePicker = Components.interfaces.nsIFilePicker;
204 var fp = Components.classes["@mozilla.org/filepicker;1"]
205 .createInstance(nsIFilePicker);
206 fp.init(window, "Select a File", nsIFilePicker.modeSave);
207
208 // set default direcotry
209 var aLocalFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
210 directory=makePathCompatible(directory);
211 aLocalFile.initWithPath(directory);
212 fp.displayDirectory=aLocalFile;
213
214 fp.appendFilter("Annotations","*.rdf; *.RDF");
215 var res=fp.show();
216 if (res==nsIFilePicker.returnOK){
217 var thefile=fp.file;
218 // --- do something with the file here ---
219 //alert(fp.file);
220 //alert(fp.fileURL.path);
221
222 var strFilePath=fp.file.path;
223 strFilePath=strFilePath.toLowerCase();
224 if (strFilePath.indexOf('.rdf')<0){
225 strFilePath=fp.fileURL.path+".rdf";
226 }else{
227 strFilePath=fp.fileURL.path;
228 }
229 strFilePath=makePathCompatible(strFilePath);
230 var f=new File(strFilePath);
231 //var boolFileExists=f.exists();
232 //alert(boolFileExists);
233 f.create();
234 f.open('w');
235 f.write(ds.serializeToString());
236 f.close();
237 refreshTree();
238 }
239 //*/
240 } else{
241 alert("Error: no alcatraz component. can't create an annotation.");
242 }
243 }
244
245
246 function file_local(){
247 // noch nicht programmiert
248 }
249
250 function file_annotaDB(){
251 alert("At the moment it is not possible to use the annota DB! This feature is not yet programmed.");
252 var menu_item=document.getElementById('local');
253 menu_item.setAttribute('checked','true');
254 }
255
256 function tree_click(){
257 var t=document.getElementById("file_tree"); //tree element
258 var l=t.view.getItemAtIndex(t.currentIndex); //aus baum treeitem herausholen mit dem selected index (currentIndex)
259 //l.firstChild ist treeitem
260 var d=l.firstChild.firstChild; //treecell
261 var firstLabel=d.getAttribute("label");
262 var cols=document.getElementById("cols");
263 var col=cols.childNodes;
264 var nodes=l.firstChild.childNodes;
265 var rdf_file="";
266 for (var i=0;i<nodes.length;i++){
267 if (col[i].getAttribute("label")=="URL"){
268 rdf_file=nodes[i].getAttribute("label");
269 }
270 }
271 if (rdf_file!=""){
272 send_annotation(rdf_file);
273 }
274 alert(rdf_file);
275 return rdf_file;
276 }
277
278
279 /***
280 * Diese Funktion wird gebraucht um im Dialog den Pfad zu digilib zu setzen
281 *
282 ***/
283 function setDigilibPath(digilibPathValue){
284 digilib_path=digilibPathValue;
285 }
286
287 /***
288 * Dialog tools momentan kann man nur den Pfad zu digilib setzen
289 *
290 ***/
291 function show_dialog(dialog){
292 if (dialog=="tool path"){
293 window.openDialog("tools_dialog.xul","funny dialog",
294 "chrome",digilib_path,setDigilibPath);
295 }
296 }
297
298
299 /***
300 * Gibt den Inhalt eines Files als String zurueck
301 *
302 ***/
303 function readFile(str_Filename){
304 var f=new File(str_Filename);
305 var str="";
306 if (f.isFile()){
307 f.open();
308 str=f.read();
309 f.close();
310 } else {
311 alert(str_Filename + " is not a file");
312 }
313 return str;
314 }
315
316
317
318 function send_annotation(rdf_file){
319 rdf_file=rdf_file.replace(/^file:\/\//,"");
320 rdf_file=makePathCompatible(rdf_file);
321 digilib_path="http://pythia2.unibe.ch:8080/docuserver/digitallibrary";
322 alert("Send Annotation: "+rdf_file);
323 strRdfFile=readFile(rdf_file);
324 if (strRdfFile!=""){
325 var formid='mainform';
326 alert(digilib_path+"/digilib.jsp");
327 var form = createForm(formid, digilib_path+"/digilib.jsp", "post", "_content");
328 //var form = createForm(formid, "http://sophia.unibe.ch:8080/examples/servlet/RequestRDF", "post", "_content");
329 //var form = createForm(formid, "http://hera.unibe.ch:8080/examples/servlet/RequestRDF", "post", "_content");
330 setFormData(form, formid, strRdfFile);
331 form.submit();
332 }
333 }
334
335 function createForm(formid, action, method, target)
336 {
337 var form = document.getElementById(formid);
338 if(form != null)
339 document.documentElement.removeChild(form);
340
341 var form = document.createElementNS("http://www.w3.org/1999/xhtml", "form");
342 form.setAttribute("id", formid);
343 form.setAttribute("action", action);
344 form.setAttribute("method", method);
345 form.setAttribute("target", target);
346 document.documentElement.appendChild(form);
347 return form;
348 }
349
350 function setFormData(form, formid, rdf)
351 {
352 var val1 = document.createElementNS("http://www.w3.org/1999/xhtml", "input");
353 val1.setAttribute('type', 'hidden');
354 val1.setAttribute('name', 'rdf');
355 val1.setAttribute('value', rdf);
356 form.appendChild(val1);
357 }
358
359
360 function getProfileDirectory(){
361 // First get the directory service and query interface it to
362 // nsIProperties
363 var dirService = Components.
364 classes['@mozilla.org/file/directory_service;1'].
365 getService(Components.interfaces.nsIProperties);
366
367 // Next get the "ProfD" property of type nsIFile from the directory
368 // service, FYI this constant is defined in
369 // mozilla/xpcom/io/nsAppDirectoryServiceDefs.h
370
371 const NS_APP_USER_PROFILE_50_DIR = "ProfD";
372 profileDir = dirService.get(NS_APP_USER_PROFILE_50_DIR,
373 Components.interfaces.nsIFile);
374
375 // Now that we have it we can show it's path. See nsIFile for the
376 // other things you that can be done with profileDir
377 //alert(profileDir.path);
378 return profileDir.path;
379 }
380
381 function getProfile(){
382 var strProfile=readFile(getProfileDirectory()+slash+"annota.dat");
383 if (strProfile==""){
384 directory=slash;
385 digilib_path="http://hera.unibe.ch:8080/alcatraz";
386 setProfile();
387 }else{
388 var params=strProfile.split("\n");
389 for (var i=0;i<params.length;i++){
390 var key_value=params[i].split("|");
391 if (key_value[0]=='directory'){
392 directory=key_value[1];
393 }
394 if (key_value[0]=='tool path'){
395 digilib_path=key_value[1];
396 }
397 }
398 }
399 }
400
401 function setProfile(){
402 var f=new File(getProfileDirectory()+slash+'annota.dat');
403 f.create();
404 f.open('w');
405 f.write('directory|'+directory+'\n'+'tool path|'+digilib_path+'\n');
406 f.close();
407 }
408
409 function traverse(node){
410 if (node.hasChildNodes){
411 var arr_nodes=node.childNodes;
412 for (var i=0;i<arr_nodes.length;i++){
413 if (arr_nodes[i].getAttribute("open")){
414 //alert(arr_nodes[i].getAttribute("open"));
415 if (arr_nodes[i].hasChildNodes && arr_nodes[i].firstChild.hasChildNodes){
416 alert(arr_nodes[i].firstChild.firstChild.getAttribute("label"));
417 }
418 }
419 traverse(arr_nodes[i]);
420 }
421 }
422 }
423
424 function traverseTree(){
425 var t=document.getElementById("file_tree");
426 traverse(t);
427 }
428
429
430 /* @beat: ich habe mich erfrecht, diese billige methode in dein
431 javascript zu verschieben, weil ich eigentlich jetzt zu
432 dir gehoert und nicht mehr in ein alcatraz.js (das gar
433 nicht mehr existiert). alcatraz.xul ist nur noch ein
434 container fuer andere xul-files und hat deshalb auch
435 keine eigenen funktionialitaeten mehr - christian
436 */
437 function dialog_annotate() {
438 window.openDialog("chrome://alcatraz/content/dialog_annotate.xul", "dialog_annotate", "chrome,dialog,resizable=no", "");
439 }