Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | ONOS GUI -- Widget -- Tooltip Service |
| 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
| 24 | // injected references |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 25 | var $log, fs; |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 26 | |
| 27 | // constants |
| 28 | var hoverHeight = 35, |
Bri Prebilic Cole | 9a5e006 | 2015-05-11 17:20:57 -0700 | [diff] [blame] | 29 | hoverDelay = 150, |
| 30 | exitDelay = 150; |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 31 | |
| 32 | // internal state |
| 33 | var tooltip, currElemId; |
| 34 | |
Bri Prebilic Cole | 6ffea6b | 2015-03-23 13:46:14 -0700 | [diff] [blame] | 35 | // === Helper functions --------------------------------------------- |
| 36 | |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 37 | function init() { |
| 38 | tooltip = d3.select('#tooltip'); |
| 39 | tooltip.html(''); |
| 40 | } |
| 41 | |
Bri Prebilic Cole | 6ffea6b | 2015-03-23 13:46:14 -0700 | [diff] [blame] | 42 | function tipStyle(mouseX, mouseY) { |
| 43 | var winWidth = fs.windowSize().width, |
| 44 | winHeight = fs.windowSize().height, |
| 45 | style = { |
Bri Prebilic Cole | f5e48b1 | 2015-04-21 14:52:36 -0700 | [diff] [blame] | 46 | display: 'inline-block', |
| 47 | left: 'auto', |
| 48 | right: 'auto' |
Bri Prebilic Cole | 6ffea6b | 2015-03-23 13:46:14 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | if (mouseX <= (winWidth / 2)) { |
| 52 | style.left = mouseX + 'px'; |
| 53 | } else { |
| 54 | style.right = (winWidth - mouseX) + 'px'; |
| 55 | } |
| 56 | |
| 57 | if (mouseY <= (winHeight / 2)) { |
| 58 | style.top = (mouseY + (hoverHeight - 10)) + 'px'; |
| 59 | } else { |
| 60 | style.top = (mouseY - hoverHeight) + 'px'; |
| 61 | } |
| 62 | |
| 63 | return style; |
| 64 | } |
| 65 | |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 66 | // === API functions ------------------------------------------------ |
| 67 | |
| 68 | function showTooltip(el, msg) { |
Simon Hunt | d552ee9 | 2015-04-02 17:06:35 -0700 | [diff] [blame] | 69 | // tooltips don't make sense on mobile devices |
| 70 | if (!el || !msg || fs.isMobile()) { |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 71 | return; |
| 72 | } |
Simon Hunt | d552ee9 | 2015-04-02 17:06:35 -0700 | [diff] [blame] | 73 | |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 74 | var elem = d3.select(el), |
| 75 | mouseX = d3.event.pageX, |
Bri Prebilic Cole | 6ffea6b | 2015-03-23 13:46:14 -0700 | [diff] [blame] | 76 | mouseY = d3.event.pageY, |
| 77 | style = tipStyle(mouseX, mouseY); |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 78 | currElemId = elem.attr('id'); |
| 79 | |
| 80 | tooltip.transition() |
| 81 | .delay(hoverDelay) |
| 82 | .each('start', function () { |
| 83 | d3.select(this).style('display', 'none'); |
| 84 | }) |
| 85 | .each('end', function () { |
Bri Prebilic Cole | 6ffea6b | 2015-03-23 13:46:14 -0700 | [diff] [blame] | 86 | d3.select(this).style(style) |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 87 | .text(msg); |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | function cancelTooltip(el) { |
| 92 | if (!el) { |
| 93 | return; |
| 94 | } |
| 95 | var elem = d3.select(el); |
| 96 | |
| 97 | if (elem.attr('id') === currElemId) { |
| 98 | tooltip.transition() |
| 99 | .delay(exitDelay) |
| 100 | .style({ |
| 101 | display: 'none' |
| 102 | }) |
| 103 | .text(''); |
| 104 | } |
| 105 | } |
| 106 | |
Bri Prebilic Cole | 8f07f0d | 2015-04-23 13:28:43 -0700 | [diff] [blame] | 107 | angular.module('onosWidget') |
| 108 | .factory('TooltipService', ['$log', 'FnService', |
| 109 | |
| 110 | function (_$log_, _fs_) { |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 111 | $log = _$log_; |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 112 | fs = _fs_; |
| 113 | |
| 114 | init(); |
| 115 | |
| 116 | return { |
| 117 | showTooltip: showTooltip, |
Bri Prebilic Cole | 9a5e006 | 2015-05-11 17:20:57 -0700 | [diff] [blame] | 118 | cancelTooltip: cancelTooltip |
Bri Prebilic Cole | 54d0938 | 2015-03-19 18:40:27 -0700 | [diff] [blame] | 119 | }; |
| 120 | }]); |
| 121 | }()); |