Valentin Valchev | 6670225 | 2011-09-19 09:32:50 +0000 | [diff] [blame] | 1 | /* |
| 2 | Structure is: |
| 3 | <div class="my-element-container"> |
| 4 | <div class="multiInput"> |
| 5 | <div id="myElement" /> + - |
| 6 | </div> |
| 7 | </div> |
| 8 | |
| 9 | Options: |
| 10 | add : function(element) - called AFTER add |
| 11 | remove : function(element) - called BEFORE remove |
| 12 | */ |
| 13 | (function( $ ){ |
| 14 | |
| 15 | var methods = { |
| 16 | init : function(options) { |
| 17 | return this.each( function() { |
| 18 | // If options exist, lets merge them with our default settings |
| 19 | var settings = { |
| 20 | add : false, |
| 21 | remove : false |
| 22 | }; |
| 23 | if (options) settings = $.extend(settings, options); |
| 24 | |
| 25 | var _this = $(this); |
| 26 | var template = _init_template( _this ); |
| 27 | _this.data('addremove_settings', settings); |
| 28 | _new_entry(template, _this); |
| 29 | }) |
| 30 | }, |
| 31 | reset : function() { |
| 32 | return this.each( function() { |
| 33 | var self = $(this); |
| 34 | self.find('div.addremove').not(':first').each( function() { |
| 35 | $(this).find('button.rem').click(); |
| 36 | }); |
| 37 | }); |
| 38 | }, |
| 39 | add : function(count) { |
| 40 | return this.each( function() { |
| 41 | var self = $(this); |
| 42 | var addfn = self.find('div.addremove:last button.add'); |
| 43 | if (addfn.size()) { |
| 44 | var num = count ? count : 1; |
| 45 | for(var i=0; i<num; i++) addfn.click(); |
| 46 | } |
| 47 | }); |
| 48 | }, |
| 49 | count : function() { |
| 50 | var self = $(this); |
| 51 | return $(this).find('div.addremove').size(); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | $.fn.addremove = function( method ) { |
| 56 | // Method calling logic |
| 57 | if ( methods[method] ) { |
| 58 | return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); |
| 59 | } else if ( typeof method === 'object' || ! method ) { |
| 60 | return methods.init.apply( this, arguments ); |
| 61 | } else { |
| 62 | $.error( 'Method ' + method + ' does not exist on jQuery.addremove' ); |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | var _new_entry = function(template, container) { |
| 67 | var settings = container.data('addremove_settings'); |
| 68 | var _entry = template.clone() |
| 69 | .find('button.add').click( function() { |
| 70 | _new_entry(template, container); |
| 71 | return false; |
| 72 | }).end() |
| 73 | .find('button.rem').click( function() { |
| 74 | if (container.addremove('count') > 1) { |
| 75 | if (typeof settings.remove == 'function') { |
| 76 | settings.remove(_entry); |
| 77 | } |
| 78 | _entry.remove(); |
| 79 | } |
| 80 | return false; |
| 81 | }).end() |
| 82 | .appendTo(container); |
| 83 | if (typeof settings.add == 'function') settings.add(_entry); |
| 84 | } |
| 85 | |
| 86 | var _init_template = function(entry) { |
| 87 | return _el('div', 'addremove') |
| 88 | .append(entry.children()) |
| 89 | .append(_el('button', 'add').text('+')) |
| 90 | .append(_el('button', 'rem').text('-')); |
| 91 | } |
| 92 | |
| 93 | var _el = function(el, clazz) { |
| 94 | var ret = $(document.createElement(el)); |
| 95 | if (clazz) ret.addClass(clazz); |
| 96 | return ret; |
| 97 | } |
| 98 | |
| 99 | })( jQuery ); |