Skip to content

Commit def707f

Browse files
authored
Merge pull request #3412 from AtlasOfLivingAustralia/feature/issue269
AtlasOfLivingAustralia/ecodata-client-plugin#269
2 parents 1728e83 + b0ef778 commit def707f

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

grails-app/assets/javascripts/knockout-custom-bindings.js

+51-15
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,41 @@ ko.bindingHandlers.popover = {
1212
ko.bindingHandlers.popover.initPopover(element, valueAccessor);
1313
},
1414
update: function(element, valueAccessor) {
15+
var $element = $(element),
16+
instance = $element.data('bs.popover'),
17+
popOptions = ko.bindingHandlers.popover.getOptions(valueAccessor),
18+
combinedOptions = popOptions.combinedOptions,
19+
options = popOptions.options;
20+
21+
if (!instance) {
22+
ko.bindingHandlers.popover.initPopover(element, valueAccessor);
23+
instance = $element.data('bs.popover');
24+
}
25+
26+
if (!instance)
27+
return;
28+
29+
// if view model has changed, update the popover
30+
instance.config.title = combinedOptions.title || "";
31+
instance.config.content = combinedOptions.content;
1532

16-
var $element = $(element);
17-
$element.popover('dispose');
18-
var options = ko.bindingHandlers.popover.initPopover(element, valueAccessor);
1933
if (options.autoShow) {
2034
if ($element.data('firstPopover') === false) {
21-
$element.popover('show');
35+
instance.show();
2236
$('body').on('click', function(e) {
23-
2437
if (e.target != element && $element.find(e.target).length == 0) {
25-
$element.popover('dispose');
38+
instance.hide();
2639
}
2740
});
2841
}
42+
2943
$element.data('firstPopover', false);
3044
}
3145

46+
// refresh popover content
47+
if(ko.bindingHandlers.popover.isPopoverShown(element)) {
48+
instance.show();
49+
}
3250
},
3351

3452
defaultOptions: {
@@ -39,23 +57,41 @@ ko.bindingHandlers.popover = {
3957
},
4058

4159
initPopover: function(element, valueAccessor) {
42-
var options = ko.utils.unwrapObservable(valueAccessor());
60+
var popOptions = ko.bindingHandlers.popover.getOptions(valueAccessor),
61+
options = popOptions.options,
62+
combinedOptions = popOptions.combinedOptions;
63+
$(element).popover(combinedOptions);
64+
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
65+
$(element).popover("dispose");
66+
});
4367

44-
if (typeof(options.content) === "undefined"){
68+
return options;
69+
},
70+
/**
71+
* constructs the options object from valueAccessor
72+
* @param valueAccessor
73+
* @returns {{combinedOptions: any, options: any}}
74+
*/
75+
getOptions: function(valueAccessor) {
76+
var options = ko.utils.unwrapObservable(valueAccessor());
77+
if (typeof(options.content) === "undefined") {
4578
options.content = ""
4679
}
4780

4881
var combinedOptions = ko.utils.extend({}, ko.bindingHandlers.popover.defaultOptions);
4982
var content = ko.utils.unwrapObservable(options.content);
5083
ko.utils.extend(combinedOptions, options);
5184
combinedOptions.description = content;
52-
53-
$(element).popover(combinedOptions);
54-
55-
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
56-
$(element).popover("dispose");
57-
});
58-
return options;
85+
return {combinedOptions: combinedOptions, options: options};
86+
},
87+
/**
88+
* id of the popover is stored in the element's aria-describedby attribute
89+
* @param element
90+
* @returns {boolean}
91+
*/
92+
isPopoverShown: function isPopoverShown(element) {
93+
const popoverId = $(element).attr("aria-describedby");
94+
return $("#" + popoverId).length > 0;
5995
}
6096
};
6197

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<div data-bind="with:${bindingProperty}" class="input-group species-select">
22
<select class="form-control form-control-sm" data-bind="speciesSelect2:$data"></select>
33
<div class="input-group-append">
4-
<span class="input-group-text" data-bind="visible:name(), popover: {title: transients.speciesTitle, content: transients.speciesInformation}"><i class="fa fa-info-circle"></i></span>
4+
<span class="input-group-text" data-bind="visible:name(), popover: {title: transients.speciesTitle, content: transients.speciesInformation}, event: { 'shown.bs.popover': fetchSpeciesImage}"><i class="fa fa-info-circle"></i></span>
55
</div>
66
</div>

0 commit comments

Comments
 (0)