From 09a0bb89b8754506f896ba35b0cbb9f471a22a96 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sat, 1 Nov 2014 17:08:59 -0400 Subject: [PATCH] Move placing the sections into methods This allows them to be overridden, allowing for things like attaching the dropdown to a different part of the DOM. The id is also now generated in a function that can be overridden. --- dist/js/select2.amd.full.js | 82 ++++++++++++++++++++++++++----------- dist/js/select2.amd.js | 82 ++++++++++++++++++++++++++----------- dist/js/select2.full.js | 82 ++++++++++++++++++++++++++----------- dist/js/select2.full.min.js | 2 +- dist/js/select2.js | 82 ++++++++++++++++++++++++++----------- dist/js/select2.min.js | 2 +- src/js/select2/core.js | 68 ++++++++++++++++++++---------- src/js/select2/options.js | 14 ++++++- 8 files changed, 292 insertions(+), 122 deletions(-) diff --git a/dist/js/select2.amd.full.js b/dist/js/select2.amd.full.js index 9b751bfe..7b5fa821 100644 --- a/dist/js/select2.amd.full.js +++ b/dist/js/select2.amd.full.js @@ -1521,11 +1521,21 @@ define('select2/defaults',[ define('select2/options',[ './defaults' ], function (Defaults) { - function Options (options) { - this.options = Defaults.apply(options); + function Options (options, $element) { + this.options = options; + + if ($element != null) { + this.fromElement($element); + } + + this.options = Defaults.apply(this.options); } Options.prototype.fromElement = function ($e) { + if (this.options.multiple == null) { + this.options.multiple = $e.prop('multiple'); + } + return this; }; @@ -1548,21 +1558,11 @@ define('select2/core',[ var Select2 = function ($element, options) { this.$element = $element; - if ($element.attr('id') != null) { - this.id = $element.attr('id'); - } else if ($element.attr('name') != null) { - this.id = $element.attr('name') + '-' + Utils.generateChars(2); - } else { - this.id = Utils.generateChars(4); - } - - this.id = 'select2-' + this.id; + this.id = this._generateId($element); options = options || {}; - options.multiple = options.multiple || $element.prop('multiple'); - - this.options = new Options(options); + this.options = new Options(options, $element); Select2.__super__.constructor.call(this); @@ -1572,35 +1572,29 @@ define('select2/core',[ this.data = new DataAdapter($element, this.options); var $container = this.render(); - this.$container = $container; - $container.insertAfter(this.$element); - - $container.width($element.outerWidth(false)); + this._placeContainer($container); var SelectionAdapter = this.options.get('selectionAdapter'); this.selection = new SelectionAdapter($element, this.options); - var $selectionContainer = $container.find('.selection'); var $selection = this.selection.render(); - $selectionContainer.append($selection); + this._placeSelection($selection); var DropdownAdapter = this.options.get('dropdownAdapter'); this.dropdown = new DropdownAdapter($element, this.options); - var $dropdownContainer = $container.find('.dropdown-wrapper'); var $dropdown = this.dropdown.render(); - $dropdownContainer.append($dropdown); + this._placeDropdown($dropdown); var ResultsAdapter = this.options.get('resultsAdapter'); this.results = new ResultsAdapter($element, this.options, this.data); - var $resultsContainer = $dropdown.find('.results'); var $results = this.results.render(); - $resultsContainer.append($results); + this._placeResults($results); // Bind events @@ -1699,6 +1693,44 @@ define('select2/core',[ Utils.Extend(Select2, Utils.Observable); + Select2.prototype._generateId = function ($element) { + var id = ''; + + if ($element.attr('id') != null) { + id = $element.attr('id'); + } else if ($element.attr('name') != null) { + id = $element.attr('name') + '-' + Utils.generateChars(2); + } else { + id = Utils.generateChars(4); + } + + id = 'select2-' + id; + + return id; + }; + + Select2.prototype._placeContainer = function ($container) { + $container.insertAfter(this.$element); + $container.width(this.$element.outerWidth(false)); + }; + + Select2.prototype._placeSelection = function ($selection) { + var $selectionContainer = this.$container.find('.selection'); + $selectionContainer.append($selection); + }; + + Select2.prototype._placeDropdown = function ($dropdown) { + this.$dropdown = $dropdown; + + var $dropdownContainer = this.$container.find('.dropdown-wrapper'); + $dropdownContainer.append($dropdown); + }; + + Select2.prototype._placeResults = function ($results) { + var $resultsContainer = this.$dropdown.find('.results'); + $resultsContainer.append($results); + }; + Select2.prototype.toggleDropdown = function () { if (this.isOpen()) { this.close(); @@ -1735,6 +1767,8 @@ define('select2/core',[ '' ); + this.$container = $container; + return $container; }; diff --git a/dist/js/select2.amd.js b/dist/js/select2.amd.js index 9b751bfe..7b5fa821 100644 --- a/dist/js/select2.amd.js +++ b/dist/js/select2.amd.js @@ -1521,11 +1521,21 @@ define('select2/defaults',[ define('select2/options',[ './defaults' ], function (Defaults) { - function Options (options) { - this.options = Defaults.apply(options); + function Options (options, $element) { + this.options = options; + + if ($element != null) { + this.fromElement($element); + } + + this.options = Defaults.apply(this.options); } Options.prototype.fromElement = function ($e) { + if (this.options.multiple == null) { + this.options.multiple = $e.prop('multiple'); + } + return this; }; @@ -1548,21 +1558,11 @@ define('select2/core',[ var Select2 = function ($element, options) { this.$element = $element; - if ($element.attr('id') != null) { - this.id = $element.attr('id'); - } else if ($element.attr('name') != null) { - this.id = $element.attr('name') + '-' + Utils.generateChars(2); - } else { - this.id = Utils.generateChars(4); - } - - this.id = 'select2-' + this.id; + this.id = this._generateId($element); options = options || {}; - options.multiple = options.multiple || $element.prop('multiple'); - - this.options = new Options(options); + this.options = new Options(options, $element); Select2.__super__.constructor.call(this); @@ -1572,35 +1572,29 @@ define('select2/core',[ this.data = new DataAdapter($element, this.options); var $container = this.render(); - this.$container = $container; - $container.insertAfter(this.$element); - - $container.width($element.outerWidth(false)); + this._placeContainer($container); var SelectionAdapter = this.options.get('selectionAdapter'); this.selection = new SelectionAdapter($element, this.options); - var $selectionContainer = $container.find('.selection'); var $selection = this.selection.render(); - $selectionContainer.append($selection); + this._placeSelection($selection); var DropdownAdapter = this.options.get('dropdownAdapter'); this.dropdown = new DropdownAdapter($element, this.options); - var $dropdownContainer = $container.find('.dropdown-wrapper'); var $dropdown = this.dropdown.render(); - $dropdownContainer.append($dropdown); + this._placeDropdown($dropdown); var ResultsAdapter = this.options.get('resultsAdapter'); this.results = new ResultsAdapter($element, this.options, this.data); - var $resultsContainer = $dropdown.find('.results'); var $results = this.results.render(); - $resultsContainer.append($results); + this._placeResults($results); // Bind events @@ -1699,6 +1693,44 @@ define('select2/core',[ Utils.Extend(Select2, Utils.Observable); + Select2.prototype._generateId = function ($element) { + var id = ''; + + if ($element.attr('id') != null) { + id = $element.attr('id'); + } else if ($element.attr('name') != null) { + id = $element.attr('name') + '-' + Utils.generateChars(2); + } else { + id = Utils.generateChars(4); + } + + id = 'select2-' + id; + + return id; + }; + + Select2.prototype._placeContainer = function ($container) { + $container.insertAfter(this.$element); + $container.width(this.$element.outerWidth(false)); + }; + + Select2.prototype._placeSelection = function ($selection) { + var $selectionContainer = this.$container.find('.selection'); + $selectionContainer.append($selection); + }; + + Select2.prototype._placeDropdown = function ($dropdown) { + this.$dropdown = $dropdown; + + var $dropdownContainer = this.$container.find('.dropdown-wrapper'); + $dropdownContainer.append($dropdown); + }; + + Select2.prototype._placeResults = function ($results) { + var $resultsContainer = this.$dropdown.find('.results'); + $resultsContainer.append($results); + }; + Select2.prototype.toggleDropdown = function () { if (this.isOpen()) { this.close(); @@ -1735,6 +1767,8 @@ define('select2/core',[ '' ); + this.$container = $container; + return $container; }; diff --git a/dist/js/select2.full.js b/dist/js/select2.full.js index 1d751664..25928652 100644 --- a/dist/js/select2.full.js +++ b/dist/js/select2.full.js @@ -11056,11 +11056,21 @@ define('select2/defaults',[ define('select2/options',[ './defaults' ], function (Defaults) { - function Options (options) { - this.options = Defaults.apply(options); + function Options (options, $element) { + this.options = options; + + if ($element != null) { + this.fromElement($element); + } + + this.options = Defaults.apply(this.options); } Options.prototype.fromElement = function ($e) { + if (this.options.multiple == null) { + this.options.multiple = $e.prop('multiple'); + } + return this; }; @@ -11083,21 +11093,11 @@ define('select2/core',[ var Select2 = function ($element, options) { this.$element = $element; - if ($element.attr('id') != null) { - this.id = $element.attr('id'); - } else if ($element.attr('name') != null) { - this.id = $element.attr('name') + '-' + Utils.generateChars(2); - } else { - this.id = Utils.generateChars(4); - } - - this.id = 'select2-' + this.id; + this.id = this._generateId($element); options = options || {}; - options.multiple = options.multiple || $element.prop('multiple'); - - this.options = new Options(options); + this.options = new Options(options, $element); Select2.__super__.constructor.call(this); @@ -11107,35 +11107,29 @@ define('select2/core',[ this.data = new DataAdapter($element, this.options); var $container = this.render(); - this.$container = $container; - $container.insertAfter(this.$element); - - $container.width($element.outerWidth(false)); + this._placeContainer($container); var SelectionAdapter = this.options.get('selectionAdapter'); this.selection = new SelectionAdapter($element, this.options); - var $selectionContainer = $container.find('.selection'); var $selection = this.selection.render(); - $selectionContainer.append($selection); + this._placeSelection($selection); var DropdownAdapter = this.options.get('dropdownAdapter'); this.dropdown = new DropdownAdapter($element, this.options); - var $dropdownContainer = $container.find('.dropdown-wrapper'); var $dropdown = this.dropdown.render(); - $dropdownContainer.append($dropdown); + this._placeDropdown($dropdown); var ResultsAdapter = this.options.get('resultsAdapter'); this.results = new ResultsAdapter($element, this.options, this.data); - var $resultsContainer = $dropdown.find('.results'); var $results = this.results.render(); - $resultsContainer.append($results); + this._placeResults($results); // Bind events @@ -11234,6 +11228,44 @@ define('select2/core',[ Utils.Extend(Select2, Utils.Observable); + Select2.prototype._generateId = function ($element) { + var id = ''; + + if ($element.attr('id') != null) { + id = $element.attr('id'); + } else if ($element.attr('name') != null) { + id = $element.attr('name') + '-' + Utils.generateChars(2); + } else { + id = Utils.generateChars(4); + } + + id = 'select2-' + id; + + return id; + }; + + Select2.prototype._placeContainer = function ($container) { + $container.insertAfter(this.$element); + $container.width(this.$element.outerWidth(false)); + }; + + Select2.prototype._placeSelection = function ($selection) { + var $selectionContainer = this.$container.find('.selection'); + $selectionContainer.append($selection); + }; + + Select2.prototype._placeDropdown = function ($dropdown) { + this.$dropdown = $dropdown; + + var $dropdownContainer = this.$container.find('.dropdown-wrapper'); + $dropdownContainer.append($dropdown); + }; + + Select2.prototype._placeResults = function ($results) { + var $resultsContainer = this.$dropdown.find('.results'); + $resultsContainer.append($results); + }; + Select2.prototype.toggleDropdown = function () { if (this.isOpen()) { this.close(); @@ -11270,6 +11302,8 @@ define('select2/core',[ '' ); + this.$container = $container; + return $container; }; diff --git a/dist/js/select2.full.min.js b/dist/js/select2.full.min.js index d29ab55a..3af8852c 100644 --- a/dist/js/select2.full.min.js +++ b/dist/js/select2.full.min.js @@ -1,4 +1,4 @@ window.$=window.$||{},function(){$&&$.fn&&$.fn.select2&&$.fn.select2.amd&&(c=$.fn.select2.amd.define,b=$.fn.select2.amd.require);var a,b,c;!function(d){function e(a,b){return u.call(a,b)}function f(a,b){var c,d,e,f,g,h,i,j,k,l,m,n=b&&b.split("/"),o=s.map,p=o&&o["*"]||{};if(a&&"."===a.charAt(0))if(b){for(n=n.slice(0,n.length-1),a=a.split("/"),g=a.length-1,s.nodeIdCompat&&w.test(a[g])&&(a[g]=a[g].replace(w,"")),a=n.concat(a),k=0;k0&&(a.splice(k-1,2),k-=2)}a=a.join("/")}else 0===a.indexOf("./")&&(a=a.substring(2));if((n||p)&&o){for(c=a.split("/"),k=c.length;k>0;k-=1){if(d=c.slice(0,k).join("/"),n)for(l=n.length;l>0;l-=1)if(e=o[n.slice(0,l).join("/")],e&&(e=e[d])){f=e,h=k;break}if(f)break;!i&&p&&p[d]&&(i=p[d],j=k)}!f&&i&&(f=i,h=j),f&&(c.splice(0,h,f),a=c.join("/"))}return a}function g(a,b){return function(){return n.apply(d,v.call(arguments,0).concat([a,b]))}}function h(a){return function(b){return f(b,a)}}function i(a){return function(b){q[a]=b}}function j(a){if(e(r,a)){var b=r[a];delete r[a],t[a]=!0,m.apply(d,b)}if(!e(q,a)&&!e(t,a))throw new Error("No "+a);return q[a]}function k(a){var b,c=a?a.indexOf("!"):-1;return c>-1&&(b=a.substring(0,c),a=a.substring(c+1,a.length)),[b,a]}function l(a){return function(){return s&&s.config&&s.config[a]||{}}}var m,n,o,p,q={},r={},s={},t={},u=Object.prototype.hasOwnProperty,v=[].slice,w=/\.js$/;o=function(a,b){var c,d=k(a),e=d[0];return a=d[1],e&&(e=f(e,b),c=j(e)),e?a=c&&c.normalize?c.normalize(a,h(b)):f(a,b):(a=f(a,b),d=k(a),e=d[0],a=d[1],e&&(c=j(e))),{f:e?e+"!"+a:a,n:a,pr:e,p:c}},p={require:function(a){return g(a)},exports:function(a){var b=q[a];return"undefined"!=typeof b?b:q[a]={}},module:function(a){return{id:a,uri:"",exports:q[a],config:l(a)}}},m=function(a,b,c,f){var h,k,l,m,n,s,u=[],v=typeof c;if(f=f||a,"undefined"===v||"function"===v){for(b=!b.length&&c.length?["require","exports","module"]:b,n=0;n0&&b-1 in a}function e(a,b,c){if(bb.isFunction(b))return bb.grep(a,function(a,d){return!!b.call(a,d,a)!==c});if(b.nodeType)return bb.grep(a,function(a){return a===b!==c});if("string"==typeof b){if(ib.test(b))return bb.filter(b,a,c);b=bb.filter(b,a)}return bb.grep(a,function(a){return V.call(b,a)>=0!==c})}function f(a,b){for(;(a=a[b])&&1!==a.nodeType;);return a}function g(a){var b=pb[a]={};return bb.each(a.match(ob)||[],function(a,c){b[c]=!0}),b}function h(){_.removeEventListener("DOMContentLoaded",h,!1),a.removeEventListener("load",h,!1),bb.ready()}function i(){Object.defineProperty(this.cache={},0,{get:function(){return{}}}),this.expando=bb.expando+Math.random()}function j(a,b,c){var d;if(void 0===c&&1===a.nodeType)if(d="data-"+b.replace(vb,"-$1").toLowerCase(),c=a.getAttribute(d),"string"==typeof c){try{c="true"===c?!0:"false"===c?!1:"null"===c?null:+c+""===c?+c:ub.test(c)?bb.parseJSON(c):c}catch(e){}tb.set(a,b,c)}else c=void 0;return c}function k(){return!0}function l(){return!1}function m(){try{return _.activeElement}catch(a){}}function n(a,b){return bb.nodeName(a,"table")&&bb.nodeName(11!==b.nodeType?b:b.firstChild,"tr")?a.getElementsByTagName("tbody")[0]||a.appendChild(a.ownerDocument.createElement("tbody")):a}function o(a){return a.type=(null!==a.getAttribute("type"))+"/"+a.type,a}function p(a){var b=Lb.exec(a.type);return b?a.type=b[1]:a.removeAttribute("type"),a}function q(a,b){for(var c=0,d=a.length;d>c;c++)sb.set(a[c],"globalEval",!b||sb.get(b[c],"globalEval"))}function r(a,b){var c,d,e,f,g,h,i,j;if(1===b.nodeType){if(sb.hasData(a)&&(f=sb.access(a),g=sb.set(b,f),j=f.events)){delete g.handle,g.events={};for(e in j)for(c=0,d=j[e].length;d>c;c++)bb.event.add(b,e,j[e][c])}tb.hasData(a)&&(h=tb.access(a),i=bb.extend({},h),tb.set(b,i))}}function s(a,b){var c=a.getElementsByTagName?a.getElementsByTagName(b||"*"):a.querySelectorAll?a.querySelectorAll(b||"*"):[];return void 0===b||b&&bb.nodeName(a,b)?bb.merge([a],c):c}function t(a,b){var c=b.nodeName.toLowerCase();"input"===c&&zb.test(a.type)?b.checked=a.checked:("input"===c||"textarea"===c)&&(b.defaultValue=a.defaultValue)}function u(b,c){var d=bb(c.createElement(b)).appendTo(c.body),e=a.getDefaultComputedStyle?a.getDefaultComputedStyle(d[0]).display:bb.css(d[0],"display");return d.detach(),e}function v(a){var b=_,c=Pb[a];return c||(c=u(a,b),"none"!==c&&c||(Ob=(Ob||bb("