From 3e05e32eda3636bbc1a12dda4f8982bb04048d71 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sat, 30 Aug 2014 19:32:29 -0400 Subject: [PATCH] Small changes in preparation for more adapters --- dist/js/select2.amd.full.js | 29 +++++++++++++++++++++-------- dist/js/select2.amd.js | 29 +++++++++++++++++++++-------- dist/js/select2.full.js | 29 +++++++++++++++++++++-------- dist/js/select2.js | 29 +++++++++++++++++++++-------- src/js/select2/core.js | 8 +++++++- src/js/select2/data/select.js | 15 +++++++++++---- src/js/select2/options.js | 2 +- src/js/select2/results.js | 2 +- src/js/select2/selection/single.js | 2 +- 9 files changed, 105 insertions(+), 40 deletions(-) diff --git a/dist/js/select2.amd.full.js b/dist/js/select2.amd.full.js index e3f05189..6ba9e1c9 100644 --- a/dist/js/select2.amd.full.js +++ b/dist/js/select2.amd.full.js @@ -245,10 +245,17 @@ define('select2/data/select',[ }; SelectAdapter.prototype.item = function ($option) { - var data = { - id: $option.val(), - text: $option.html() - }; + var data = $option.data("data"); + + // If the data has already be generated, use it + if (data == null) { + data = { + id: $option.val(), + text: $option.html() + }; + + $option.data("data", data); + } return data; }; @@ -356,7 +363,7 @@ define('select2/results',[ self.setClasses(); }) - this.$results.on("click", ".option", function (evt) { + this.$results.on("mouseup", ".option", function (evt) { var $this = $(this); var data = $this.data("data"); @@ -441,7 +448,7 @@ define('select2/selection/single',[ SingleSelection.prototype.bind = function (container, $container) { var self = this; - this.$selection.on('click', function (evt) { + this.$selection.on('mousedown', function (evt) { self.trigger("toggle", { originalEvent: evt }); @@ -561,7 +568,7 @@ define('select2/options',[ function Options (options) { this.options = options; - this.dataAdapter = SelectData; + this.dataAdapter = options.dataAdapter || SelectData; this.resultsAdapter = ResultsList; this.dropdownAdapter = options.dropdownAdapter || Dropdown; this.selectionAdapter = options.selectionAdapter; @@ -660,12 +667,18 @@ define('select2/core',[ // Set the initial state this.data.current(function (initialData) { - self.selection.update(initialData); + self.trigger("selection:update", { + data: initialData + }); }); this.data.query({}, function (data) { self.results.trigger("results:all", data); }); + + // Hide the original select + + $element.hide(); }; Utils.Extend(Select2, Utils.Observable); diff --git a/dist/js/select2.amd.js b/dist/js/select2.amd.js index e3f05189..6ba9e1c9 100644 --- a/dist/js/select2.amd.js +++ b/dist/js/select2.amd.js @@ -245,10 +245,17 @@ define('select2/data/select',[ }; SelectAdapter.prototype.item = function ($option) { - var data = { - id: $option.val(), - text: $option.html() - }; + var data = $option.data("data"); + + // If the data has already be generated, use it + if (data == null) { + data = { + id: $option.val(), + text: $option.html() + }; + + $option.data("data", data); + } return data; }; @@ -356,7 +363,7 @@ define('select2/results',[ self.setClasses(); }) - this.$results.on("click", ".option", function (evt) { + this.$results.on("mouseup", ".option", function (evt) { var $this = $(this); var data = $this.data("data"); @@ -441,7 +448,7 @@ define('select2/selection/single',[ SingleSelection.prototype.bind = function (container, $container) { var self = this; - this.$selection.on('click', function (evt) { + this.$selection.on('mousedown', function (evt) { self.trigger("toggle", { originalEvent: evt }); @@ -561,7 +568,7 @@ define('select2/options',[ function Options (options) { this.options = options; - this.dataAdapter = SelectData; + this.dataAdapter = options.dataAdapter || SelectData; this.resultsAdapter = ResultsList; this.dropdownAdapter = options.dropdownAdapter || Dropdown; this.selectionAdapter = options.selectionAdapter; @@ -660,12 +667,18 @@ define('select2/core',[ // Set the initial state this.data.current(function (initialData) { - self.selection.update(initialData); + self.trigger("selection:update", { + data: initialData + }); }); this.data.query({}, function (data) { self.results.trigger("results:all", data); }); + + // Hide the original select + + $element.hide(); }; Utils.Extend(Select2, Utils.Observable); diff --git a/dist/js/select2.full.js b/dist/js/select2.full.js index b4d3a3b9..f3cba9c5 100644 --- a/dist/js/select2.full.js +++ b/dist/js/select2.full.js @@ -9782,10 +9782,17 @@ define('select2/data/select',[ }; SelectAdapter.prototype.item = function ($option) { - var data = { - id: $option.val(), - text: $option.html() - }; + var data = $option.data("data"); + + // If the data has already be generated, use it + if (data == null) { + data = { + id: $option.val(), + text: $option.html() + }; + + $option.data("data", data); + } return data; }; @@ -9893,7 +9900,7 @@ define('select2/results',[ self.setClasses(); }) - this.$results.on("click", ".option", function (evt) { + this.$results.on("mouseup", ".option", function (evt) { var $this = $(this); var data = $this.data("data"); @@ -9978,7 +9985,7 @@ define('select2/selection/single',[ SingleSelection.prototype.bind = function (container, $container) { var self = this; - this.$selection.on('click', function (evt) { + this.$selection.on('mousedown', function (evt) { self.trigger("toggle", { originalEvent: evt }); @@ -10098,7 +10105,7 @@ define('select2/options',[ function Options (options) { this.options = options; - this.dataAdapter = SelectData; + this.dataAdapter = options.dataAdapter || SelectData; this.resultsAdapter = ResultsList; this.dropdownAdapter = options.dropdownAdapter || Dropdown; this.selectionAdapter = options.selectionAdapter; @@ -10197,12 +10204,18 @@ define('select2/core',[ // Set the initial state this.data.current(function (initialData) { - self.selection.update(initialData); + self.trigger("selection:update", { + data: initialData + }); }); this.data.query({}, function (data) { self.results.trigger("results:all", data); }); + + // Hide the original select + + $element.hide(); }; Utils.Extend(Select2, Utils.Observable); diff --git a/dist/js/select2.js b/dist/js/select2.js index dccc1154..6281994c 100644 --- a/dist/js/select2.js +++ b/dist/js/select2.js @@ -673,10 +673,17 @@ define('select2/data/select',[ }; SelectAdapter.prototype.item = function ($option) { - var data = { - id: $option.val(), - text: $option.html() - }; + var data = $option.data("data"); + + // If the data has already be generated, use it + if (data == null) { + data = { + id: $option.val(), + text: $option.html() + }; + + $option.data("data", data); + } return data; }; @@ -784,7 +791,7 @@ define('select2/results',[ self.setClasses(); }) - this.$results.on("click", ".option", function (evt) { + this.$results.on("mouseup", ".option", function (evt) { var $this = $(this); var data = $this.data("data"); @@ -869,7 +876,7 @@ define('select2/selection/single',[ SingleSelection.prototype.bind = function (container, $container) { var self = this; - this.$selection.on('click', function (evt) { + this.$selection.on('mousedown', function (evt) { self.trigger("toggle", { originalEvent: evt }); @@ -989,7 +996,7 @@ define('select2/options',[ function Options (options) { this.options = options; - this.dataAdapter = SelectData; + this.dataAdapter = options.dataAdapter || SelectData; this.resultsAdapter = ResultsList; this.dropdownAdapter = options.dropdownAdapter || Dropdown; this.selectionAdapter = options.selectionAdapter; @@ -1088,12 +1095,18 @@ define('select2/core',[ // Set the initial state this.data.current(function (initialData) { - self.selection.update(initialData); + self.trigger("selection:update", { + data: initialData + }); }); this.data.query({}, function (data) { self.results.trigger("results:all", data); }); + + // Hide the original select + + $element.hide(); }; Utils.Extend(Select2, Utils.Observable); diff --git a/src/js/select2/core.js b/src/js/select2/core.js index 06ebd053..8d0081c4 100644 --- a/src/js/select2/core.js +++ b/src/js/select2/core.js @@ -80,12 +80,18 @@ define([ // Set the initial state this.data.current(function (initialData) { - self.selection.update(initialData); + self.trigger("selection:update", { + data: initialData + }); }); this.data.query({}, function (data) { self.results.trigger("results:all", data); }); + + // Hide the original select + + $element.hide(); }; Utils.Extend(Select2, Utils.Observable); diff --git a/src/js/select2/data/select.js b/src/js/select2/data/select.js index 04ceddba..a89697f1 100644 --- a/src/js/select2/data/select.js +++ b/src/js/select2/data/select.js @@ -107,10 +107,17 @@ define([ }; SelectAdapter.prototype.item = function ($option) { - var data = { - id: $option.val(), - text: $option.html() - }; + var data = $option.data("data"); + + // If the data has already be generated, use it + if (data == null) { + data = { + id: $option.val(), + text: $option.html() + }; + + $option.data("data", data); + } return data; }; diff --git a/src/js/select2/options.js b/src/js/select2/options.js index dce6212e..b0ce465d 100644 --- a/src/js/select2/options.js +++ b/src/js/select2/options.js @@ -9,7 +9,7 @@ define([ function Options (options) { this.options = options; - this.dataAdapter = SelectData; + this.dataAdapter = options.dataAdapter || SelectData; this.resultsAdapter = ResultsList; this.dropdownAdapter = options.dropdownAdapter || Dropdown; this.selectionAdapter = options.selectionAdapter; diff --git a/src/js/select2/results.js b/src/js/select2/results.js index bb0f5159..b25747d4 100644 --- a/src/js/select2/results.js +++ b/src/js/select2/results.js @@ -86,7 +86,7 @@ define([ self.setClasses(); }) - this.$results.on("click", ".option", function (evt) { + this.$results.on("mouseup", ".option", function (evt) { var $this = $(this); var data = $this.data("data"); diff --git a/src/js/select2/selection/single.js b/src/js/select2/selection/single.js index 86a42184..5e77e7c3 100644 --- a/src/js/select2/selection/single.js +++ b/src/js/select2/selection/single.js @@ -25,7 +25,7 @@ define([ SingleSelection.prototype.bind = function (container, $container) { var self = this; - this.$selection.on('click', function (evt) { + this.$selection.on('mousedown', function (evt) { self.trigger("toggle", { originalEvent: evt });