Skip to content

Commit 75b62de

Browse files
sreichelfballiano
authored andcommitted
Added counter for input fields with maximum-length attribute (#2950)
1 parent 5fad9fe commit 75b62de

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

app/design/adminhtml/default/default/layout/admin.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@
2121
-->
2222

2323
<layout>
24+
<adminhtml_input_counter_handle>
25+
<reference name="head">
26+
<action method="addJs">
27+
<script>mage/adminhtml/input-counter.js</script>
28+
</action>
29+
</reference>
30+
</adminhtml_input_counter_handle>
31+
2432
<!-- admin acl users edit page -->
2533
<adminhtml_permissions_user_edit>
2634
<reference name="left">
@@ -87,4 +95,16 @@
8795
<block type="adminhtml/cache_additional" name="cache.additional" template="system/cache/additional.phtml"></block>
8896
</reference>
8997
</adminhtml_cache_index>
98+
99+
<adminhtml_catalog_product_attribute_edit>
100+
<update handle="adminhtml_input_counter_handle"/>
101+
</adminhtml_catalog_product_attribute_edit>
102+
103+
<adminhtml_catalog_product_edit>
104+
<update handle="adminhtml_input_counter_handle"/>
105+
</adminhtml_catalog_product_edit>
106+
107+
<adminhtml_system_config_edit>
108+
<update handle="adminhtml_input_counter_handle"/>
109+
</adminhtml_system_config_edit>
90110
</layout>

js/mage/adminhtml/input-counter.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// https://stackoverflow.com/a/44436408/5703627
2+
document.observe('dom:loaded', function() {
3+
Element.addMethods({
4+
// setup once, memorize the counter element and maxLen
5+
prepare_for_countdown: function(element) {
6+
var elm = $(element);
7+
// even if you call it multiple times, it only works once
8+
if(!elm.retrieve('counter')) {
9+
var counter = new Element('span');
10+
elm.next('.note').insert(counter);
11+
elm.store('counter', counter);
12+
var maxLen = elm.className.match(/maximum-length-(\d+)/)[1];
13+
elm.store('maxLen', maxLen);
14+
}
15+
return elm; // so you can chain
16+
},
17+
// display the value, run once at load and on each observed keyup
18+
countdown: function(element) {
19+
var elm = $(element);
20+
var curLen = elm.getValue().length;
21+
var maxLen = elm.retrieve('maxLen');
22+
var count = maxLen - curLen;
23+
var counter = elm.retrieve('counter');
24+
counter.update(' (' + curLen + '/' + maxLen + ')');
25+
if (curLen > maxLen) {
26+
counter.setStyle({'color': 'red'});
27+
} else {
28+
counter.setStyle({'color': 'inherit'});
29+
}
30+
return elm;
31+
}
32+
});
33+
34+
// run setup and call countdown once outside of listener to initialize
35+
$$('.validate-length').invoke('prepare_for_countdown').invoke('countdown');
36+
37+
// deferred listener, only responds to keyups that issue from a matching element
38+
document.on('keyup', '.validate-length', function(evt, elm) {
39+
elm.countdown();
40+
});
41+
});

0 commit comments

Comments
 (0)