From 1197960a4e945da65b4207896708c320acf1def1 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Thu, 28 Aug 2014 21:20:36 -0400 Subject: [PATCH] Add proof-of-concept decorator for clearing --- dist/js/select2.amd.full.js | 39 +++--- dist/js/select2.amd.js | 39 +++--- dist/js/select2.full.js | 39 +++--- dist/js/select2.js | 39 +++--- playground/basic/basic.html | 23 ---- playground/basic/decorators.html | 215 +++++++++++++++++++++++++++++++ src/js/select2/options.js | 2 +- src/js/select2/results.js | 2 - src/js/select2/utils.js | 35 ++--- 9 files changed, 315 insertions(+), 118 deletions(-) create mode 100644 playground/basic/decorators.html diff --git a/dist/js/select2.amd.full.js b/dist/js/select2.amd.full.js index 76b95bd2..74b3b52d 100644 --- a/dist/js/select2.amd.full.js +++ b/dist/js/select2.amd.full.js @@ -46,13 +46,13 @@ define('select2/utils',[], function () { function DecoratedClass () { var unshift = Array.prototype.unshift; - unshift.call(arguments, SuperClass.prototype.constructor); - var argCount = DecoratorClass.prototype.constructor.length; var calledConstructor = SuperClass.prototype.constructor; if (argCount > 0) { + unshift.call(arguments, SuperClass.prototype.constructor); + calledConstructor = DecoratorClass.prototype.constructor; } @@ -74,23 +74,26 @@ define('select2/utils',[], function () { for (var m = 0; m < decoratedMethods.length; m++) { var methodName = decoratedMethods[m]; - var originalMethod = function () {}; + function calledMethod (methodName) { + // Stub out the original method if it's not decorating an actual method + var originalMethod = function () {}; - if (methodName in DecoratedClass.prototype) { - originalMethod = DecoratedClass.prototype[methodName]; + if (methodName in DecoratedClass.prototype) { + originalMethod = DecoratedClass.prototype[methodName]; + } + + var decoratedMethod = DecoratorClass.prototype[methodName]; + + return function () { + var unshift = Array.prototype.unshift; + + unshift.call(arguments, originalMethod); + + return decoratedMethod.apply(this, arguments); + } } - var decoratedMethod = DecoratorClass.prototype[methodName]; - - function calledMethod () { - var unshift = Array.prototype.unshift; - - unshift.call(arguments, originalMethod); - - return decoratedMethod.apply(this, arguments); - } - - DecoratedClass.prototype[methodName] = calledMethod; + DecoratedClass.prototype[methodName] = calledMethod(methodName); } return DecoratedClass; @@ -275,8 +278,6 @@ define('select2/results',[ var $options = self.$results.find(".option"); - console.log($options); - $options.each(function () { var $option = $(this); var item = $option.data("data"); @@ -430,7 +431,7 @@ define('select2/options',[ this.dataAdapter = SelectData; this.resultsAdapter = ResultsList; this.dropdownAdapter = Dropdown; - this.selectionAdapter = Selection; + this.selectionAdapter = options.selectionAdapter || Selection; } return Options; diff --git a/dist/js/select2.amd.js b/dist/js/select2.amd.js index 76b95bd2..74b3b52d 100644 --- a/dist/js/select2.amd.js +++ b/dist/js/select2.amd.js @@ -46,13 +46,13 @@ define('select2/utils',[], function () { function DecoratedClass () { var unshift = Array.prototype.unshift; - unshift.call(arguments, SuperClass.prototype.constructor); - var argCount = DecoratorClass.prototype.constructor.length; var calledConstructor = SuperClass.prototype.constructor; if (argCount > 0) { + unshift.call(arguments, SuperClass.prototype.constructor); + calledConstructor = DecoratorClass.prototype.constructor; } @@ -74,23 +74,26 @@ define('select2/utils',[], function () { for (var m = 0; m < decoratedMethods.length; m++) { var methodName = decoratedMethods[m]; - var originalMethod = function () {}; + function calledMethod (methodName) { + // Stub out the original method if it's not decorating an actual method + var originalMethod = function () {}; - if (methodName in DecoratedClass.prototype) { - originalMethod = DecoratedClass.prototype[methodName]; + if (methodName in DecoratedClass.prototype) { + originalMethod = DecoratedClass.prototype[methodName]; + } + + var decoratedMethod = DecoratorClass.prototype[methodName]; + + return function () { + var unshift = Array.prototype.unshift; + + unshift.call(arguments, originalMethod); + + return decoratedMethod.apply(this, arguments); + } } - var decoratedMethod = DecoratorClass.prototype[methodName]; - - function calledMethod () { - var unshift = Array.prototype.unshift; - - unshift.call(arguments, originalMethod); - - return decoratedMethod.apply(this, arguments); - } - - DecoratedClass.prototype[methodName] = calledMethod; + DecoratedClass.prototype[methodName] = calledMethod(methodName); } return DecoratedClass; @@ -275,8 +278,6 @@ define('select2/results',[ var $options = self.$results.find(".option"); - console.log($options); - $options.each(function () { var $option = $(this); var item = $option.data("data"); @@ -430,7 +431,7 @@ define('select2/options',[ this.dataAdapter = SelectData; this.resultsAdapter = ResultsList; this.dropdownAdapter = Dropdown; - this.selectionAdapter = Selection; + this.selectionAdapter = options.selectionAdapter || Selection; } return Options; diff --git a/dist/js/select2.full.js b/dist/js/select2.full.js index 88f93ec9..f2511667 100644 --- a/dist/js/select2.full.js +++ b/dist/js/select2.full.js @@ -9583,13 +9583,13 @@ define('select2/utils',[], function () { function DecoratedClass () { var unshift = Array.prototype.unshift; - unshift.call(arguments, SuperClass.prototype.constructor); - var argCount = DecoratorClass.prototype.constructor.length; var calledConstructor = SuperClass.prototype.constructor; if (argCount > 0) { + unshift.call(arguments, SuperClass.prototype.constructor); + calledConstructor = DecoratorClass.prototype.constructor; } @@ -9611,23 +9611,26 @@ define('select2/utils',[], function () { for (var m = 0; m < decoratedMethods.length; m++) { var methodName = decoratedMethods[m]; - var originalMethod = function () {}; + function calledMethod (methodName) { + // Stub out the original method if it's not decorating an actual method + var originalMethod = function () {}; - if (methodName in DecoratedClass.prototype) { - originalMethod = DecoratedClass.prototype[methodName]; + if (methodName in DecoratedClass.prototype) { + originalMethod = DecoratedClass.prototype[methodName]; + } + + var decoratedMethod = DecoratorClass.prototype[methodName]; + + return function () { + var unshift = Array.prototype.unshift; + + unshift.call(arguments, originalMethod); + + return decoratedMethod.apply(this, arguments); + } } - var decoratedMethod = DecoratorClass.prototype[methodName]; - - function calledMethod () { - var unshift = Array.prototype.unshift; - - unshift.call(arguments, originalMethod); - - return decoratedMethod.apply(this, arguments); - } - - DecoratedClass.prototype[methodName] = calledMethod; + DecoratedClass.prototype[methodName] = calledMethod(methodName); } return DecoratedClass; @@ -9812,8 +9815,6 @@ define('select2/results',[ var $options = self.$results.find(".option"); - console.log($options); - $options.each(function () { var $option = $(this); var item = $option.data("data"); @@ -9967,7 +9968,7 @@ define('select2/options',[ this.dataAdapter = SelectData; this.resultsAdapter = ResultsList; this.dropdownAdapter = Dropdown; - this.selectionAdapter = Selection; + this.selectionAdapter = options.selectionAdapter || Selection; } return Options; diff --git a/dist/js/select2.js b/dist/js/select2.js index 1c08c362..06ce806e 100644 --- a/dist/js/select2.js +++ b/dist/js/select2.js @@ -474,13 +474,13 @@ define('select2/utils',[], function () { function DecoratedClass () { var unshift = Array.prototype.unshift; - unshift.call(arguments, SuperClass.prototype.constructor); - var argCount = DecoratorClass.prototype.constructor.length; var calledConstructor = SuperClass.prototype.constructor; if (argCount > 0) { + unshift.call(arguments, SuperClass.prototype.constructor); + calledConstructor = DecoratorClass.prototype.constructor; } @@ -502,23 +502,26 @@ define('select2/utils',[], function () { for (var m = 0; m < decoratedMethods.length; m++) { var methodName = decoratedMethods[m]; - var originalMethod = function () {}; + function calledMethod (methodName) { + // Stub out the original method if it's not decorating an actual method + var originalMethod = function () {}; - if (methodName in DecoratedClass.prototype) { - originalMethod = DecoratedClass.prototype[methodName]; + if (methodName in DecoratedClass.prototype) { + originalMethod = DecoratedClass.prototype[methodName]; + } + + var decoratedMethod = DecoratorClass.prototype[methodName]; + + return function () { + var unshift = Array.prototype.unshift; + + unshift.call(arguments, originalMethod); + + return decoratedMethod.apply(this, arguments); + } } - var decoratedMethod = DecoratorClass.prototype[methodName]; - - function calledMethod () { - var unshift = Array.prototype.unshift; - - unshift.call(arguments, originalMethod); - - return decoratedMethod.apply(this, arguments); - } - - DecoratedClass.prototype[methodName] = calledMethod; + DecoratedClass.prototype[methodName] = calledMethod(methodName); } return DecoratedClass; @@ -703,8 +706,6 @@ define('select2/results',[ var $options = self.$results.find(".option"); - console.log($options); - $options.each(function () { var $option = $(this); var item = $option.data("data"); @@ -858,7 +859,7 @@ define('select2/options',[ this.dataAdapter = SelectData; this.resultsAdapter = ResultsList; this.dropdownAdapter = Dropdown; - this.selectionAdapter = Selection; + this.selectionAdapter = options.selectionAdapter || Selection; } return Options; diff --git a/playground/basic/basic.html b/playground/basic/basic.html index 048bdead..311a201f 100644 --- a/playground/basic/basic.html +++ b/playground/basic/basic.html @@ -120,29 +120,6 @@ -
-
-
- - - - -
hello there -
- - + + + + + + + +
+
+
+ + + + + + + + diff --git a/src/js/select2/options.js b/src/js/select2/options.js index 2010ef42..8193cde3 100644 --- a/src/js/select2/options.js +++ b/src/js/select2/options.js @@ -10,7 +10,7 @@ define([ this.dataAdapter = SelectData; this.resultsAdapter = ResultsList; this.dropdownAdapter = Dropdown; - this.selectionAdapter = Selection; + this.selectionAdapter = options.selectionAdapter || Selection; } return Options; diff --git a/src/js/select2/results.js b/src/js/select2/results.js index 0dc4e1be..5532e410 100644 --- a/src/js/select2/results.js +++ b/src/js/select2/results.js @@ -48,8 +48,6 @@ define([ var $options = self.$results.find(".option"); - console.log($options); - $options.each(function () { var $option = $(this); var item = $option.data("data"); diff --git a/src/js/select2/utils.js b/src/js/select2/utils.js index c80cc862..4e856d04 100644 --- a/src/js/select2/utils.js +++ b/src/js/select2/utils.js @@ -46,13 +46,13 @@ define([], function () { function DecoratedClass () { var unshift = Array.prototype.unshift; - unshift.call(arguments, SuperClass.prototype.constructor); - var argCount = DecoratorClass.prototype.constructor.length; var calledConstructor = SuperClass.prototype.constructor; if (argCount > 0) { + unshift.call(arguments, SuperClass.prototype.constructor); + calledConstructor = DecoratorClass.prototype.constructor; } @@ -74,23 +74,26 @@ define([], function () { for (var m = 0; m < decoratedMethods.length; m++) { var methodName = decoratedMethods[m]; - var originalMethod = function () {}; + function calledMethod (methodName) { + // Stub out the original method if it's not decorating an actual method + var originalMethod = function () {}; - if (methodName in DecoratedClass.prototype) { - originalMethod = DecoratedClass.prototype[methodName]; + if (methodName in DecoratedClass.prototype) { + originalMethod = DecoratedClass.prototype[methodName]; + } + + var decoratedMethod = DecoratorClass.prototype[methodName]; + + return function () { + var unshift = Array.prototype.unshift; + + unshift.call(arguments, originalMethod); + + return decoratedMethod.apply(this, arguments); + } } - var decoratedMethod = DecoratorClass.prototype[methodName]; - - function calledMethod () { - var unshift = Array.prototype.unshift; - - unshift.call(arguments, originalMethod); - - return decoratedMethod.apply(this, arguments); - } - - DecoratedClass.prototype[methodName] = calledMethod; + DecoratedClass.prototype[methodName] = calledMethod(methodName); } return DecoratedClass;