This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.inplace.js
106 lines (85 loc) · 3.06 KB
/
jquery.inplace.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
106
/**********
* Name : Inplace Title Plugin
* Author : Mojtaba Kianifar
* Version : 1.00
**********/
(function($, window, document, undefined){
function Inplace(element, options){
this.$element = $(element);
this.options = options;
this.title = this.$element.text();
this.$input = null;
this.has_actions = false;
this.$element.bind('complete', this.options.oncomplete);
this.init()
}
Inplace.prototype = {
init : function () {
this.$element.wrapInner('<span class="dp-inplace-value"></span>');
this.$edit = $('<button class="dp-edit-handel">').prependTo(this.$element);
this.$value = this.$element.find('.dp-inplace-value');
this.$input = $('<input type="text">').appendTo(this.$element).hide();
this.$save = $("<button class='dp-action-save'></button>").appendTo(this.$element).hide();
this.$cancel = $("<button class='dp-action-cancel'></button>").appendTo(this.$element).hide();
this.$cancel.bind("click", $.proxy(function (e) {
e.stopPropagation();
this.cancel();
}, this));
this.$save.bind("click", $.proxy(function (e) {
e.stopPropagation();
this.save();
}, this));
this.$edit.bind("click", $.proxy(function () {
this._showActions();
}, this));
this.$input.bind("keypress", $.proxy(function (e) {
if( e.which == 13){
this.save();
}
}, this))
},
_showActions: function(){
var value = this.title == ' ' ? '' : this.title;
this.$input.val(value).show().focus();
this.$save.show();
this.$cancel.show();
this.$value.hide();
},
_hideActions: function(){
this.$input.hide();
this.$save.hide();
this.$cancel.hide();
this.$value.show();
},
cancel : function () {
this._hideActions();
},
save : function () {
this.title = this.$input.val() == '' ? ' ' : this.$input.val();
this.$value.html(this.title);
this._hideActions();
this.$element.trigger('complete');
}
};
$.fn.inplace = function(options){
if( typeof options == 'string'){
var plugin = this.data('inplace');
if(plugin){
var r = plugin[options].apply(plugin, Array.prototype.slice.call( arguments, 1 ) );
if(r) return r
}
return this
}
options = $.extend({}, $.fn.inplace.defaults, options);
return this.each(function(){
var plugin = $.data(this, 'inplace');
if( ! plugin ){
plugin = new Inplace(this, options);
$.data(this, 'inplace', plugin);
}
});
};
$.fn.inplace.defaults = {
oncomplete: null,
};
})(jQuery, window, document);