github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/asset_client/src/components/modals.js (about) 1 /** 2 * Copyright 2017 Intel Corporation 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 'use strict' 18 19 const m = require('mithril') 20 const _ = require('lodash') 21 const $ = require('jquery') 22 23 /** 24 * A basic Bootstrap modal. Requires at least a title and body be set in 25 * attributes. Also accepts text and functions for accepting/canceling. 26 */ 27 const BasicModal = { 28 view (vnode) { 29 // Set default accept/cancel values 30 const acceptText = vnode.attrs.acceptText || 'Accept' 31 const cancelText = vnode.attrs.cancelText || 'Cancel' 32 const acceptFn = vnode.attrs.acceptFn || _.identity 33 const cancelFn = vnode.attrs.cancelFn || _.identity 34 35 return m('.modal.fade#modal', { 36 tabindex: '-1', 37 role: 'dialog', 38 'aria-labelby': 'modal' 39 }, [ 40 m('.modal-dialog', { role: 'document' }, 41 m('form', 42 m('.modal-content', 43 m('.modal-header', 44 m('h5.modal-title', vnode.attrs.title), 45 m('button.close', { 46 type: 'button', 47 onclick: cancelFn, 48 'data-dismiss': 'modal', 49 'aria-label': 'Close' 50 }, m('span', { 'aria-hidden': 'true' }, m.trust('×'))) 51 ), 52 m('.modal-body', vnode.attrs.body), 53 m('.modal-footer', 54 m('button.btn.btn-secondary', { 55 type: 'button', 56 onclick: cancelFn, 57 'data-dismiss': 'modal' 58 }, cancelText), 59 m('button.btn.btn-primary', { 60 type: 'submit', 61 onclick: acceptFn, 62 'data-dismiss': 'modal' 63 }, acceptText))))) 64 ]) 65 } 66 } 67 68 /** 69 * Renders/shows a modal component, with attributes, returning a promise. 70 * On close, unmounts the component and resolves/rejects the promise, 71 * with rejection indicating the cancel button was pressed. 72 */ 73 const show = (modal, attrs, children) => { 74 let acceptFn = null 75 let cancelFn = null 76 const onClosePromise = new Promise((resolve, reject) => { 77 acceptFn = resolve 78 cancelFn = reject 79 }) 80 81 const container = document.getElementById('modal-container') 82 m.render(container, 83 m(modal, _.assign(attrs, { acceptFn, cancelFn }, children))) 84 const $modal = $('#modal') 85 $modal.on('hidden.bs.modal', () => m.mount(container, null)) 86 $modal.modal('show') 87 88 return onClosePromise 89 } 90 91 module.exports = { 92 BasicModal, 93 show 94 }