forked from waqarkhan88/jquery-plugin-makeEditable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jQuery.makeEditable.js
105 lines (93 loc) · 3.26 KB
/
jQuery.makeEditable.js
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
(function( $ ) {
$.fn.makeEditable = function() {
return this.each(function() {
if($(this).get(0).tagName.toLowerCase() == "span") {
if($(this).attr("data-type") && $(this).attr("data-type").length > 0) {
var dataType = $(this).attr("data-type").toLowerCase();
var thisElem = this;
var newElement;
var newElemName = $(this).attr("data-elemName") ? $(this).attr("data-elemName") : "ME_" + $(this).attr('id');
switch(dataType) {
case 'boolean':
newElement = $("<input type=\"checkbox\" name=\"" + newElemName + "\" value=\"" + $(thisElem).val() + "\" style=\"display:none;\" />");
$(newElement).on("blur", function() {
$(thisElem).text($(newElement).is(":checked") ? "Yes" : "No");
$(newElement).hide();
$(thisElem).show();
});
$(thisElem).on("click", function() {
$(newElement).prop("checked", $(thisElem).text().toLowerCase() == "yes" ? true : false);
$(newElement).val($(thisElem).text());
$(thisElem).hide();
$(newElement).show();
$(newElement).focus();
});
break;
case 'list':
var values = getValuesFromCallback($(this).attr("data-callback"));
newElement = $("<select name=\"" + newElemName + "\" style=\"display:none;\" />");
if(values) {
$(values).each(function() {
$(newElement).append($("<option value=\"" + this.value + "\">" + this.label + "</option>"));
});
}
$(newElement).on("blur", function() {
$(thisElem).text($(newElement).find(":selected").text());
$(thisElem).attr("data-value", $(newElement).val());
$(newElement).hide();
$(thisElem).show();
});
$(thisElem).on("click", function() {
var oldValue = $(thisElem).attr("data-value");
$(newElement).val(oldValue ? oldValue : "");
$(thisElem).hide();
$(newElement).show();
$(newElement).focus();
});
break;
case 'text':
newElement = $("<input type=\"text\" name=\"" + newElemName + "\" value=\"" + $(thisElem).val() + "\" style=\"display:none;\" />");
$(newElement).on("blur", function() {
$(thisElem).text($(newElement).val());
$(newElement).hide();
$(thisElem).show();
});
$(thisElem).on("click", function() {
$(newElement).val($(thisElem).text());
$(thisElem).hide();
$(newElement).show();
$(newElement).focus();
});
break;
}
$(newElement).insertAfter(this);
$(this).css({cursor: "hand"});
} else {
debug($(this).get(0) + " has no data-type defined.");
}
} else {
debug("this can only be applied to <span> elements");
}
});
};
var getValuesFromCallback = function(callback) {
callback = window[callback];
if(callback && (typeof callback === "function")) {
var values = callback();
if(values && values.length > 0) {
return values;
} else {
debug("No values returned from callback");
}
} else {
debug("Value callback required for this data-type.");
}
};
var debug = function(message) {
if(window.console && window.console.log) {
console.log(message);
} else {
alert(message);
}
};
}( jQuery ));