Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
When displaying an addthis in e.g. a dialog, it is not automatically loaded (except when the addthis library is added the first time).
Proposed resolution
Add a behavior that checks if the newly loaded context has any .addthis_toolbar, then load it with addthis.toolbox('.addthis_toolbox');.
Avoid to be called the first time the behavior is called, I think you can check for that with context == document, since it happens automatically then. Might not be worth it, though.
Remaining tasks
User interface changes
API changes
Data model changes
Comment | File | Size | Author |
---|---|---|---|
#2 | add_js_behavior_to-2557075-2.patch | 1.17 KB | sasanikolic |
| |||
#6 | add_js_behavior_to-2557075-6.patch | 1.25 KB | sasanikolic |
| |||
#6 | add_js_behavior_to-2557075-6-interdiff.txt | 1.31 KB | sasanikolic |
#8 | add_js_behavior_to-2557075-8.patch | 1.21 KB | sasanikolic |
| |||
#14 | interdiff-2557075-13-14.txt | 1.94 KB | pivica |
Comments
Comment #2
sasanikolic CreditAttribution: sasanikolic at MD Systems GmbH commentedComment #3
miro_dietiker80 col wrapping. :-)
adthis => addthis... nice typo!
sharemessage => ShareMessage
And similar with the other line.
That's really an ugly race condition problem/ workaround with this polling. We should try to avoid this kind of dirty fix as much as possible.
Is there no other way?
If not, note that this Interval triggering is also fired and eating up browser performance for the non-ajax cases. And in case .addthis_toolbox isn't found, it keeps firing forever... At least fix the checks and sequence.
Comment #4
pivica CreditAttribution: pivica at MD Systems GmbH commentedIn core/misc/dialog.js in openDialog() method code is triggering couple of events, the one we probably can reuse are dialog:beforecreate and dialog:aftercreate. So the code can look like this
Not sure which event is better to use, probably both are OK.
Comment #5
BerdirThat works with dialog, but what about other ajax updates, for example some views endless scroll thing with share messages that need to be loaded/added?
Comment #6
sasanikolic CreditAttribution: sasanikolic at MD Systems GmbH commentedTemporary fix until we find a better solution...
Comment #7
pivica CreditAttribution: pivica at MD Systems GmbH commentedOK I misunderstood original problem which is actually a case that we can load addthis js dynamically later on some ajax event and then we need to execute it when actual js is loaded.
So addthis API is triggering event when it is ready (http://support.addthis.com/customer/portal/articles/1365497-addthis-java...), and it looks like this
And maybe we should use this also, but this again needs global addthis var to be initialized.
I have checked quickly addthis js code but could not found any place where addthis is triggering some event on load.
There is a way to detect when some js code is loaded, for example like explained on http://stackoverflow.com/questions/8718303/javascript-to-detect-when-ext..., but two problems here
1. Drupal is controling loading of JS code so not sure can we use this at all
2. Then not sure how much browsers support this script load event
So on the end using interval is not that bad but as Miro already said we should first check is addthis exist and if not then start that interval.
Comment #8
sasanikolic CreditAttribution: sasanikolic at MD Systems GmbH commentedOk, moved also the check for the addthis up.
Comment #9
miro_dietikerStill needs to be outside the .each().
Comment #10
sasanikolic CreditAttribution: sasanikolic at MD Systems GmbH commentedMoved...
Comment #11
pivica CreditAttribution: pivica at MD Systems GmbH commentedEmpty comment line? This should be just empty line.
Missing empty like after "use strict" and Drupal.behaviors.sharemessage should have doc comment.
What is this addthis doing here?
Hmm maybe I am missing something obvious here but as I see it we should do two separate if here.
First if should check if there is some element in context which has addthis_toolbox class. If there are no such elements we don't have nothing to do. But if there is such a element we then continue to second if.
Second if should then check if window.addthis exist, if it exist then we can call toolbox for that element. If it does not exist then we can use setInterval approach and wait that addthis global is initialized.
Also current if is not OK for a case when addthis_toolbox elements exist but addthis is undefined - basically you will never enter inside and get to the setInterval code.
Space is missing after function().
Here again we are doing $(context).find('.addthis_toolbox') like in outer if, not optimal.
No need to write window.addthis.toolbox, global vars you can always access directly in any scope, so addthis.toolbox is enough.
Not sure currently where this call should be but we should call clearInterval(interval) only when we detect that addthis is defined.
Because we are using Drupal global var inside you should add it here also in call and before in function definition, something like
(function ($, Drupal) {
// ....
})(jQuery, Drupal);
Comment #12
pivica CreditAttribution: pivica at MD Systems GmbH commentedCan't we just say here
addthis.toolbox(this);
When you use class selector I guess addthis will first select all elements on page, and we should limit this to current context, and then try to init toolbox on them.
Check API doc on http://support.addthis.com/customer/portal/articles/1365325-rendering-to... which says that for target(s) we can also use HTML element references.
Comment #13
sasanikolic CreditAttribution: sasanikolic at MD Systems GmbH commentedMade the changes for the comments above. I am only not sure about how to change the $(context).find('.addthis_toolbox') for comment #6. Any suggestion?
Comment #14
pivica CreditAttribution: pivica at MD Systems GmbH commentedNo point 4. i was thinking something else. OK maybe it will be faster to create a patch ;)
Didn't test this at all just rewrite it how i guess it should work.
@sasanikolic can you do full test of this and check that everything works properly?
Comment #15
miro_dietikerNow it looks pretty solid. ;-)
Comment #16
sasanikolic CreditAttribution: sasanikolic at MD Systems GmbH commentedTested with IE, Firefox, Chrome and Safari. Works fine. I think we're good to go with this finally. :)
Comment #17
BerdirGreat, committed.