Skip to content
Browse files

better compatibility with zepto

  • Loading branch information...
1 parent 33d0e2c commit d41e0b4a1c2830df0dcaafa7590abf86ee7b645b @joestelmach joestelmach committed Jan 5, 2013
Showing with 115 additions and 138 deletions.
  1. +2 −2 src/js/backbone_ui.js
  2. +6 −31 src/js/button.js
  3. +107 −105 src/js/input_event.js
View
4 src/js/backbone_ui.js
@@ -58,10 +58,10 @@
var options = _(this.options).clone();
_(objects).each(function(object) {
- $.extend(true, this, object);
+ $.extend(this, object);
}, this);
- $.extend(true, this.options, options);
+ $.extend(this.options, options);
}
});
View
37 src/js/button.js
@@ -27,37 +27,12 @@
$(this.el).addClass('button');
- // if we're running in a mobile environment, the 'click' event
- // isn't quite translated correctly
- if(Backbone.UI.IS_MOBILE) {
- $(this.el).bind('touchstart', _(function(e) {
- $(this.el).addClass('active');
-
- Backbone.UI._activeButton = this;
- var bodyUpListener = $(document.body).bind('touchend', function(e) {
- if(Backbone.UI._activeButton) {
- if(e.target === Backbone.UI._activeButton.el || $(e.target).closest('.button.active').length > 0) {
- if(Backbone.UI._activeButton.options.onClick) Backbone.UI._activeButton.options.onClick(e);
- }
- $(Backbone.UI._activeButton.el).removeClass('active');
- }
-
- Backbone.UI._activeButton = null;
- $(document.body).unbind('touchend', bodyUpListener);
- });
-
- return false;
- }).bind(this));
- }
-
- else {
- $(this.el).bind('click', _(function(e) {
- if(!this.options.disabled && !this.options.active && this.options.onClick) {
- this.options.onClick(e);
- }
- return false;
- }).bind(this));
- }
+ $(this.el).bind('click', _(function(e) {
+ if(!this.options.disabled && !this.options.active && this.options.onClick) {
+ this.options.onClick(e);
+ }
+ return false;
+ }).bind(this));
},
render : function() {
View
212 src/js/input_event.js
@@ -7,120 +7,122 @@
MIT license
www.opensource.org/licenses/mit-license.php
*/
-(function($, udf) {
- var ns = ".inputEvent ",
- // A bunch of data strings that we use regularly
- dataBnd = "bound.inputEvent",
- dataVal = "value.inputEvent",
- dataDlg = "delegated.inputEvent",
- // Set up our list of events
- bindTo = [
- "input", "textInput",
- "propertychange",
- "paste", "cut",
- "keydown", "keyup",
- "drop",
- ""].join(ns),
- // Events required for delegate, mostly for IE support
- dlgtTo = [ "focusin", "mouseover", "dragstart", "" ].join(ns),
- // Elements supporting text input, not including contentEditable
- supported = {TEXTAREA:udf, INPUT:udf},
- // Events that fire before input value is updated
- delay = { paste:udf, cut:udf, keydown:udf, drop:udf, textInput:udf };
+if(window.jQuery) {
+ (function($, udf) {
+ var ns = ".inputEvent ",
+ // A bunch of data strings that we use regularly
+ dataBnd = "bound.inputEvent",
+ dataVal = "value.inputEvent",
+ dataDlg = "delegated.inputEvent",
+ // Set up our list of events
+ bindTo = [
+ "input", "textInput",
+ "propertychange",
+ "paste", "cut",
+ "keydown", "keyup",
+ "drop",
+ ""].join(ns),
+ // Events required for delegate, mostly for IE support
+ dlgtTo = [ "focusin", "mouseover", "dragstart", "" ].join(ns),
+ // Elements supporting text input, not including contentEditable
+ supported = {TEXTAREA:udf, INPUT:udf},
+ // Events that fire before input value is updated
+ delay = { paste:udf, cut:udf, keydown:udf, drop:udf, textInput:udf };
- // this checks if the tag is supported or has the contentEditable property
- function isSupported(elem) {
- return $(elem).prop('contenteditable') == "true" ||
- elem.tagName in supported;
- };
+ // this checks if the tag is supported or has the contentEditable property
+ function isSupported(elem) {
+ return $(elem).prop('contenteditable') == "true" ||
+ elem.tagName in supported;
+ };
- $.event.special.txtinput = {
- setup: function(data, namespaces, handler) {
- var timer,
- bndCount,
- // Get references to the element
- elem = this,
- $elem = $(this),
- triggered = false;
+ $.event.special.txtinput = {
+ setup: function(data, namespaces, handler) {
+ var timer,
+ bndCount,
+ // Get references to the element
+ elem = this,
+ $elem = $(this),
+ triggered = false;
- if (isSupported(elem)) {
- bndCount = $.data(elem, dataBnd) || 0;
+ if (isSupported(elem)) {
+ bndCount = $.data(elem, dataBnd) || 0;
- if (!bndCount)
- $elem.bind(bindTo, handler);
+ if (!bndCount)
+ $elem.bind(bindTo, handler);
- $.data(elem, dataBnd, ++bndCount);
- $.data(elem, dataVal, elem.value);
- } else {
- $elem.bind(dlgtTo, function (e) {
- var target = e.target;
- if (isSupported(target) && !$.data(elem, dataDlg)) {
- bndCount = $.data(target, dataBnd) || 0;
+ $.data(elem, dataBnd, ++bndCount);
+ $.data(elem, dataVal, elem.value);
+ } else {
+ $elem.bind(dlgtTo, function (e) {
+ var target = e.target;
+ if (isSupported(target) && !$.data(elem, dataDlg)) {
+ bndCount = $.data(target, dataBnd) || 0;
- if (!bndCount)
- $(target).bind(bindTo, handler);
+ if (!bndCount)
+ $(target).bind(bindTo, handler);
- // make sure we increase the count only once for each bound ancestor
- $.data(elem, dataDlg, true);
- $.data(target, dataBnd, ++bndCount);
- $.data(target, dataVal, target.value);
- }
- });
- }
- function handler (e) {
- var elem = e.target;
+ // make sure we increase the count only once for each bound ancestor
+ $.data(elem, dataDlg, true);
+ $.data(target, dataBnd, ++bndCount);
+ $.data(target, dataVal, target.value);
+ }
+ });
+ }
+ function handler (e) {
+ var elem = e.target;
- // Clear previous timers because we only need to know about 1 change
- window.clearTimeout(timer), timer = null;
+ // Clear previous timers because we only need to know about 1 change
+ window.clearTimeout(timer), timer = null;
- // Return if we've already triggered the event
- if (triggered)
- return;
+ // Return if we've already triggered the event
+ if (triggered)
+ return;
- // paste, cut, keydown and drop all fire before the value is updated
- if (e.type in delay && !timer) {
- // ...so we need to delay them until after the event has fired
- timer = window.setTimeout(function () {
- if (elem.value !== $.data(elem, dataVal)) {
- $(elem).trigger("txtinput");
- $.data(elem, dataVal, elem.value);
- }
- }, 0);
- }
- else if (e.type == "propertychange") {
- if (e.originalEvent.propertyName == "value") {
- $(elem).trigger("txtinput");
- $.data(elem, dataVal, elem.value);
- triggered = true;
- window.setTimeout(function () {
- triggered = false;
- }, 0);
- }
- }
- else {
- $(elem).trigger("txtinput");
- $.data(elem, dataVal, elem.value);
- triggered = true;
- window.setTimeout(function () {
- triggered = false;
- }, 0);
- }
- }
- },
- teardown: function () {
- var elem = $(this);
- elem.unbind(dlgtTo);
- elem.find("input, textarea").andSelf().each(function () {
- bndCount = $.data(this, dataBnd, ($.data(this, dataBnd) || 1)-1);
+ // paste, cut, keydown and drop all fire before the value is updated
+ if (e.type in delay && !timer) {
+ // ...so we need to delay them until after the event has fired
+ timer = window.setTimeout(function () {
+ if (elem.value !== $.data(elem, dataVal)) {
+ $(elem).trigger("txtinput");
+ $.data(elem, dataVal, elem.value);
+ }
+ }, 0);
+ }
+ else if (e.type == "propertychange") {
+ if (e.originalEvent.propertyName == "value") {
+ $(elem).trigger("txtinput");
+ $.data(elem, dataVal, elem.value);
+ triggered = true;
+ window.setTimeout(function () {
+ triggered = false;
+ }, 0);
+ }
+ }
+ else {
+ $(elem).trigger("txtinput");
+ $.data(elem, dataVal, elem.value);
+ triggered = true;
+ window.setTimeout(function () {
+ triggered = false;
+ }, 0);
+ }
+ }
+ },
+ teardown: function () {
+ var elem = $(this);
+ elem.unbind(dlgtTo);
+ elem.find("input, textarea").andSelf().each(function () {
+ bndCount = $.data(this, dataBnd, ($.data(this, dataBnd) || 1)-1);
- if (!bndCount)
- elem.unbind(bindTo);
- });
- }
- };
+ if (!bndCount)
+ elem.unbind(bindTo);
+ });
+ }
+ };
- // Setup our jQuery shorthand method
- $.fn.input = function (handler) {
- return handler ? $(this).bind("txtinput", handler) : this.trigger("txtinput");
- }
-})(jQuery);
+ // Setup our jQuery shorthand method
+ $.fn.input = function (handler) {
+ return handler ? $(this).bind("txtinput", handler) : this.trigger("txtinput");
+ }
+ })(window.jQuery);
+}

0 comments on commit d41e0b4

Please sign in to comment.
Something went wrong with that request. Please try again.