|
13 | 13 | */
|
14 | 14 | ko.bindingHandlers.popover = {
|
15 | 15 |
|
16 |
| - init: function (element, valueAccessor) { |
| 16 | + init: function(element, valueAccessor) { |
17 | 17 | ko.bindingHandlers.popover.initPopover(element, valueAccessor);
|
18 | 18 | },
|
19 |
| - update: function (element, valueAccessor) { |
| 19 | + update: function(element, valueAccessor) { |
| 20 | + var $element = $(element), |
| 21 | + instance = $element.data('bs.popover'), |
| 22 | + popOptions = ko.bindingHandlers.popover.getOptions(valueAccessor), |
| 23 | + combinedOptions = popOptions.combinedOptions, |
| 24 | + options = popOptions.options; |
| 25 | + |
| 26 | + if (!instance) { |
| 27 | + ko.bindingHandlers.popover.initPopover(element, valueAccessor); |
| 28 | + instance = $element.data('bs.popover'); |
| 29 | + } |
| 30 | + |
| 31 | + if (!instance) |
| 32 | + return; |
| 33 | + |
| 34 | + // if view model has changed, update the popover |
| 35 | + instance.config.title = combinedOptions.title || ""; |
| 36 | + instance.config.content = combinedOptions.content; |
20 | 37 |
|
21 |
| - var $element = $(element); |
22 |
| - $element.popover('dispose'); |
23 |
| - var options = ko.bindingHandlers.popover.initPopover(element, valueAccessor); |
24 | 38 | if (options.autoShow) {
|
25 | 39 | if ($element.data('firstPopover') === false) {
|
26 |
| - $element.popover('show'); |
27 |
| - $('body').on('click', function (e) { |
28 |
| - |
| 40 | + instance.show(); |
| 41 | + $('body').on('click', function(e) { |
29 | 42 | if (e.target != element && $element.find(e.target).length == 0) {
|
30 |
| - $element.popover('dispose'); |
| 43 | + instance.hide(); |
31 | 44 | }
|
32 | 45 | });
|
33 | 46 | }
|
| 47 | + |
34 | 48 | $element.data('firstPopover', false);
|
35 | 49 | }
|
36 | 50 |
|
| 51 | + // refresh popover content |
| 52 | + if(ko.bindingHandlers.popover.isPopoverShown(element)) { |
| 53 | + instance.show(); |
| 54 | + } |
37 | 55 | },
|
38 | 56 |
|
39 | 57 | defaultOptions: {
|
40 | 58 | placement: "right",
|
41 | 59 | animation: true,
|
42 | 60 | html: true,
|
43 |
| - trigger: "hover", |
44 |
| - delay: { |
45 |
| - show: 250 |
46 |
| - } |
| 61 | + trigger: "hover" |
47 | 62 | },
|
48 | 63 |
|
49 |
| - initPopover: function (element, valueAccessor) { |
| 64 | + initPopover: function(element, valueAccessor) { |
| 65 | + var popOptions = ko.bindingHandlers.popover.getOptions(valueAccessor), |
| 66 | + options = popOptions.options, |
| 67 | + combinedOptions = popOptions.combinedOptions; |
| 68 | + $(element).popover(combinedOptions); |
| 69 | + ko.utils.domNodeDisposal.addDisposeCallback(element, function() { |
| 70 | + $(element).popover("dispose"); |
| 71 | + }); |
| 72 | + |
| 73 | + return options; |
| 74 | + }, |
| 75 | + /** |
| 76 | + * constructs the options object from valueAccessor |
| 77 | + * @param valueAccessor |
| 78 | + * @returns {{combinedOptions: any, options: any}} |
| 79 | + */ |
| 80 | + getOptions: function(valueAccessor) { |
50 | 81 | var options = ko.utils.unwrapObservable(valueAccessor());
|
| 82 | + if (typeof(options.content) === "undefined") { |
| 83 | + options.content = "" |
| 84 | + } |
51 | 85 |
|
52 | 86 | var combinedOptions = ko.utils.extend({}, ko.bindingHandlers.popover.defaultOptions);
|
53 | 87 | var content = ko.utils.unwrapObservable(options.content);
|
54 | 88 | ko.utils.extend(combinedOptions, options);
|
55 | 89 | combinedOptions.description = content;
|
56 |
| - |
57 |
| - $(element).popover(combinedOptions); |
58 |
| - |
59 |
| - ko.utils.domNodeDisposal.addDisposeCallback(element, function () { |
60 |
| - $(element).popover("dispose"); |
61 |
| - }); |
62 |
| - return options; |
| 90 | + return {combinedOptions: combinedOptions, options: options}; |
| 91 | + }, |
| 92 | + /** |
| 93 | + * id of the popover is stored in the element's aria-describedby attribute |
| 94 | + * @param element |
| 95 | + * @returns {boolean} |
| 96 | + */ |
| 97 | + isPopoverShown: function isPopoverShown(element) { |
| 98 | + const popoverId = $(element).attr("aria-describedby"); |
| 99 | + return $("#" + popoverId).length > 0; |
63 | 100 | }
|
64 | 101 | };
|
65 | 102 |
|
|
0 commit comments