summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-plugin-action-context.js
blob: 5ac8773205524301f6a26a7af909d43d64fc7b7f (plain)
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
/**
 * @license
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
(function(window) {
  'use strict';

  function GrPluginActionContext(plugin, action, change, revision) {
    this.action = action;
    this.plugin = plugin;
    this.change = change;
    this.revision = revision;
    this._popups = [];
  }

  GrPluginActionContext.prototype.popup = function(element) {
    this._popups.push(this.plugin.deprecated.popup(element));
  };

  GrPluginActionContext.prototype.hide = function() {
    for (const popupApi of this._popups) {
      popupApi.close();
    }
    this._popups.splice(0);
  };

  GrPluginActionContext.prototype.refresh = function() {
    window.location.reload();
  };

  GrPluginActionContext.prototype.textfield = function() {
    return document.createElement('paper-input');
  };

  GrPluginActionContext.prototype.br = function() {
    return document.createElement('br');
  };

  GrPluginActionContext.prototype.msg = function(text) {
    const label = document.createElement('gr-label');
    Polymer.dom(label).appendChild(document.createTextNode(text));
    return label;
  };

  GrPluginActionContext.prototype.div = function(...els) {
    const div = document.createElement('div');
    for (const el of els) {
      Polymer.dom(div).appendChild(el);
    }
    return div;
  };

  GrPluginActionContext.prototype.button = function(label, callbacks) {
    const onClick = callbacks && callbacks.onclick;
    const button = document.createElement('gr-button');
    Polymer.dom(button).appendChild(document.createTextNode(label));
    if (onClick) {
      this.plugin.eventHelper(button).onTap(onClick);
    }
    return button;
  };

  GrPluginActionContext.prototype.checkbox = function() {
    const checkbox = document.createElement('input');
    checkbox.type = 'checkbox';
    return checkbox;
  };

  GrPluginActionContext.prototype.label = function(checkbox, title) {
    return this.div(checkbox, this.msg(title));
  };

  GrPluginActionContext.prototype.prependLabel = function(title, checkbox) {
    return this.label(checkbox, title);
  };

  GrPluginActionContext.prototype.call = function(payload, onSuccess) {
    if (!this.action.__url) {
      console.warn(`Unable to ${this.action.method} to ${this.action.__key}!`);
      return;
    }
    this.plugin.restApi()
        .send(this.action.method, this.action.__url, payload)
        .then(onSuccess)
        .catch(error => {
          document.dispatchEvent(new CustomEvent('show-alert', {
            detail: {
              message: `Plugin network error: ${error}`,
            },
          }));
        });
  };

  window.GrPluginActionContext = GrPluginActionContext;
})(window);