Skip to content

Commit 15c08ab

Browse files
committed
Refine input number API
Add joined_digits rules for selection of multiple ids. Rename test.html to sample.html
1 parent 097a7f3 commit 15c08ab

File tree

6 files changed

+89
-35
lines changed

6 files changed

+89
-35
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ Router::post('foo/bar', function(){
1010
```
1111
Why not try this without any Router::xxx like code, just write your code in separated files:
1212
* http://localhost
13+
1314
![Demo portal](https://github.com/steem/qwp/blob/master/doc/demo_portal.png)
1415

1516
* http://localhost/?m=foo&op=bar
17+
1618
![Demo foo bar](https://github.com/steem/qwp/blob/master/doc/demo_foo_bar.png)
1719

1820
## qwp

include/common.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ function get_query_string() {
7575
}
7676
return "";
7777
}
78+
function get_joined_digits(&$v, &$ids) {
79+
$ids = explode(',', $v);
80+
if (count($ids) === 0) {
81+
return false;
82+
}
83+
foreach ($ids as $id) {
84+
if (!is_digits($id)) {
85+
return false;
86+
}
87+
}
88+
return true;
89+
}
7890
function array_to_query_string(&$arr) {
7991
$sep = '';
8092
$p = '';
@@ -1028,6 +1040,7 @@ function get_input_rules($k = null) {
10281040
'ipv6' => "^(?:-?\\d+|-?\\d{1,3}(?:,\\d{3})+)?(?:\\.\\d+)?$",
10291041
'datehour' => "^\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d$",
10301042
'datetime' => "^\\d\\d\\d\\d-\\d\\d-\\d\\d \\d\\d:\\d\\d:\\d\\d$",
1043+
'joined_digits' => "^\\d+[\\d|,]+$",
10311044
);
10321045
return $k ? (isset($rules[$k]) ? $rules[$k] : false): $rules;
10331046
}
@@ -1060,6 +1073,13 @@ function is_digits($v) {
10601073
}
10611074
return preg_match("/" . $statement . "/", $v);
10621075
}
1076+
function is_joined_digits($v) {
1077+
static $statement;
1078+
if (!isset($statement)) {
1079+
$statement = get_input_rules('joined_digits');
1080+
}
1081+
return preg_match("/" . $statement . "/", $v);
1082+
}
10631083
function is_letters($v) {
10641084
static $statement;
10651085
if (!isset($statement)) {

include/db.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ function qwp_create_query(&$query, $table_name, &$fields, &$options = null) {
341341
if (is_array($options['limits'])) $query->range($options['limits'][0], $options['limits'][1]);
342342
else $query->range(0, $options['limits']);
343343
}
344-
if ($options['random'] && $options['random']) {
344+
if (isset($options['random']) && $options['random']) {
345345
$query->orderBy('RAND()');
346346
}
347347
}

js/qwp.js

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ qwp.$fn = function(fn) {
8787
return window[s] ? s : false;
8888
};
8989
function $noop() {}
90+
function $false() {return false;}
9091
function $L(txt) {
9192
if (_LANG && _LANG[txt]) txt = _LANG[txt];
9293
if (arguments.length == 1) return txt;
@@ -213,6 +214,13 @@ $h = {};
213214
$.extend(qwp, {
214215
_:'&nbsp',
215216
br:'<br>',
217+
$: function(s, p) {
218+
if (qwp.isString(s)) {
219+
if (p) return p.find(s);
220+
else return $(s);
221+
}
222+
return s;
223+
},
216224
isString: function(v) {
217225
return $.type(v) == 'string';
218226
},
@@ -464,67 +472,85 @@ $h = {};
464472
$.gritter.remove(qwp.lastGritterId);
465473
qwp.lastGritterId = false;
466474
}
475+
},
476+
parseProps: function(p) {
477+
var o = {};
478+
if (!p || p.length === 0) return o;
479+
p = p.split('|');
480+
for (var i = 0, cnt = p.length; i < cnt; ++i) {
481+
var a = p[i];
482+
var d = a.indexOf('=');
483+
if (d == -1) continue;
484+
o[a.substr(0, d)] = a.substr(d + 1);
485+
}
486+
return o;
467487
}
468488
});
469489
qwp.ui = {
470490
defaultIcon: 'glyphicon',
471491
input: {
472-
number: function(s, enter, defaultValue, minValue, regExp, parent, params) {
473-
if (qwp.isString(s)) {
474-
if (parent) parent.find(s);
475-
else s = $(s);
476-
}
492+
number: function(s, props, parent, params) {
493+
s = qwp.$(s, parent);
494+
s.unbind('blur').unbind('keypress');
495+
if (props.enter) s.unbind('keyup');
496+
if (props.disablePaste) s.unbind('paste').on('paste', $false);
477497
s.keypress(function(e){
478498
if (e.keyCode < 48 || e.keyCode > 57) return false;
479499
});
480-
if (enter) {
500+
if (props.enter) {
481501
s.keyup(function(e){
482502
if (e.keyCode == 13) {
483-
if (window[enter]) window[enter](e.delegateTarget, params);
484-
else eval(enter);
503+
if (window[props.enter]) window[props.enter](e.delegateTarget, params);
504+
else eval(props.enter);
485505
}
486506
});
487507
}
488508
s.blur(function(e){
489509
var o = $(e.delegateTarget);
490-
var isDefinedValue = typeof(defaultValue) !== 'undefined', v = o.val(), isValid;
510+
var isDefinedValue = typeof(props.defaultValue) !== 'undefined', v = o.val(), isValid;
491511
if (v.length === 0) {
492512
if (isDefinedValue) {
493-
o.val(defaultValue);
513+
o.val(props.defaultValue);
494514
o.trigger('change', e);
495515
}
496516
return;
497517
}
498518
isValid = /^\d+$/.test(v);
499-
if (isValid && regExp) {
500-
var re = new RegExp(regExp);
519+
if (isValid && props.regExp) {
520+
var re = new RegExp(props.regExp);
501521
isValid = re.test(v);
502522
}
503523
if (!isValid) {
504-
if (isDefinedValue) o.val(defaultValue);
524+
if (isDefinedValue) o.val(props.defaultValue);
505525
else o.val('');
506526
o.trigger('change', e);
507527
return;
508528
}
509-
if (minValue) {
510-
v = parseInt(v);
511-
if (v < parseInt(minValue)) {
512-
o.val(minValue);
529+
v = parseInt(v);
530+
if (props.minValue) {
531+
if (v < parseInt(props.minValue)) {
532+
o.val(props.minValue);
533+
o.trigger('change', e);
534+
return;
535+
}
536+
}
537+
if (props.maxValue) {
538+
if (v > parseInt(props.maxValue)) {
539+
o.val(props.maxValue);
513540
o.trigger('change', e);
514541
}
515542
}
516543
});
544+
return s;
517545
},
518546
createUIComponents: function(p) {
519547
var t;
520-
if (p) t = p.find('input[dt=number]');
521-
else t = $('input[dt=number]');
548+
if (p) t = p.find('input[qwp=number]');
549+
else t = $('input[qwp=number]');
522550
t.each(function(i, o){
523551
o = $(o);
524-
var enter = o.attr('dtEnter');
525-
o.unbind('blur').unbind('keypress');
526-
if (enter) o.unbind('keyup');
527-
qwp.ui.input.number(o, enter, o.attr('dtValue'), o.attr('dtMinValue'), o.attr('dtRegExp'), enter, p);
552+
var props = qwp.parseProps(o.attr('props'));
553+
qwp.ui.input.number(o, props, p);
528554
});
529555
}
530556
},
@@ -584,11 +610,8 @@ $h = {};
584610
qwp.ui._fns.push(f);
585611
},
586612
createUIComponents: function(p) {
587-
var t;
588-
if (p) t = p.find('[data-rel=tooltip]');
589-
else t = $('[data-rel=tooltip]');
590613
qwp.ui.input.createUIComponents(p);
591-
t.each(function (i, e) {
614+
qwp.$('[data-rel=tooltip]', p).each(function (i, e) {
592615
e = $(e);
593616
if (!e.hasClass('tooltip-info')) e.addClass('tooltip-info');
594617
e.tooltip();

modules/users/ops_del.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33

44
function delete_user(&$msg, &$data) {
55
global $F;
6-
$ids = explode(',', $F);
7-
foreach ($ids as $id) {
8-
if (!is_digits($id) || $id == '1') {
9-
return false;
10-
}
6+
7+
$msg = L('Invalid parameters');
8+
if (!get_joined_digits($F, $ids)) {
9+
return false;
1110
}
1211
db_delete('qwp_user')->condition('id', $ids, 'in')->execute();
1312
$msg = L('Delete selected users successfully');

test.html renamed to sample.html

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
r: function(f){qwp._r.push(f);},
1414
isEmpty: function(o){return !o || !o.length;}
1515
};
16+
function numInputBlur(o) {
17+
$(o).blur();
18+
}
1619
</script>
1720
</head>
1821
<body>
@@ -29,8 +32,14 @@
2932
</qwp>
3033
<div id="test_ctl" width="100px" style="background-color: #ddd;padding: 10px;margin-left: 10px;margin-right: 10px;position: relative">
3134
<div id="test_radio"></div>
32-
<a id="test_uri" href="#">URI</a><br />
33-
<a href="#" onclick="qwp.loading.gif.show('#test_ctl')">Show loading image</a><br /><a href="#" onclick="qwp.loading.gif.hide('#test_ctl')">Hide loading image</a>
35+
<a id="test_uri" href="#">URI API sample</a><br />
36+
<a href="#" onclick="qwp.loading.gif.show('#test_ctl')">Show loading image</a><br /><a href="#" onclick="qwp.loading.gif.hide('#test_ctl')">Hide loading image</a><br>
37+
</div>
38+
<br>
39+
<div style="background-color: #ddd;padding: 10px;margin-left: 10px;margin-right: 10px;position: relative">
40+
<a href="#" onclick="alert('button1 click')" id="button1">Button1</a><br />
41+
<input qwp="number" type="text" props="enter=numInputBlur|defaultValue=5|minValue=1|maxValue=10" value="5"> Press enter key will call function 'numInputBlur'<br>
42+
<input qwp="number" type="text" props="enter=$('#button1').click()|defaultValue=3" value="3"> Press enter key will trigger button1 click event <br>
3443
</div>
3544
<qwp tmpl="table-right-html">
3645
<div style="text-align: right;">
@@ -45,6 +54,7 @@
4554
</div>
4655
</qwp>
4756
<br/>
57+
&nbsp;Click the table header to stop table loading and hide progress bar.
4858
<div id="test_container" class="row qwp-table" style="margin-left: 50px;margin-right: 50px;"></div>
4959
<script src="js/jquery-1.11.3.min.js"></script>
5060
<script src="js/bootstrap.min.js"></script>

0 commit comments

Comments
 (0)