| lombardof | fb37bca | 2018-05-03 16:20:04 +0200 | [diff] [blame] | 1 | (function (root, factory) { |
| 2 | if (typeof module === 'object' && module.exports) { |
| 3 | module.exports = function (d3) { |
| 4 | d3.contextMenu = factory(d3); |
| 5 | return d3.contextMenu; |
| 6 | }; |
| 7 | } else if (typeof define === 'function' && define.amd) { |
| 8 | try { |
| 9 | var d3 = require('d3'); |
| 10 | } catch (e) { |
| 11 | d3 = root.d3; |
| 12 | } |
| 13 | |
| 14 | d3.contextMenu = factory(d3); |
| 15 | define([], function () { |
| 16 | return d3.contextMenu; |
| 17 | }); |
| 18 | } else if (root.d3) { |
| 19 | root.d3.contextMenu = factory(root.d3); |
| 20 | } |
| 21 | }(this, |
| 22 | function (d3) { |
| 23 | return function (menu, opts) { |
| 24 | var openCallback, |
| 25 | closeCallback; |
| 26 | |
| 27 | if (typeof opts === 'function') { |
| 28 | openCallback = opts; |
| 29 | } else { |
| 30 | opts = opts || {}; |
| 31 | openCallback = opts.onOpen; |
| 32 | closeCallback = opts.onClose; |
| 33 | } |
| 34 | |
| 35 | // create the div element that will hold the context menu |
| 36 | d3.selectAll('.d3-context-menu').data([1]) |
| 37 | .enter() |
| 38 | .append('div') |
| 39 | .attr('class', 'd3-context-menu'); |
| 40 | |
| 41 | // close menu |
| 42 | d3.select('body').on('click.d3-context-menu', function () { |
| 43 | d3.select('.d3-context-menu').style('display', 'none'); |
| 44 | if (closeCallback) { |
| 45 | closeCallback(); |
| 46 | } |
| 47 | }); |
| 48 | |
| 49 | // this gets executed when a contextmenu event occurs |
| 50 | return function (data, index) { |
| 51 | var elm = this; |
| 52 | d3.selectAll('.d3-context-menu').html(''); |
| 53 | var list = d3.selectAll('.d3-context-menu') |
| 54 | .on('contextmenu', function (d) { |
| 55 | d3.select('.d3-context-menu').style('display', 'none'); |
| 56 | d3.event.preventDefault(); |
| 57 | d3.event.stopPropagation(); |
| 58 | }) |
| 59 | .append('ul'); |
| 60 | list.selectAll('li').data(typeof menu === 'function' ? menu(data) : menu).enter() |
| 61 | .filter(function (d) { |
| 62 | if(opts.type_object == 'node'){ |
| 63 | if (opts.edit_mode || opts.edit_mode == d.edit_mode ) { |
| 64 | if ((d.nodes == undefined || d.nodes.length == 0) || |
| 65 | (d.nodes != undefined && d.nodes.length > 0 && d.nodes.indexOf(data.info.type) > -1)) |
| 66 | return true |
| 67 | } |
| 68 | } |
| 69 | if(opts.type_object == 'link'){ |
| 70 | if (opts.edit_mode == d.edit_mode) { |
| 71 | |
| 72 | return true |
| 73 | } |
| 74 | } |
| 75 | return false; |
| 76 | }) |
| 77 | .append('li') |
| 78 | .attr('class', function (d) { |
| 79 | var ret = ''; |
| 80 | if (d.divider) { |
| 81 | ret += ' is-divider'; |
| 82 | } |
| 83 | if (d.disabled) { |
| 84 | ret += ' is-disabled'; |
| 85 | } |
| 86 | if (!d.action) { |
| 87 | ret += ' is-header'; |
| 88 | } |
| 89 | //if() |
| 90 | return ret; |
| 91 | }) |
| 92 | .html(function (d) { |
| 93 | if (d.divider) { |
| 94 | return '<hr>'; |
| 95 | } |
| 96 | if (!d.title) { |
| 97 | console.error('No title attribute set. Check the spelling of your options.'); |
| 98 | } |
| 99 | return (typeof d.title === 'string') ? d.title : d.title(data); |
| 100 | }) |
| 101 | .on('click', function (d, i) { |
| 102 | if (d.disabled) return; // do nothing if disabled |
| 103 | if (!d.action) return; // headers have no "action" |
| 104 | d.action(elm, data, index); |
| 105 | d3.select('.d3-context-menu').style('display', 'none'); |
| 106 | |
| 107 | if (closeCallback) { |
| 108 | closeCallback(); |
| 109 | } |
| 110 | }); |
| 111 | |
| 112 | // the openCallback allows an action to fire before the menu is displayed |
| 113 | // an example usage would be closing a tooltip |
| 114 | if (openCallback) { |
| 115 | if (openCallback(data, index) === false) { |
| 116 | return; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | // display context menu |
| 121 | d3.select('.d3-context-menu') |
| 122 | .style('left', (d3.event.pageX - 2) + 'px') |
| 123 | .style('top', (d3.event.pageY - 2) + 'px') |
| 124 | .style('display', 'block'); |
| 125 | |
| 126 | d3.event.preventDefault(); |
| 127 | d3.event.stopPropagation(); |
| 128 | }; |
| 129 | }; |
| 130 | } |
| 131 | )); |