summaryrefslogtreecommitdiffstats
path: root/polygerrit-ui/app/elements/plugins/gr-dom-hooks/gr-dom-hooks.js
blob: 230be0e03a35c26347b2d2131c54dd014089a03e (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
 * @license
 * Copyright (C) 2017 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 GrDomHooksManager(plugin) {
    this._plugin = plugin;
    this._hooks = {};
  }

  GrDomHooksManager.prototype._getHookName = function(endpointName,
      opt_moduleName) {
    if (opt_moduleName) {
      return endpointName + ' ' + opt_moduleName;
    } else {
      return this._plugin.getPluginName() + '-autogenerated-' + endpointName;
    }
  };

  GrDomHooksManager.prototype.getDomHook = function(endpointName,
      opt_moduleName) {
    const hookName = this._getHookName(endpointName, opt_moduleName);
    if (!this._hooks[hookName]) {
      this._hooks[hookName] = new GrDomHook(hookName, opt_moduleName);
    }
    return this._hooks[hookName];
  };

  function GrDomHook(hookName, opt_moduleName) {
    this._instances = [];
    this._callbacks = [];
    if (opt_moduleName) {
      this._moduleName = opt_moduleName;
    } else {
      this._moduleName = hookName;
      this._createPlaceholder(hookName);
    }
  }

  GrDomHook.prototype._createPlaceholder = function(hookName) {
    Polymer({
      is: hookName,
      properties: {
        plugin: Object,
        content: Object,
      },
    });
  };

  GrDomHook.prototype.handleInstanceDetached = function(instance) {
    const index = this._instances.indexOf(instance);
    if (index !== -1) {
      this._instances.splice(index, 1);
    }
  };

  GrDomHook.prototype.handleInstanceAttached = function(instance) {
    this._instances.push(instance);
    this._callbacks.forEach(callback => callback(instance));
  };

  /**
   * Get instance of last DOM hook element attached into the endpoint.
   * Returns a Promise, that's resolved when attachment is done.
   * @return {!Promise<!Element>}
   */
  GrDomHook.prototype.getLastAttached = function() {
    if (this._instances.length) {
      return Promise.resolve(this._instances.slice(-1)[0]);
    }
    if (!this._lastAttachedPromise) {
      let resolve;
      const promise = new Promise(r => resolve = r);
      this._callbacks.push(resolve);
      this._lastAttachedPromise = promise.then(element => {
        this._lastAttachedPromise = null;
        const index = this._callbacks.indexOf(resolve);
        if (index !== -1) {
          this._callbacks.splice(index, 1);
        }
        return element;
      });
    }
    return this._lastAttachedPromise;
  };

  /**
   * Get all DOM hook elements.
   */
  GrDomHook.prototype.getAllAttached = function() {
    return this._instances;
  };

  /**
   * Install a new callback to invoke when a new instance of DOM hook element
   * is attached.
   * @param {function(Element)} callback
   */
  GrDomHook.prototype.onAttached = function(callback) {
    this._callbacks.push(callback);
    return this;
  };

  /**
   * Name of DOM hook element that will be installed into the endpoint.
   */
  GrDomHook.prototype.getModuleName = function() {
    return this._moduleName;
  };

  GrDomHook.prototype.getPublicAPI = function() {
    const result = {};
    const exposedMethods = [
      'onAttached', 'getLastAttached', 'getAllAttached', 'getModuleName',
    ];
    for (const p of exposedMethods) {
      result[p] = this[p].bind(this);
    }
    return result;
  };

  window.GrDomHook = GrDomHook;
  window.GrDomHooksManager = GrDomHooksManager;
})(window);