1 | /* |
---|
2 | * @content Mocking retrieval of annotations |
---|
3 | * (see: https://trac.clarin.eu/wiki/DASISH/XSD%20and%20XML#RespondingGETapiannotationslinkhttp:en.wikipedia.orgwikiSagrada_FamC3ADliaaccessread; |
---|
4 | * Scenario - visiting an annotated web page: https://trac.clarin.eu/wiki/DASISH/Scenario) |
---|
5 | */ |
---|
6 | |
---|
7 | //var url = '/api/annotations?link="http://en.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia"'; |
---|
8 | var url = '/api/annotations?link="http://snd.gu.se/"'; |
---|
9 | |
---|
10 | |
---|
11 | // Mock for Responding GET api/annotations?link="URI" |
---|
12 | $.mockjax({ |
---|
13 | /* url: A string or regular expression specifying the url of the request that the data should be mocked for. */ |
---|
14 | // url: '/api/annotations?link="http://en.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia"&access=read', |
---|
15 | url: '/api/annotations?link="http://en.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia"', |
---|
16 | // url: '/api/annotations?link="http://en.wikipedia.org/wiki/Sagrada_Fam%C3%ADlia"', |
---|
17 | /* type: Specify what HTTP method to match, usually GET or POST. Case-insensitive, so get and post also work. */ |
---|
18 | type: 'GET', |
---|
19 | /* dataType: Allowed data formats are Text, HTML, JSON, Script and XML. */ |
---|
20 | dataType: 'xml', |
---|
21 | /* responseTime: An integer that specifies a simulated network and server latency (in milliseconds). */ |
---|
22 | responseTime: 750, |
---|
23 | /* proxy: A string specifying a path to a file, from which the contents will be returned for the request. */ |
---|
24 | proxy: 'mocks/annotations-sagrada-GET.xml', |
---|
25 | }); |
---|
26 | |
---|
27 | $.mockjax({ |
---|
28 | /* url: A string or regular expression specifying the url of the request that the data should be mocked for. */ |
---|
29 | // url: '/api/annotations?link="http://snd.gu.se/"&access=read', |
---|
30 | url: '/api/annotations?link="http://snd.gu.se/"', |
---|
31 | // url: '/api/annotations?link="http://snd.gu.se/"', |
---|
32 | /* type: Specify what HTTP method to match, usually GET or POST. Case-insensitive, so get and post also work. */ |
---|
33 | type: 'GET', |
---|
34 | /* dataType: Allowed data formats are Text, HTML, JSON, Script and XML. */ |
---|
35 | dataType: 'xml', |
---|
36 | /* responseTime: An integer that specifies a simulated network and server latency (in milliseconds). */ |
---|
37 | responseTime: 750, |
---|
38 | /* proxy: A string specifying a path to a file, from which the contents will be returned for the request. */ |
---|
39 | proxy: 'mocks/annotations-snd-GET.xml', |
---|
40 | }); |
---|
41 | |
---|
42 | // Mock for Responding GET api/annotations/AID20130808114716 (annot-0001-GET.xml) |
---|
43 | $.mockjax({ |
---|
44 | url: '/api/annotations/AID20130808114716', |
---|
45 | type: 'GET', |
---|
46 | dataType: 'xml', |
---|
47 | responseTime: 750, |
---|
48 | proxy: 'mocks/annot-0001-GET.xml', |
---|
49 | }); |
---|
50 | |
---|
51 | // Mock for Responding GET api/annotations/AID20130808124131 (annot-0002-GET.xml) |
---|
52 | $.mockjax({ |
---|
53 | url: '/api/annotations/AID20130808124131', |
---|
54 | type: 'GET', |
---|
55 | dataType: 'xml', |
---|
56 | responseTime: 750, |
---|
57 | proxy: 'mocks/annot-0002-GET.xml', |
---|
58 | }); |
---|
59 | |
---|
60 | // Mock for Responding GET api/annotations/AID20130809151651 (annot-0003-GET.xml) |
---|
61 | $.mockjax({ |
---|
62 | url: '/api/annotations/AID20130809151651', |
---|
63 | type: 'GET', |
---|
64 | dataType: 'xml', |
---|
65 | responseTime: 750, |
---|
66 | proxy: 'mocks/annot-0003-GET.xml', |
---|
67 | }); |
---|
68 | |
---|
69 | // Mock for Responding GET api/annotations/AID20130816145322 (annot-0004-GET.xml) |
---|
70 | $.mockjax({ |
---|
71 | url: '/api/annotations/AID20130816145322', |
---|
72 | type: 'GET', |
---|
73 | dataType: 'xml', |
---|
74 | responseTime: 750, |
---|
75 | proxy: 'mocks/annot-0004-GET.xml', |
---|
76 | }); |
---|
77 | |
---|
78 | // Mock for Responding GET api/annotations/AID20130816170015 (annot-0005-GET.xml) |
---|
79 | $.mockjax({ |
---|
80 | url: '/api/annotations/AID20130816170015', |
---|
81 | type: 'GET', |
---|
82 | dataType: 'xml', |
---|
83 | responseTime: 750, |
---|
84 | proxy: 'mocks/annot-0005-GET.xml', |
---|
85 | }); |
---|
86 | |
---|
87 | // Mock for Responding GET api/annotations/AID20130920110032 (annot-0006-GET.xml) |
---|
88 | $.mockjax({ |
---|
89 | url: '/api/annotations/AID20130920110032', |
---|
90 | type: 'GET', |
---|
91 | dataType: 'xml', |
---|
92 | responseTime: 750, |
---|
93 | proxy: 'mocks/annot-0006-GET.xml', |
---|
94 | }); |
---|
95 | |
---|
96 | var XMLData = function() { |
---|
97 | return { |
---|
98 | getMockAnnotations: function(url) { |
---|
99 | $.ajax({ |
---|
100 | url: url, |
---|
101 | dataType: 'xml', |
---|
102 | type: 'GET', |
---|
103 | success: function(xml, textStatus, jqXHR) { |
---|
104 | // success: function(xml) { |
---|
105 | // can be used if parameters textStatus and jqXHR are not required within the following code block |
---|
106 | |
---|
107 | $xml = $(xml); |
---|
108 | |
---|
109 | console.log('Data: ' + $xml); |
---|
110 | console.log($xml); |
---|
111 | console.log('Status: ' + textStatus); |
---|
112 | console.log('XMLHttpRequest object: ' + jqXHR); |
---|
113 | console.log(jqXHR); |
---|
114 | |
---|
115 | var aidArr = []; |
---|
116 | var aid_string = ""; |
---|
117 | |
---|
118 | $xml.find('annotations annotation').each(function() { |
---|
119 | $aid_uri = $(this).attr('ref'); |
---|
120 | aidArr.push($aid_uri.substr($aid_uri.lastIndexOf('/')+1)); // http://dasish.eu/annotations/ is erased |
---|
121 | }); |
---|
122 | |
---|
123 | console.log(aidArr); |
---|
124 | |
---|
125 | for (var i in aidArr) { |
---|
126 | aid_string += '<br />' + aidArr[i]; |
---|
127 | $('#xmlContents').html('<h4 style="margin-bottom: 0;">List of AIDs returned from REST service address<br />GET api/annotations?link="URI":</h4>' + aid_string); |
---|
128 | url = ""; |
---|
129 | url = '/api/annotations/' + aidArr[i]; |
---|
130 | |
---|
131 | $.ajax({ |
---|
132 | url: url, |
---|
133 | dataType: 'xml', |
---|
134 | type: 'GET', |
---|
135 | success: function(xml) { |
---|
136 | |
---|
137 | $xml = $(xml); |
---|
138 | |
---|
139 | var body = ""; |
---|
140 | var aid = ""; |
---|
141 | |
---|
142 | $xml.find('annotation').each(function() { |
---|
143 | body = $(this).find('body').text(); |
---|
144 | aid = $(this).attr('URI').substr($(this).attr('URI').lastIndexOf('/')+1); |
---|
145 | |
---|
146 | $('#xmlContents').append("<br /><br /><strong>Body content of " + aid + ":</strong><br /><em>" + body + "</em>"); |
---|
147 | |
---|
148 | var om_object = annotation2om_object(this); |
---|
149 | $('#xmlContents').append("<br /><strong>converted to om_object:</strong><br/><textarea cols='130' rows='18'>"+JSON.stringify(om_object, null, ' ')+"</textarea>"); |
---|
150 | }); |
---|
151 | |
---|
152 | } |
---|
153 | }); |
---|
154 | |
---|
155 | } |
---|
156 | |
---|
157 | } |
---|
158 | |
---|
159 | }); |
---|
160 | } |
---|
161 | } |
---|
162 | }(); |
---|
163 | |
---|
164 | XMLData.getMockAnnotations(url); |
---|