Commit 59f24175e7d5c6d83665acd1b248b31d6f9db914
1 parent
c4bed142
Exists in
master
and in
29 other branches
Add jquery plugin textchange
Showing
2 changed files
with
77 additions
and
1 deletions
Show diff stats
app/views/layouts/_javascript.html.erb
| ... | ... | @@ -2,7 +2,7 @@ |
| 2 | 2 | 'jquery-2.1.1.min', 'jquery-migrate-1.2.1', |
| 3 | 3 | 'jquery.noconflict.js', 'jquery.cycle.all.min.js', 'thickbox.js', 'lightbox', 'colorbox', |
| 4 | 4 | 'jquery-ui-1.10.4/js/jquery-ui-1.10.4.min', 'jquery.scrollTo', 'jquery.form.js', 'jquery-validation/jquery.validate', |
| 5 | -'jquery.cookie', 'jquery.ba-bbq.min.js', 'reflection', 'jquery.tokeninput', 'jquery.typewatch', | |
| 5 | +'jquery.cookie', 'jquery.ba-bbq.min.js', 'reflection', 'jquery.tokeninput', 'jquery.typewatch', 'jquery.textchange', | |
| 6 | 6 | 'add-and-join', 'report-abuse', 'catalog', 'manage-products', 'autogrow', |
| 7 | 7 | 'jquery-timepicker-addon/dist/jquery-ui-timepicker-addon', 'application.js', 'rails.js', :cache => 'cache/application' %> |
| 8 | 8 | ... | ... |
| ... | ... | @@ -0,0 +1,76 @@ |
| 1 | +/*! | |
| 2 | + * jQuery TextChange Plugin | |
| 3 | + * http://www.zurb.com/playground/jquery-text-change-custom-event | |
| 4 | + * | |
| 5 | + * Copyright 2010, ZURB | |
| 6 | + * Released under the MIT License | |
| 7 | + */ | |
| 8 | +(function ($) { | |
| 9 | + | |
| 10 | + $.event.special.textchange = { | |
| 11 | + | |
| 12 | + setup: function (data, namespaces) { | |
| 13 | + $(this).data('lastValue', this.contentEditable === 'true' ? $(this).html() : $(this).val()); | |
| 14 | + $(this).bind('keyup.textchange', $.event.special.textchange.handler); | |
| 15 | + $(this).bind('cut.textchange paste.textchange input.textchange', $.event.special.textchange.delayedHandler); | |
| 16 | + }, | |
| 17 | + | |
| 18 | + teardown: function (namespaces) { | |
| 19 | + $(this).unbind('.textchange'); | |
| 20 | + }, | |
| 21 | + | |
| 22 | + handler: function (event) { | |
| 23 | + $.event.special.textchange.triggerIfChanged($(this)); | |
| 24 | + }, | |
| 25 | + | |
| 26 | + delayedHandler: function (event) { | |
| 27 | + var element = $(this); | |
| 28 | + setTimeout(function () { | |
| 29 | + $.event.special.textchange.triggerIfChanged(element); | |
| 30 | + }, 25); | |
| 31 | + }, | |
| 32 | + | |
| 33 | + triggerIfChanged: function (element) { | |
| 34 | + var current = element[0].contentEditable === 'true' ? element.html() : element.val(); | |
| 35 | + if (current !== element.data('lastValue')) { | |
| 36 | + element.trigger('textchange', [element.data('lastValue')]); | |
| 37 | + element.data('lastValue', current); | |
| 38 | + } | |
| 39 | + } | |
| 40 | + }; | |
| 41 | + | |
| 42 | + $.event.special.hastext = { | |
| 43 | + | |
| 44 | + setup: function (data, namespaces) { | |
| 45 | + $(this).bind('textchange', $.event.special.hastext.handler); | |
| 46 | + }, | |
| 47 | + | |
| 48 | + teardown: function (namespaces) { | |
| 49 | + $(this).unbind('textchange', $.event.special.hastext.handler); | |
| 50 | + }, | |
| 51 | + | |
| 52 | + handler: function (event, lastValue) { | |
| 53 | + if ((lastValue === '') && lastValue !== $(this).val()) { | |
| 54 | + $(this).trigger('hastext'); | |
| 55 | + } | |
| 56 | + } | |
| 57 | + }; | |
| 58 | + | |
| 59 | + $.event.special.notext = { | |
| 60 | + | |
| 61 | + setup: function (data, namespaces) { | |
| 62 | + $(this).bind('textchange', $.event.special.notext.handler); | |
| 63 | + }, | |
| 64 | + | |
| 65 | + teardown: function (namespaces) { | |
| 66 | + $(this).unbind('textchange', $.event.special.notext.handler); | |
| 67 | + }, | |
| 68 | + | |
| 69 | + handler: function (event, lastValue) { | |
| 70 | + if ($(this).val() === '' && $(this).val() !== lastValue) { | |
| 71 | + $(this).trigger('notext'); | |
| 72 | + } | |
| 73 | + } | |
| 74 | + }; | |
| 75 | + | |
| 76 | +})(jQuery); | |
| 0 | 77 | \ No newline at end of file | ... | ... |