From ccabdeba1ecde9a99593a2deb3e1b0ae337b90d2 Mon Sep 17 00:00:00 2001 From: Elijah Madden Date: Thu, 24 Nov 2016 10:04:42 +0900 Subject: [PATCH 1/3] Initialize chosen when adding rows in repeatable subforms --- layouts/joomla/html/formbehavior/chosen.php | 23 ++++++++++++------- .../cms/html/JHtmlFormbehaviorTest.php | 2 +- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/layouts/joomla/html/formbehavior/chosen.php b/layouts/joomla/html/formbehavior/chosen.php index 71f14519e5407..0037745c8dac2 100644 --- a/layouts/joomla/html/formbehavior/chosen.php +++ b/layouts/joomla/html/formbehavior/chosen.php @@ -22,17 +22,24 @@ // Include jQuery JHtml::_('jquery.framework'); -JHtml::_('script', 'jui/chosen.jquery.min.js', array('version' => 'auto', 'relative' => true, 'detectDebug' => $debug)); -JHtml::_('stylesheet', 'jui/chosen.css', array('version' => 'auto', 'relative' => true)); +JHtml::_('script', 'jui/chosen.jquery.min.js', false, true, false, false, $debug); +JHtml::_('stylesheet', 'jui/chosen.css', false, true); // Options array to json options string $options_str = json_encode($options, ($debug && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false)); JFactory::getDocument()->addScriptDeclaration( - " - jQuery(document).ready(function (){ - jQuery('" . $selector . "').chosen(" . $options_str . "); - }); - " -); \ No newline at end of file + ' + jQuery(function ($) { + initChosen(); + $("body").on("subform-row-add", initChosen); + + function initChosen(event, container) + { + container = container || document; + $(container).find(' . json_encode($selector) . ').chosen(' . $options_str . '); + } + }); + ' +); diff --git a/tests/unit/suites/libraries/cms/html/JHtmlFormbehaviorTest.php b/tests/unit/suites/libraries/cms/html/JHtmlFormbehaviorTest.php index a231ee34e15c6..fb503608fd69d 100644 --- a/tests/unit/suites/libraries/cms/html/JHtmlFormbehaviorTest.php +++ b/tests/unit/suites/libraries/cms/html/JHtmlFormbehaviorTest.php @@ -100,7 +100,7 @@ public function testChosen() ); $this->assertContains( - "jQuery('testSelect').chosen", + '$(container).find("testSelect").chosen', $document->_script['text/javascript'], 'Verify that the Chosen JS is initialised with the supplied selector' ); From 763a1bfc48eb7d433176a5770f5bdae62c174ca9 Mon Sep 17 00:00:00 2001 From: Elijah Madden Date: Thu, 24 Nov 2016 10:14:03 +0900 Subject: [PATCH 2/3] Use JHtml::script and JHtml::stylesheet in the new way --- layouts/joomla/html/formbehavior/chosen.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/layouts/joomla/html/formbehavior/chosen.php b/layouts/joomla/html/formbehavior/chosen.php index 0037745c8dac2..3c35941512a49 100644 --- a/layouts/joomla/html/formbehavior/chosen.php +++ b/layouts/joomla/html/formbehavior/chosen.php @@ -22,13 +22,12 @@ // Include jQuery JHtml::_('jquery.framework'); -JHtml::_('script', 'jui/chosen.jquery.min.js', false, true, false, false, $debug); -JHtml::_('stylesheet', 'jui/chosen.css', false, true); +JHtml::_('script', 'jui/chosen.jquery.min.js', array('version' => 'auto', 'relative' => true, 'detectDebug' => $debug)); +JHtml::_('stylesheet', 'jui/chosen.css', array('version' => 'auto', 'relative' => true)); // Options array to json options string $options_str = json_encode($options, ($debug && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false)); - JFactory::getDocument()->addScriptDeclaration( ' jQuery(function ($) { From 3c295334a667b55c3393782ca76777a7b22b2d59 Mon Sep 17 00:00:00 2001 From: Elijah Madden Date: Thu, 24 Nov 2016 10:24:33 +0900 Subject: [PATCH 3/3] Subform does not need to fix chosen. --- media/system/js/subform-repeatable-uncompressed.js | 5 ----- media/system/js/subform-repeatable.js | 3 ++- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/media/system/js/subform-repeatable-uncompressed.js b/media/system/js/subform-repeatable-uncompressed.js index a778652b1c2fb..13eafb112a74b 100644 --- a/media/system/js/subform-repeatable-uncompressed.js +++ b/media/system/js/subform-repeatable-uncompressed.js @@ -226,11 +226,6 @@ // method for hack the scripts that can be related // to the one of field that in given $row $.subformRepeatable.prototype.fixScripts = function($row){ - // init chosen if any - if($.fn.chosen){ - $row.find('select.advancedSelect').chosen(); - } - //color picker $row.find('.minicolors').each(function() { var $el = $(this); diff --git a/media/system/js/subform-repeatable.js b/media/system/js/subform-repeatable.js index a5f1ab8a7191f..78f1be6628a68 100644 --- a/media/system/js/subform-repeatable.js +++ b/media/system/js/subform-repeatable.js @@ -1 +1,2 @@ -(function(e){"use strict";e.subformRepeatable=function(t,n){this.$container=e(t);if(this.$container.data("subformRepeatable"))return r;this.$container.data("subformRepeatable",r),this.options=e.extend({},e.subformRepeatable.defaults,n),this.template="",this.prepareTemplate(),this.$containerRows=this.options.rowsContainer?this.$container.find(this.options.rowsContainer):this.$container,this.lastRowNum=this.$containerRows.find(this.options.repeatableElement).length;var r=this;this.$container.on("click",this.options.btAdd,function(t){t.preventDefault();var n=e(this).parents(r.options.repeatableElement);n.length||(n=null),r.addRow(n)}),this.$container.on("click",this.options.btRemove,function(t){t.preventDefault();var n=e(this).parents(r.options.repeatableElement);r.removeRow(n)}),this.options.btMove&&this.$containerRows.sortable({items:this.options.repeatableElement,handle:this.options.btMove,tolerance:"pointer"}),this.$container.trigger("subform-ready")},e.subformRepeatable.prototype.prepareTemplate=function(){if(this.options.rowTemplateSelector){var t=this.$container.find(this.options.rowTemplateSelector)[0]||{};this.template=e.trim(t.text||t.textContent)}else{var n=this.$container.find(this.options.repeatableElement).get(0),r=e(n).clone();try{this.clearScripts(r)}catch(i){window.console&&console.log(i)}this.template=r.prop("outerHTML")}},e.subformRepeatable.prototype.addRow=function(t){var n=this.$containerRows.find(this.options.repeatableElement).length;if(n>=this.options.maximum)return null;var r=e.parseHTML(this.template);t?e(t).after(r):this.$containerRows.append(r);var i=e(r);i.attr("data-new","true"),this.fixUniqueAttributes(i,n);try{this.fixScripts(i)}catch(s){window.console&&console.log(s)}return this.$container.trigger("subform-row-add",i),i},e.subformRepeatable.prototype.removeRow=function(e){var t=this.$containerRows.find(this.options.repeatableElement).length;if(t<=this.options.minimum)return;this.$container.trigger("subform-row-remove",e),e.remove()},e.subformRepeatable.prototype.fixUniqueAttributes=function(t,n){this.lastRowNum++;var r=t.attr("data-group"),i=t.attr("data-base-name"),n=n||0,s=Math.max(this.lastRowNum,n+1),o=i+s;this.lastRowNum=s,t.attr("data-group",o);var u=t.find("*[name]"),a={};for(var f=0,l=u.length;f=this.options.maximum)return null;var i=t.parseHTML(this.template);e?t(e).after(i):this.$containerRows.append(i);var a=t(i);a.attr("data-new","true"),this.fixUniqueAttributes(a,o);try{this.fixScripts(a)}catch(t){window.console&&console.log(t)}return this.$container.trigger("subform-row-add",a),a},t.subformRepeatable.prototype.removeRow=function(t){var e=this.$containerRows.find(this.options.repeatableElement).length;e<=this.options.minimum||(this.$container.trigger("subform-row-remove",t),t.remove())},t.subformRepeatable.prototype.fixUniqueAttributes=function(e,o){this.lastRowNum++;var i=e.attr("data-group"),a=e.attr("data-base-name"),o=o||0,r=Math.max(this.lastRowNum,o+1),n=a+r;this.lastRowNum=r,e.attr("data-group",n);for(var s=e.find("*[name]"),l={},p=0,f=s.length;p