-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggleForm.js
More file actions
154 lines (119 loc) · 4.03 KB
/
toggleForm.js
File metadata and controls
154 lines (119 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
;(function ($) {
var defaults = {
className: 'ui-toggleForm',
edit: 'button.edit',
cancel: 'button.cancel',
data: '.data',
form: '.form',
close: 'close',
hideOtherForms: true
};
var methods = {
init: function (options) {
return this.each(function (index) {
var settings = $.extend({}, defaults);
if (options) {
settings = $.extend(settings, options);
}
var self = $(this);
//so we can use settings in other methods
self.data('toggleForm-settings', settings);
var edit = self.find(settings.edit),
cancel = self.find(settings.cancel),
data = self.find(settings.data),
form = self.find(settings.form);
if(!self.data('toggleForm-toggled')) {
self.addClass(settings.className);
var close = $('<button>', {
type: 'button',
'class': settings.close,
html: '×'
}).appendTo(self);
edit.on('click', function (e) {
e.preventDefault();
var toggle = $(this).data('toggle');
if (settings.hideOtherForms) {
methods.showData.apply($('.' + settings.className));
}
showForm(toggle);
//console.log('edit clicked');
});
close.on('click', function () {
showData();
//console.log('close clicked');
});
cancel.on('click', function (e) {
e.preventDefault();
showData();
scrollToForm();
//console.log('cancel clicked');
});
self.data('toggleForm-toggled', true);
//console.log('toggleForm has been initialized on <' + self.prop('tagName') + ' id="' + self.attr('id') + '"> has been initialized');
}
else {
var close = self.find('button.' + settings.close);
}
showData();
function showData() {
close.add(form).hide();
data.add(edit).fadeIn('fast');
}
function showForm(toggle) {
data.add(edit).hide();
close.add($(toggle)).fadeIn('fast');
scrollToForm();
}
function scrollToForm() {
if ($(window).width() < 768) {
setTimeout(function () {
$(window).scrollTop(self.offset().top);
}, 250);
}
}
});
},
showForm: function () {
return this.each(function () {
var self = $(this),
settings = self.data('toggleForm-settings');
if (settings) {
self.find(settings.data + ', ' + settings.edit).hide();
self.find('button.' + settings.close + ', ' + settings.form).show();
}
});
},
showData: function () {
return this.each(function () {
var self = $(this),
settings = self.data('toggleForm-settings');
if (settings) {
self.find('button.' + settings.close + ', ' + settings.form).hide();
self.find(settings.data + ', ' + settings.edit).show();
}
});
},
destroy: function () {
return this.each(function () {
var self = $(this),
settings = self.data('toggleForm-settings');
if (settings) {
self.find(settings.data + ', ' + settings.form + ', ' + settings.edit).show();
self.find(settings.edit + ', ' + settings.cancel).off('click');
self.find('button.' + settings.close).remove();
self.data('toggleForm-toggled', false);
self.removeClass(settings.className);
}
});
}
};
$.fn.toggleForm = function (method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === "object" || !method) {
return methods.init.apply(this, arguments);
} else {
$.error("Method " + method + " does not exist on toggleForm");
}
};
})(jQuery);