blob: 6f90da4a96e5dc63dfb4773d731950244099cef6 [file] [log] [blame]
Valentin Valchev66702252011-09-19 09:32:50 +00001/*
Carsten Ziegelerb972c2b2013-07-26 08:47:46 +00002 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
Valentin Valchev66702252011-09-19 09:32:50 +000017 Structure is:
18 <div class="my-element-container">
19 <div class="multiInput">
20 <div id="myElement" /> + -
21 </div>
22 </div>
23
24 Options:
25 add : function(element) - called AFTER add
26 remove : function(element) - called BEFORE remove
27*/
28(function( $ ){
29
30 var methods = {
31 init : function(options) {
32 return this.each( function() {
33 // If options exist, lets merge them with our default settings
34 var settings = {
35 add : false,
36 remove : false
37 };
38 if (options) settings = $.extend(settings, options);
39
40 var _this = $(this);
41 var template = _init_template( _this );
42 _this.data('addremove_settings', settings);
43 _new_entry(template, _this);
44 })
45 },
46 reset : function() {
47 return this.each( function() {
48 var self = $(this);
49 self.find('div.addremove').not(':first').each( function() {
50 $(this).find('button.rem').click();
51 });
52 });
53 },
54 add : function(count) {
55 return this.each( function() {
56 var self = $(this);
57 var addfn = self.find('div.addremove:last button.add');
58 if (addfn.size()) {
59 var num = count ? count : 1;
60 for(var i=0; i<num; i++) addfn.click();
61 }
62 });
63 },
64 count : function() {
65 var self = $(this);
66 return $(this).find('div.addremove').size();
67 }
68 };
69
70 $.fn.addremove = function( method ) {
71 // Method calling logic
72 if ( methods[method] ) {
73 return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
74 } else if ( typeof method === 'object' || ! method ) {
75 return methods.init.apply( this, arguments );
76 } else {
77 $.error( 'Method ' + method + ' does not exist on jQuery.addremove' );
78 }
79 };
80
81 var _new_entry = function(template, container) {
82 var settings = container.data('addremove_settings');
83 var _entry = template.clone()
84 .find('button.add').click( function() {
85 _new_entry(template, container);
86 return false;
87 }).end()
88 .find('button.rem').click( function() {
89 if (container.addremove('count') > 1) {
90 if (typeof settings.remove == 'function') {
91 settings.remove(_entry);
92 }
93 _entry.remove();
94 }
95 return false;
96 }).end()
97 .appendTo(container);
98 if (typeof settings.add == 'function') settings.add(_entry);
99 }
100
101 var _init_template = function(entry) {
102 return _el('div', 'addremove')
103 .append(entry.children())
104 .append(_el('button', 'add').text('+'))
105 .append(_el('button', 'rem').text('-'));
106 }
107
108 var _el = function(el, clazz) {
109 var ret = $(document.createElement(el));
110 if (clazz) ret.addClass(clazz);
111 return ret;
112 }
113
114})( jQuery );