Problem/Motivation

Setup picker is executed multiple times in drupal behaviours which can cause picker markup to be broken.

Proposed resolution

Wrap the setup picker code in once condition so that markup is not duplicated.

Comments

saurabhpandit26 created an issue. See original summary.

saurabhpandit26’s picture

StatusFileSize
new759 bytes
nikunjkotecha’s picture

@Saurabh please raise MR so it is easy to review

+ stlib_picker.setupPicker(jQuery('#myPicker', context), drupalSettings.sharethis.service_string_markup, drupal_st.serviceCallback);

Here we can use "jQuery(this)" instead of jQuery('#myPicker', context) as it is already inside .once

nikunjkotecha’s picture

Status: Active » Needs work
saurabhpandit26’s picture

@Nikunj unable to create an issue fork for this, so uploading a new patch again.

saurabhpandit26’s picture

StatusFileSize
new735 bytes
saurabhpandit26’s picture

StatusFileSize
new771 bytes

Was getting undefined function error in patch #5

saurabhpandit26’s picture

StatusFileSize
new966 bytes

Refactored patch again to use jquery this object.

vivek panicker’s picture

Comments regarding the patch #8

diff --git a/js/sharethis.form.js b/js/sharethis.form.js
index 8b15b21..87efe49 100644
--- a/js/sharethis.form.js
+++ b/js/sharethis.form.js
@@ -3,7 +3,7 @@
  * This file contains most of the code for the configuration page.
  */
 
-(function ($, window, drupalSettings, stlib_picker) {
+ (function ($, window, drupalSettings, stlib_picker) {
 
   'use strict';
 
@@ -157,9 +157,11 @@
 
   Drupal.behaviors.shareThisForm = {
     attach: function (context) {
-      stlib_picker.setupPicker(jQuery('#myPicker', context), drupalSettings.sharethis.service_string_markup, drupal_st.serviceCallback);
-      drupal_st.addEvents();
-      drupal_st.setupServiceText();
+      jQuery('#myPicker', context).once('setup_picker_once').each(function () {
+        stlib_picker.setupPicker(jQuery(this), drupalSettings.sharethis.service_string_markup, drupal_st.serviceCallback);
+        drupal_st.addEvents();
+        drupal_st.setupServiceText();
+      });
     }
   };

1/ There seems to be an extra space before (function ($`.
2/ jQuery('#myPicker', context) can be jQuery('#myPicker', context)

nashkrammer’s picture

StatusFileSize
new743 bytes
new331 bytes

1/ There seems to be an extra space before (function ($`. Fixed
2/ jQuery('#myPicker', context) can be jQuery('#myPicker', context) any particular reason to have indent here.

vivek panicker’s picture

Sorry, my bad.
Copy + paste error.
What I meant was that can we use `$` instead of `jQuery` there?
jQuery('#myPicker', context) can be $('#myPicker', context)

nashkrammer’s picture