forked from olton/metroui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput-control.js
106 lines (87 loc) · 2.72 KB
/
input-control.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
/**
* jQuery plugin for input elements for Metro UI CSS framework
*/
(function($) {
$.Input = function(element, options) {
var defaults = {
};
var plugin = this;
plugin.settings = {};
var $element = $(element);
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
if ($element.hasClass('text')) {
initTextInput();
} else if ($element.hasClass('password')) {
initPasswordInput();
}
};
/**
* initialize text input element behavior
*/
var initTextInput = function () {
var $helper,
input;
$helper = $element.children('.helper');
if (!$helper.get(0)) {
return;
}
$helper.attr('tabindex', '-1');
$helper.attr('type', 'button');
// clear text when click on helper
$helper.on('click', function () {
input = $element.children('input');
if (input.prop('readonly')) {
return;
}
input.val('');
input.focus();
});
};
/**
* initialize password input element behavior
*/
var initPasswordInput = function () {
var $helper,
password,
text;
$helper = $element.children('.helper');
if (!$helper.get(0)) {
return;
}
text = $('<input type="text" />');
password = $element.children('input');
$helper.attr('tabindex', '-1');
$helper.attr('type', 'button');
// insert text element and hode password element when push helper
$helper.on('mousedown', function () {
password.hide();
text.insertAfter(password);
text.val(password.val());
});
// return password and remove text element
$helper.on('mouseup, mouseout', function () {
text.detach();
password.show();
password.focus();
});
};
plugin.init();
};
$.fn.Input = function(options) {
return this.each(function() {
if (undefined == $(this).data('Input')) {
var plugin = new $.Input(this, options);
$(this).data('Input', plugin);
}
});
}
})(jQuery);
$(function(){
var allInputs = $('.input-control');
allInputs.each(function (index, input) {
var params = {};
$input = $(input);
$input.Input(params);
});
});