Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 1 | /** |
| 2 | * The result panel |
| 3 | * |
| 4 | * @author Nils Diewald |
| 5 | */ |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 6 | "use strict"; |
| 7 | |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 8 | define([ |
| 9 | 'panel', |
| 10 | 'view/result/koralquery' |
| 11 | ], function (panelClass, kqViewClass) { |
| 12 | |
| 13 | const d = document; |
| 14 | |
| 15 | // Localization values |
| 16 | const loc = KorAP.Locale; |
| 17 | loc.TOGGLE_ALIGN = loc.TOGGLE_ALIGN || 'toggle alignment'; |
| 18 | loc.SHOW_KQ = loc.SHOW_KQ || 'show KoralQuery'; |
Akron | ece4bb7 | 2020-10-19 12:24:20 +0200 | [diff] [blame] | 19 | |
| 20 | const aRoll = ['left', 'right','center']; |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 21 | |
| 22 | return { |
Akron | 386f122 | 2022-12-21 16:26:11 +0100 | [diff] [blame^] | 23 | type : 'result', |
| 24 | |
Akron | 6112b52 | 2018-07-25 13:48:53 +0200 | [diff] [blame] | 25 | create : function (opened) { |
| 26 | return Object.create(panelClass)._init(['result']).upgradeTo(this)._init(opened); |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 27 | }, |
| 28 | |
| 29 | // Initialize panel |
Akron | 6112b52 | 2018-07-25 13:48:53 +0200 | [diff] [blame] | 30 | _init : function (opened) { |
| 31 | this._opened = opened; |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 32 | |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 33 | // If plugins are enabled, add all buttons for the result panel |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 34 | if (KorAP.Plugin) { |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 35 | |
| 36 | // Add all result buttons in order |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 37 | KorAP.Plugin |
| 38 | .buttonGroup("result") |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 39 | .forEach(i => this.actions().add.apply(this.actions(), i)); |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 40 | |
| 41 | KorAP.Plugin.clearButtonGroup("result"); |
| 42 | }; |
| 43 | |
hebasta | d025eb9 | 2019-12-07 21:04:42 +0100 | [diff] [blame] | 44 | this.prepend = true; |
hebasta | 043e96f | 2019-11-28 12:33:00 +0100 | [diff] [blame] | 45 | |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 46 | return this; |
| 47 | }, |
| 48 | |
| 49 | |
| 50 | /** |
| 51 | * Add KoralQuery action to panel |
| 52 | */ |
| 53 | addKqAction : function () { |
| 54 | |
| 55 | // Open KoralQuery view |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 56 | const kqButton = this.actions().add( |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 57 | loc.SHOW_KQ, |
| 58 | {'cls':['show-kq','button-icon']}, |
| 59 | function () { |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 60 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 61 | const t = this; |
| 62 | |
| 63 | // Show only once - otherwise toggle |
| 64 | if (t._kq && t._kq.shown()) { |
| 65 | t._kq.close(); |
| 66 | return; |
| 67 | }; |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 68 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 69 | t._kq = kqViewClass.create(); |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 70 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 71 | // On close, remove session info on KQ |
| 72 | t._kq.onClose = function () { |
| 73 | delete this._opened['kq']; |
| 74 | }.bind(t); |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 75 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 76 | t._opened['kq'] = true; |
| 77 | t.add(t._kq); |
| 78 | } |
| 79 | ); |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 80 | |
| 81 | // Show KoralQuery in case it's meant to be shown |
Akron | 6112b52 | 2018-07-25 13:48:53 +0200 | [diff] [blame] | 82 | if (this._opened['kq']) |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 83 | kqButton.click(); |
| 84 | }, |
| 85 | |
Akron | 14f3ee9 | 2020-10-19 11:59:40 +0200 | [diff] [blame] | 86 | |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 87 | /** |
| 88 | * Add align action to panel |
| 89 | */ |
| 90 | addAlignAction : function () { |
| 91 | /** |
| 92 | * Toggle the alignment (left <=> right) |
| 93 | */ |
Akron | 37ea119 | 2021-07-28 10:40:14 +0200 | [diff] [blame] | 94 | this.actions().add(loc.TOGGLE_ALIGN, {'cls':['align','right','button-icon']}, function (e) { |
Akron | ece4bb7 | 2020-10-19 12:24:20 +0200 | [diff] [blame] | 95 | var olCl = d.querySelector('#search > ol').classList; |
| 96 | |
| 97 | aRoll.find(function(align, i) { |
| 98 | if (olCl.contains('align-' + align)) { |
| 99 | const n = i >= 2 ? 0 : i+1; |
| 100 | const nn = n >= 2 ? 0 : n+1; |
| 101 | olCl.remove('align-' + align); |
| 102 | olCl.add('align-' + aRoll[n]); |
| 103 | this.button.toggleClass(aRoll[n], aRoll[nn]); |
| 104 | return true; |
| 105 | }; |
| 106 | }, this); |
| 107 | }); |
Akron | 5cb9b2b | 2018-07-24 17:01:09 +0200 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | }); |