blob: 88523442e014a6abe8478397988faab2c88bde68 [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001requirejs.config({
2 baseUrl: '../js/src',
3 paths : {
4 'lib': '../lib'
5 }
6});
7
8var hint = undefined;
9
10require(['plugin/server','panel/query', 'hint', 'hint/foundries/cnx','lib/domReady','api'], function (pluginClass, queryPanelClass, hintClass, hintArray, domReady) {
11 KorAP.hintArray = hintArray;
12 KorAP.Hint = null;
13
14 //TO BE ABLE TO USE THIS DEMO YOU MUST MANUALLY OVERRIDE LINE 9 IN hint/menu.js WITH hint/querystoringitem.js
15 // This is so that this demo can be looked at in the main branch. Currently it is not easy to override the item prototype class
16 // used in hint-menus.
17
18 //TODO: Tutorial on how to add to master
19 //TODO: How to querynames.
20
21 //How to add this to master:
22 // All KorAP.Hint.functionname = function(...) {...} should be added to the hint class. setNumQueries should be called upon hintClass creation.
23 // overwrite the onclick function in hint/item.js with the one provided in hint/querystoringitem.js. The buttons need to have be stored
24 // in static references, so that they can be re-added in unReadQueries and readQueries. I recommend making them attributes of either hint
25 // or hint.menu("-").container.
26
27 domReady(function() {
28 KorAP.URL="http://localhost:3000"
29
30 KorAP.Hint = hintClass.create();
31
32 KorAP.Hint.numStoredQueries = 0; //used for the query names.
33 /**
34 * Get and set the number of currently stored queries to what the server says. This is used to allow for
35 * the naming process of queries to always create queries with unique names until we implement custom query
36 * names.
37 */
38 KorAP.Hint.setNumQueries = function (retList) {
39 if (arguments.length === 0){ // regular function call
40 KorAP.API.getQueryList(KorAP.Hint.setNumQueries,console.log); //Ask the Server for count
41 } else if (retList !== undefined) { //call by the API (Server)
42 this.numStoredQueries = retList.length; //set the count thanks to server
43 console.log("Set counter for number of stored queries to: " + retList.length);
44 };
45 }.bind(KorAP.Hint);
46
47 KorAP.Hint.setNumQueries();
48
49 KorAP.restoreMinusMenuButton = { // Whenever used is at location 0
50 defaultTextValue : "Back",
51 onclick : function (e) {
52 console.log("back click");
53 this._menu.hint().unReadQueries();
54 this.content(); //Reset to default Text
55 //e.halt();
56 },
57 chop : function (e) {
58 this.onclick(e);
59 }
60 };
61
62 KorAP.triggerDeleteModeButton = { // Whenever used is at location 1
63 defaultTextValue : "Delete Queries",
64
65 onclick : function (e) {
66
67 if (this._menu.hint()._hintItemMode === "REGULAR") {
68 // make it so that selecting queries now deletes them later
69 this._menu.hint()._deleteTheseQueries = [];
70 this.content("Delete The Selected Queries");
71 this.menu().container().item(0).content("Cancel Deletion");
72 this._menu.hint()._hintItemMode = "DELETE SELECTION";
73
74 } else if (this._menu.hint()._hintItemMode === "DELETE SELECTION") {
75 // Delete stuff
76 this._menu.hint()._deleteTheseQueries.forEach(function (queryname){
77 KorAP.API.deleteQuery(queryname,console.log);
78 },undefined);
79 this._menu.hint()._deleteTheseQueries = [];
80 this._menu.hint()._hintItemMode = "REGULAR";
81 this.menu().container().item(0).content(); //"Back"
82 this.content("Start Deleting Queries");
83 KorAP.Hint.setNumQueries(); //Set value to something meaningful
84 KorAP.API.getQueryList(KorAP.Hint.readQueries,console.log);
85
86 } else {
87 //ERROR
88 console.log("What?");
89 };
90 this.menu("-").show();
91 //e.halt();
92 }
93 };
94
95 JSONListToQueryTemplates= function(JSONList) {
96 var queryItemTemplates = [];
97 if (JSONList !== undefined) {
98 JSONList.forEach(function(query){
99 queryItemTemplates.push([
100 query["name"],
101 query["koralQuery"] || query["q"],
102 query["description"] || ""
103 ])
104 }, undefined); //no "this" needed
105 };
106 return queryItemTemplates
107 };
108
109
110 KorAP.SaveButton = { // Whenever used is at location 0
111 onclick : function (e) {
112 var query = {
113 //As of now this content is completely irrelevant. Instead, all is stored thanks to the URL.
114 };
115 var newestQueryName = "query" + KorAP.Hint.numStoredQueries + "?q=" + this._menu._hint._inputField._el.value + "&desc=" + "ExampleDescr.";
116 //console.log(newestQueryName);
117 //Wenn voll CacheData löschen.
118 //Die Adresse muss sein:
119 // /query1?q=queryname?ql=...&desc=...
120 // See t/plugin/query_reference.t for examples
121 //putQuery will stringify it for you
122 KorAP.API.putQuery(newestQueryName,query,console.log());
123 KorAP.Hint.setNumQueries(); //Set value to something meaningful
124 //console.log(JSON.stringify(query));
125 this.menu("-").show();
126 //e.halt();
127 },
128 defaultTextValue : "Save Query"
129 };
130
131 KorAP.DisplayQueriesButton = { // Whenever used is at location 1
132 onclick : function (e) {
133 KorAP.API.getQueryList(KorAP.Hint.readQueries,console.log);
134 this.menu("-").show();
135 //e.halt();
136 },
137 defaultTextValue : "Display Stored Queries"
138 };
139
140
141 KorAP.Hint.menu("-").container().addItem(KorAP.SaveButton); //must be added to a specific context menu.
142 KorAP.Hint.menu("-").container().addItem(KorAP.DisplayQueriesButton); //must be added to a specific context menu.
143
144 KorAP.Hint.readQueries = function (JSONList) {
145 //console.log(JSONList);
146 this.menu("-").readItems(JSONListToQueryTemplates(JSONList));
147 this._hintItemMode = "REGULAR"; //alternative: "DELETE SELECTION"
148 this._deleteTheseQueries = [];
149 // add first, remove later to avoid empty lists
150 this.menu("-").container().addItem(KorAP.restoreMinusMenuButton);
151 this.menu("-").container().addItem(KorAP.triggerDeleteModeButton);
152 this.menu("-").container().removeItemByIndex(0); //Save Button
153 this.menu("-").container().removeItemByIndex(0); //Display Button
154 this.menu("-").show();
155 }.bind(KorAP.Hint);
156
157 KorAP.Hint.unReadQueries = function () {
158 KorAP.log("unread");
159 this.menu("-").readItems(KorAP.annotationHelper["-"]);
160 this._hintItemMode = "REGULAR"; //alternative: "DELETE SELECTION"
161 this._deleteTheseQueries = [];
162 // add first, remove later to avoid empty lists
163 this.menu("-").container().addItem(KorAP.SaveButton);
164 this.menu("-").container().addItem(KorAP.DisplayQueriesButton);
165 this.menu("-").container().removeItemByIndex(0); //restoreMinusMenuButton
166 this.menu("-").container().removeItemByIndex(0); //triggerDeleteModeButton
167 this.menu("-").show();
168
169 }.bind(KorAP.Hint);
170
171
172 /**
173 * Add query panel
174 */
175 var queryPanel = queryPanelClass.create();
176
177 // Get input field
178 var sform = document.getElementById("searchform");
179 var vcView = document.getElementById('vc-view')
180 if (sform && vcView) {
181 // The views are below the query bar
182 sform.insertBefore(queryPanel.element(),vcView);
183 KorAP.Panel = KorAP.Panel || {};
184 KorAP.Panel['query'] = queryPanel;
185 }
186
187 // Load plugin server
188 KorAP.Plugin = pluginClass.create();
189 KorAP.Plugin.register
190 // Register match plugin
191 KorAP.Plugin.register({
192 'name' : 'Example New',
193 'desc' : 'Some content about cats',
194 'embed' : [{
195 'panel' : 'query',
196 'title' : 'Translate',
197 'classes' : ['translate'],
198 'onClick' : {
199 "template" : "http://localhost:3003/demo/plugin-client.html"
200 }
201 },{
202 'panel' : 'query',
203 'title' : 'Glemm',
204 'classes' : ['glemm'],
205 'onClick' : {
206 "action":"toggle",
207 "template" : "http://localhost:3003/demo/plugin-client.html"
208 }
209 }]
210 });
211
212 // Register match plugin
213 KorAP.Plugin.register({
214 'name' : 'Glimpse',
215 'desc' : 'Shorten all queries',
216 'embed' : [{
217 'panel' : 'query',
218 'title' : 'Glimpse',
219 'classes' : ['glimpse'],
220 'onClick' : {
221 "action" : "toggle",
222 "template" : "http://localhost:3003/demo/plugin-client.html"
223 }
224 }]
225 });
226
227 console.log(KorAP.Hint);
228 });
229});
230
231function demoAlert (pos, msg) {
232 if (KorAP.hint !== undefined)
233 KorAP.Hint.alert(pos, msg);
234}