|
| 1 | +/* |
| 2 | +Copyright 2015 OpenMarket Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | + |
| 18 | +'use strict'; |
| 19 | + |
| 20 | +var React = require('react'); |
| 21 | +var q = require('q'); |
| 22 | + |
| 23 | +// Shamelessly ripped off Modal.js. There's probably a better way |
| 24 | +// of doing reusable widgets like dialog boxes & menus where we go and |
| 25 | +// pass in a custom control as the actual body. |
| 26 | + |
| 27 | +module.exports = { |
| 28 | + ContextualMenuContainerId: "mx_ContextualMenu_Container", |
| 29 | + |
| 30 | + getOrCreateContainer: function() { |
| 31 | + var container = document.getElementById(this.ContextualMenuContainerId); |
| 32 | + |
| 33 | + if (!container) { |
| 34 | + container = document.createElement("div"); |
| 35 | + container.id = this.ContextualMenuContainerId; |
| 36 | + document.body.appendChild(container); |
| 37 | + } |
| 38 | + |
| 39 | + return container; |
| 40 | + }, |
| 41 | + |
| 42 | + createMenu: function (Element, props) { |
| 43 | + var self = this; |
| 44 | + |
| 45 | + var closeMenu = function() { |
| 46 | + React.unmountComponentAtNode(self.getOrCreateContainer()); |
| 47 | + |
| 48 | + if (props && props.onFinished) props.onFinished.apply(null, arguments); |
| 49 | + }; |
| 50 | + |
| 51 | + var position = { |
| 52 | + top: props.top - 20, |
| 53 | + right: props.right + 8, |
| 54 | + }; |
| 55 | + |
| 56 | + // FIXME: If a menu uses getDefaultProps it clobbers the onFinished |
| 57 | + // property set here so you can't close the menu from a button click! |
| 58 | + var menu = ( |
| 59 | + <div className="mx_ContextualMenu_wrapper"> |
| 60 | + <div className="mx_ContextualMenu" style={position}> |
| 61 | + <img className="mx_ContextualMenu_chevron" src="img/chevron-right.png" width="9" height="16" /> |
| 62 | + <Element {...props} onFinished={closeMenu}/> |
| 63 | + </div> |
| 64 | + <div className="mx_ContextualMenu_background" onClick={closeMenu}></div> |
| 65 | + </div> |
| 66 | + ); |
| 67 | + |
| 68 | + React.render(menu, this.getOrCreateContainer()); |
| 69 | + |
| 70 | + return {close: closeMenu}; |
| 71 | + }, |
| 72 | +}; |
0 commit comments