forked from olton/metroui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput-control.js
More file actions
113 lines (96 loc) · 3.15 KB
/
input-control.js
File metadata and controls
113 lines (96 loc) · 3.15 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
/**
* jQuery plugin for input elements for Metro UI CSS framework
*/
(function($) {
var pluginName = 'Input',
initAllSelector = '.input-control',
paramKeys = [];
$[pluginName] = function(element, options) {
if (!element) {
return $()[pluginName]({initAll: true});
}
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, .btn-clear');
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, .btn-reveal');
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[pluginName] = function(options) {
var elements = options && options.initAll ? $(initAllSelector) : this;
return elements.each(function() {
var that = $(this),
params = {},
plugin;
if (undefined == that.data(pluginName)) {
$.each(paramKeys, function(index, key){
params[key[0].toLowerCase() + key.slice(1)] = that.data('param' + key);
});
plugin = new $[pluginName](this, params);
that.data(pluginName, plugin);
}
});
};
// autoinit
$(function(){
$()["Input"]({initAll: true});
});
})(jQuery);