commit
3498666115
3 files changed
+291
-213
No files matched your search
@@ -7,11 +7,11 @@
|
||||
* Released under the MIT license
|
||||
*
|
||||
*/
|
||||
'use strict';
|
||||
if ($.web2py !== undefined) {
|
||||
$.error('web2py.js has already been loaded!');
|
||||
}
|
||||
|
||||
|
||||
String.prototype.reverse = function () {
|
||||
return this.split('').reverse().join('');
|
||||
};
|
||||
@@ -19,9 +19,13 @@
|
||||
|
||||
$.web2py = web2py = {
|
||||
|
||||
isUndefined: function (obj) {
|
||||
/* grabbed from underscore.js */
|
||||
return obj === void 0;
|
||||
},
|
||||
popup: function (url) {
|
||||
/* popup a window */
|
||||
newwindow = window.open(url, 'name', 'height=400,width=600');
|
||||
var newwindow = window.open(url, 'name', 'height=400,width=600');
|
||||
if (window.focus) newwindow.focus();
|
||||
return false;
|
||||
},
|
||||
@@ -36,33 +40,33 @@
|
||||
},
|
||||
ajax: function (u, s, t) {
|
||||
/*simple ajax function*/
|
||||
query = '';
|
||||
if(typeof s == "string") {
|
||||
d = $(s).serialize();
|
||||
var query = '';
|
||||
if (typeof s == 'string') {
|
||||
var d = $(s).serialize();
|
||||
if (d) {
|
||||
query = d;
|
||||
}
|
||||
} else {
|
||||
pcs = [];
|
||||
if(s != null && s != undefined)
|
||||
for(i = 0; i < s.length; i++) {
|
||||
q = $("[name=" + s[i] + "]").serialize();
|
||||
var pcs = [];
|
||||
if (s !== null && !web2py.isUndefined(s))
|
||||
for (var i = 0; i < s.length; i++) {
|
||||
var q = $('[name=' + s[i] + ']').serialize();
|
||||
if (q) {
|
||||
pcs.push(q);
|
||||
}
|
||||
}
|
||||
if (pcs.length > 0) {
|
||||
query = pcs.join("&");
|
||||
query = pcs.join('&');
|
||||
}
|
||||
}
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
type: 'POST',
|
||||
url: u,
|
||||
data: query,
|
||||
success: function (msg) {
|
||||
if (t) {
|
||||
if (t == ':eval') eval(msg);
|
||||
else if(typeof t == 'string') $("#" + t).html(msg);
|
||||
else if (typeof t == 'string') $('#' + t).html(msg);
|
||||
else t(msg);
|
||||
}
|
||||
}
|
||||
@@ -76,7 +80,9 @@
|
||||
*/
|
||||
/*adds btn class to buttons*/
|
||||
$('button:not([class^="btn"])', target).addClass('btn');
|
||||
$('form input[type="submit"]:not([class^="btn"]), form input[type="button"]:not([class^="btn"])', target).addClass('btn');
|
||||
$(
|
||||
'form input[type="submit"]:not([class^="btn"]), form input[type="button"]:not([class^="btn"])',
|
||||
target).addClass('btn');
|
||||
/* javascript for PasswordWidget*/
|
||||
$('input[type=password][data-w2p_entropy]', target).each(function () {
|
||||
web2py.validate_entropy($(this));
|
||||
@@ -93,7 +99,7 @@
|
||||
/* the line we clicked on was deleted, just add to end of list */
|
||||
new_line.appendTo(ul);
|
||||
}
|
||||
new_line.find(":text").focus();
|
||||
new_line.find(':text').focus();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -106,21 +112,22 @@
|
||||
|
||||
function ml(ul) {
|
||||
/* clone the first field */
|
||||
var line = $(ul).find("li:first").clone(true);
|
||||
var line = $(ul).find('li:first').clone(true);
|
||||
line.find(':text').val('');
|
||||
return line;
|
||||
}
|
||||
|
||||
function rel(ul) {
|
||||
/* keep only as many as needed*/
|
||||
$(ul).find("li").each(function() {
|
||||
$(ul).find('li').each(function () {
|
||||
var trimmed = $.trim($(this.firstChild).val());
|
||||
if(trimmed == '') $(this).remove();
|
||||
if (trimmed === '') $(this).remove();
|
||||
else $(this.firstChild).val(trimmed);
|
||||
});
|
||||
}
|
||||
var ul = this;
|
||||
$(ul).find(":text").after('<a href="#">+</a> <a href="#">-</a>').keypress(function(e) {
|
||||
$(ul).find(':text').after('<a href="#">+</a> <a href="#">-</a>').keypress(
|
||||
function (e) {
|
||||
return (e.which == 13) ? pe(ul, e) : true;
|
||||
}).next().click(function (e) {
|
||||
pe(ul, e);
|
||||
@@ -151,7 +158,7 @@
|
||||
eval(decodeURIComponent(command));
|
||||
}
|
||||
if (flash) {
|
||||
web2py.flash(decodeURIComponent(flash))
|
||||
web2py.flash(decodeURIComponent(flash));
|
||||
}
|
||||
},
|
||||
event_handlers: function () {
|
||||
@@ -161,7 +168,7 @@
|
||||
* and require no dom manipulations
|
||||
*/
|
||||
var doc = $(document);
|
||||
doc.on('click', '.w2p_flash', function(e) {
|
||||
doc.on('click', '.w2p_flash', function () {
|
||||
var t = $(this);
|
||||
if (t.css('top') == '0px') t.slideUp('slow');
|
||||
else t.fadeOut();
|
||||
@@ -171,36 +178,39 @@
|
||||
if (this.value != nvalue) this.value = nvalue;
|
||||
});
|
||||
doc.on('keyup', 'input.double, input.decimal', function () {
|
||||
var nvalue = this.value.reverse().replace(/[^0-9\-\.,]|[\-](?=.)|[\.,](?=[0-9]*[\.,])/g, '').reverse();
|
||||
var nvalue = this.value.reverse().replace(
|
||||
/[^0-9\-\.,]|[\-](?=.)|[\.,](?=[0-9]*[\.,])/g, '').reverse();
|
||||
if (this.value != nvalue) this.value = nvalue;
|
||||
});
|
||||
var confirm_message = (typeof w2p_ajax_confirm_message != 'undefined') ? w2p_ajax_confirm_message : "Are you sure you want to delete this object?";
|
||||
doc.on('click', "input[type='checkbox'].delete", function() {
|
||||
var confirm_message = !web2py.isUndefined(w2p_ajax_confirm_message) ? w2p_ajax_confirm_message :
|
||||
'Are you sure you want to delete this object?';
|
||||
doc.on('click', 'input[type="checkbox"].delete', function () {
|
||||
if (this.checked)
|
||||
if (!web2py.confirm(confirm_message)) this.checked = false;
|
||||
});
|
||||
var datetime_format = (typeof w2p_ajax_datetime_format != 'undefined') ? w2p_ajax_datetime_format : "%Y-%m-%d %H:%M:%S";
|
||||
doc.on('click', "input.datetime", function() {
|
||||
var datetime_format = !web2py.isUndefined(w2p_ajax_datetime_format) ? w2p_ajax_datetime_format :
|
||||
'%Y-%m-%d %H:%M:%S';
|
||||
doc.on('click', 'input.datetime', function () {
|
||||
var tformat = $(this).data('w2p_datetime_format');
|
||||
var active = $(this).data('w2p_datetime');
|
||||
var format = (typeof tformat != 'undefined') ? tformat : datetime_format;
|
||||
var format = !web2py.isUndefined(tformat) ? tformat : datetime_format;
|
||||
if (active === undefined) {
|
||||
Calendar.setup({
|
||||
inputField: this,
|
||||
ifFormat: format,
|
||||
showsTime: true,
|
||||
timeFormat: "24"
|
||||
timeFormat: '24'
|
||||
});
|
||||
$(this).attr('autocomplete', 'off');
|
||||
$(this).data('w2p_datetime', 1);
|
||||
$(this).trigger('click');
|
||||
}
|
||||
});
|
||||
var date_format = (typeof w2p_ajax_date_format != 'undefined') ? w2p_ajax_date_format : "%Y-%m-%d";
|
||||
doc.on('click', "input.date", function() {
|
||||
var date_format = !web2py.isUndefined(w2p_ajax_date_format) ? w2p_ajax_date_format : '%Y-%m-%d';
|
||||
doc.on('click', 'input.date', function () {
|
||||
var tformat = $(this).data('w2p_date_format');
|
||||
var active = $(this).data('w2p_date');
|
||||
var format = (typeof tformat != 'undefined') ? tformat : date_format;
|
||||
var format = !web2py.isUndefined(tformat) ? tformat : date_format;
|
||||
if (active === undefined) {
|
||||
Calendar.setup({
|
||||
inputField: this,
|
||||
@@ -212,9 +222,9 @@
|
||||
$(this).trigger('click');
|
||||
}
|
||||
});
|
||||
doc.on('focus', "input.time", function() {
|
||||
doc.on('focus', 'input.time', function () {
|
||||
var active = $(this).data('w2p_time');
|
||||
if(active === undefined) {
|
||||
if (web2py.isUndefined(active)) {
|
||||
$(this).timeEntry({
|
||||
spinnerImage: ''
|
||||
}).attr('autocomplete', 'off');
|
||||
@@ -225,16 +235,21 @@
|
||||
$(doc).on('submit', 'form', function () {
|
||||
var submit_button = $(this).find(web2py.formInputClickSelector);
|
||||
web2py.disableElement(submit_button);
|
||||
/* safeguard in case the form doesn't trigger a refresh,
|
||||
see https://github.com/web2py/web2py/issues/1100 */
|
||||
setTimeout(function () {
|
||||
web2py.enableElement(submit_button);
|
||||
}, 5000);
|
||||
});
|
||||
doc.ajaxSuccess(function (e, xhr) {
|
||||
var redirect = xhr.getResponseHeader('web2py-redirect-location');
|
||||
if (redirect !== null) {
|
||||
window.location = redirect;
|
||||
};
|
||||
}
|
||||
/* run this here only if this Ajax request is NOT for a web2py component. */
|
||||
if(xhr.getResponseHeader('web2py-component-content') == null) {
|
||||
if (xhr.getResponseHeader('web2py-component-content') === null) {
|
||||
web2py.after_ajax(xhr);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
doc.ajaxError(function (e, xhr, settings, exception) {
|
||||
@@ -252,21 +267,21 @@
|
||||
},
|
||||
trap_form: function (action, target) {
|
||||
/* traps any LOADed form */
|
||||
$('#' + target + ' form').each(function(i) {
|
||||
$('#' + target + ' form').each(function () {
|
||||
var form = $(this);
|
||||
if (form.hasClass('no_trap')) {
|
||||
return;
|
||||
}
|
||||
|
||||
var w2p_target = $(this).attr('data-w2p_target');
|
||||
if (typeof w2p_target === typeof undefined || w2p_target === false) {
|
||||
if (web2py.isUndefined(w2p_target) || w2p_target === false) {
|
||||
form.attr('data-w2p_target', target);
|
||||
} else {
|
||||
target = w2p_target;
|
||||
}
|
||||
|
||||
var url = form.attr('action');
|
||||
if((url === "") || (url === "#") || (typeof url === 'undefined')) {
|
||||
if ((url === '') || (url === '#') || web2py.isUndefined(url)) {
|
||||
/* form has no action. Use component url. */
|
||||
url = action;
|
||||
}
|
||||
@@ -280,9 +295,9 @@
|
||||
form.on('click', web2py.formInputClickSelector, function (e) {
|
||||
e.preventDefault();
|
||||
var input_name = $(this).attr('name');
|
||||
if(input_name != undefined) {
|
||||
if (!web2py.isUndefined(input_name)) {
|
||||
$('<input type="hidden" />').attr('name', input_name)
|
||||
.attr('value', $(this).val()).appendTo(form)
|
||||
.attr('value', $(this).val()).appendTo(form);
|
||||
}
|
||||
form.trigger('submit');
|
||||
});
|
||||
@@ -290,9 +305,9 @@
|
||||
},
|
||||
ajax_page: function (method, action, data, target, element) {
|
||||
/* element is a new parameter, but should be put be put in front */
|
||||
if(element == undefined) element = $(document);
|
||||
if (web2py.isUndefined(element)) element = $(document);
|
||||
/* if target is not there, fill it with something that there isn't in the page*/
|
||||
if(target == undefined || target == '') target = 'w2p_none';
|
||||
if (web2py.isUndefined(target) || target === '') target = 'w2p_none';
|
||||
if (web2py.fire(element, 'ajax:before', null, target)) { /*test a usecase, should stop here if returns false */
|
||||
$.ajax({
|
||||
'type': method,
|
||||
@@ -330,9 +345,9 @@
|
||||
component: function (action, target, timeout, times, el) {
|
||||
/* element is a new parameter, but should be put in front */
|
||||
$(function () {
|
||||
var jelement = $("#" + target);
|
||||
var jelement = $('#' + target);
|
||||
var element = jelement.get(0);
|
||||
var statement = "jQuery('#" + target + "').get(0).reload();";
|
||||
var statement = 'jQuery("#' + target + '").get(0).reload();';
|
||||
element.reload = function () {
|
||||
/* Continue if times is Infinity or
|
||||
* the times limit is not reached
|
||||
@@ -435,17 +450,17 @@
|
||||
}
|
||||
}
|
||||
var entropy = mystring.length * Math.log(score) / 0.6931471805599453;
|
||||
return Math.round(entropy * 100) / 100
|
||||
return Math.round(entropy * 100) / 100;
|
||||
},
|
||||
validate_entropy: function (myfield, req_entropy) {
|
||||
if(myfield.data('w2p_entropy') != undefined) req_entropy = myfield.data('w2p_entropy');
|
||||
if (!web2py.isUndefined(myfield.data('w2p_entropy'))) req_entropy = myfield.data('w2p_entropy');
|
||||
var validator = function () {
|
||||
var v = (web2py.calc_entropy(myfield.val()) || 0) / req_entropy;
|
||||
var r = 0,
|
||||
g = 0,
|
||||
b = 0,
|
||||
rs = function (x) {
|
||||
return Math.round(x * 15).toString(16)
|
||||
return Math.round(x * 15).toString(16);
|
||||
};
|
||||
if (v <= 0.5) {
|
||||
r = 1.0;
|
||||
@@ -456,13 +471,14 @@
|
||||
}
|
||||
var color = '#' + rs(r) + rs(g) + rs(b);
|
||||
myfield.css('background-color', color);
|
||||
entropy_callback = myfield.data('entropy_callback');
|
||||
var entropy_callback = myfield.data('entropy_callback');
|
||||
if (entropy_callback) entropy_callback(v);
|
||||
}
|
||||
if(!myfield.hasClass('entropy_check')) myfield.on('keyup', validator).on('keydown', validator).addClass('entropy_check');
|
||||
};
|
||||
if (!myfield.hasClass('entropy_check')) myfield.on('keyup', validator).on('keydown', validator)
|
||||
.addClass('entropy_check');
|
||||
},
|
||||
web2py_websocket: function (url, onmessage, onopen, onclose) {
|
||||
if("WebSocket" in window) {
|
||||
if ('WebSocket' in window) {
|
||||
var ws = new WebSocket(url);
|
||||
ws.onopen = onopen ? onopen : (function () {});
|
||||
ws.onmessage = onmessage;
|
||||
@@ -497,19 +513,24 @@
|
||||
/* replace element's html with the 'data-disable-with' after storing original html
|
||||
* and prevent clicking on it */
|
||||
disableElement: function (el) {
|
||||
if (!web2py.isUndefined(el.data('w2p_disable'))) {
|
||||
return false;
|
||||
}
|
||||
el.addClass('disabled');
|
||||
var method = el.is('input') ? 'val' : 'html';
|
||||
//method = el.attr('name') ? 'html' : 'val';
|
||||
var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working...";
|
||||
var disable_with_message = (!web2py.isUndefined(w2p_ajax_disable_with_message)) ?
|
||||
w2p_ajax_disable_with_message : 'Working...';
|
||||
/*store enabled state if not already disabled */
|
||||
if(el.data('w2p_enable_with') === undefined) {
|
||||
if (web2py.isUndefined(el.data('w2p_enable_with'))) {
|
||||
el.data('w2p_enable_with', el[method]());
|
||||
}
|
||||
/*if you don't want to see "working..." on buttons, replace the following
|
||||
* two lines with this one
|
||||
* el.data('w2p_disable_with', el[method]());
|
||||
*/
|
||||
if((el.data('w2p_disable_with') == 'default') || (el.data('w2p_disable_with') === undefined)) {
|
||||
if ((el.data('w2p_disable_with') == 'default') || (web2py.isUndefined(el.data(
|
||||
'w2p_disable_with')))) {
|
||||
el.data('w2p_disable_with', disable_with_message);
|
||||
}
|
||||
|
||||
@@ -524,7 +545,7 @@
|
||||
/* restore element to its original state which was disabled by 'disableElement' above*/
|
||||
enableElement: function (el) {
|
||||
var method = el.is('input') ? 'val' : 'html';
|
||||
if(el.data('w2p_enable_with') !== undefined) {
|
||||
if (!web2py.isUndefined(el.data('w2p_enable_with'))) {
|
||||
/* set to old enabled state */
|
||||
el[method](el.data('w2p_enable_with'));
|
||||
el.removeData('w2p_enable_with');
|
||||
@@ -570,7 +591,7 @@
|
||||
for (var name in triggers) {
|
||||
$('#' + name, target).change(show_if).keyup(show_if);
|
||||
show_if.call($('#' + name, target));
|
||||
};
|
||||
}
|
||||
},
|
||||
component_handler: function (target) {
|
||||
$('div[data-w2p_remote]', target).each(function () {
|
||||
@@ -581,7 +602,7 @@
|
||||
timeout = el.data('w2p_timeout');
|
||||
target = el.attr('id');
|
||||
web2py.component(remote, target, timeout, times, $(this));
|
||||
})
|
||||
});
|
||||
},
|
||||
a_handler: function (el, e) {
|
||||
e.preventDefault();
|
||||
@@ -591,19 +612,20 @@
|
||||
var confirm_message = el.data('w2p_confirm');
|
||||
|
||||
var pre_call = el.data('w2p_pre_call');
|
||||
if(pre_call != undefined) {
|
||||
if (!web2py.isUndefined(pre_call)) {
|
||||
eval(pre_call);
|
||||
}
|
||||
if (confirm_message) {
|
||||
if(confirm_message == 'default')
|
||||
if (confirm_message == 'default') {
|
||||
confirm_message = w2p_ajax_confirm_message ||
|
||||
'Are you sure you want to delete this object?';
|
||||
}
|
||||
if (!web2py.confirm(confirm_message)) {
|
||||
web2py.stopEverything(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(target == undefined) {
|
||||
if (web2py.isUndefined(target)) {
|
||||
if (method == 'GET') {
|
||||
web2py.ajax_page('get', action, [], '', el);
|
||||
} else if (method == 'POST') {
|
||||
@@ -623,10 +645,10 @@
|
||||
web2py.a_handler($(this), e);
|
||||
});
|
||||
/* removal of element should happen only on success */
|
||||
el.on('ajax:success', 'a[data-w2p_method][data-w2p_remove]', function(e) {
|
||||
el.on('ajax:success', 'a[data-w2p_method][data-w2p_remove]', function () {
|
||||
var el = $(this);
|
||||
var toremove = el.data('w2p_remove');
|
||||
if(toremove != undefined) {
|
||||
if (!web2py.isUndefined(toremove)) {
|
||||
toremove = el.closest(toremove);
|
||||
if (!toremove.length) {
|
||||
/*this enables removal of whatever selector if a closest is not found */
|
||||
@@ -635,11 +657,11 @@
|
||||
toremove.remove();
|
||||
}
|
||||
});
|
||||
el.on('ajax:beforeSend', 'a[data-w2p_method][data-w2p_disable_with]', function(e) {
|
||||
el.on('ajax:beforeSend', 'a[data-w2p_method][data-w2p_disable_with]', function () {
|
||||
web2py.disableElement($(this));
|
||||
});
|
||||
/*re-enable click on completion*/
|
||||
el.on('ajax:complete', 'a[data-w2p_method][data-w2p_disable_with]', function(e) {
|
||||
el.on('ajax:complete', 'a[data-w2p_method][data-w2p_disable_with]', function () {
|
||||
web2py.enableElement($(this));
|
||||
});
|
||||
},
|
||||
@@ -653,10 +675,14 @@
|
||||
var element = $(this),
|
||||
method = element.is('button') ? 'html' : 'val';
|
||||
var disable_with = element.data('w2p_disable_with');
|
||||
if(disable_with == undefined) {
|
||||
element.data('w2p_disable_with', element[method]())
|
||||
var disable = element.data('w2p_disable');
|
||||
if (!web2py.isUndefined(disable)) {
|
||||
return false;
|
||||
}
|
||||
if(element.data('w2p_enable_with') === undefined) {
|
||||
if (web2py.isUndefined(disable_with)) {
|
||||
element.data('w2p_disable_with', element[method]());
|
||||
}
|
||||
if (web2py.isUndefined(element.data('w2p_enable_with'))) {
|
||||
element.data('w2p_enable_with', element[method]());
|
||||
}
|
||||
element[method](element.data('w2p_disable_with'));
|
||||
@@ -681,10 +707,10 @@
|
||||
},
|
||||
form_handlers: function () {
|
||||
var el = $(document);
|
||||
el.on('ajax:beforeSend', 'form[data-w2p_target]', function(e) {
|
||||
el.on('ajax:beforeSend', 'form[data-w2p_target]', function () {
|
||||
web2py.disableFormElements($(this));
|
||||
});
|
||||
el.on('ajax:complete', 'form[data-w2p_target]', function(e) {
|
||||
el.on('ajax:complete', 'form[data-w2p_target]', function () {
|
||||
web2py.enableFormElements($(this));
|
||||
});
|
||||
},
|
||||
@@ -693,7 +719,7 @@
|
||||
invalidate: function (target) {
|
||||
$('div[data-w2p_remote]', target).each(function () {
|
||||
var el = $('#' + $(this).attr('id')).get(0);
|
||||
if(el.timing !== undefined) { // Block triggering regular routines
|
||||
if (!web2py.isUndefined(el.timing)) { // Block triggering regular routines
|
||||
clearInterval(el.timing);
|
||||
}
|
||||
});
|
||||
@@ -708,7 +734,7 @@
|
||||
web2py.a_handlers();
|
||||
web2py.form_handlers();
|
||||
}
|
||||
}
|
||||
};
|
||||
/*end of functions */
|
||||
/*main hook*/
|
||||
$(function () {
|
||||
|
||||
@@ -7,11 +7,11 @@
|
||||
* Released under the MIT license
|
||||
*
|
||||
*/
|
||||
'use strict';
|
||||
if ($.web2py !== undefined) {
|
||||
$.error('web2py.js has already been loaded!');
|
||||
}
|
||||
|
||||
|
||||
String.prototype.reverse = function () {
|
||||
return this.split('').reverse().join('');
|
||||
};
|
||||
@@ -19,9 +19,13 @@
|
||||
|
||||
$.web2py = web2py = {
|
||||
|
||||
isUndefined: function (obj) {
|
||||
/* grabbed from underscore.js */
|
||||
return obj === void 0;
|
||||
},
|
||||
popup: function (url) {
|
||||
/* popup a window */
|
||||
newwindow = window.open(url, 'name', 'height=400,width=600');
|
||||
var newwindow = window.open(url, 'name', 'height=400,width=600');
|
||||
if (window.focus) newwindow.focus();
|
||||
return false;
|
||||
},
|
||||
@@ -36,33 +40,33 @@
|
||||
},
|
||||
ajax: function (u, s, t) {
|
||||
/*simple ajax function*/
|
||||
query = '';
|
||||
if(typeof s == "string") {
|
||||
d = $(s).serialize();
|
||||
var query = '';
|
||||
if (typeof s == 'string') {
|
||||
var d = $(s).serialize();
|
||||
if (d) {
|
||||
query = d;
|
||||
}
|
||||
} else {
|
||||
pcs = [];
|
||||
if(s != null && s != undefined)
|
||||
for(i = 0; i < s.length; i++) {
|
||||
q = $("[name=" + s[i] + "]").serialize();
|
||||
var pcs = [];
|
||||
if (s !== null && !web2py.isUndefined(s))
|
||||
for (var i = 0; i < s.length; i++) {
|
||||
var q = $('[name=' + s[i] + ']').serialize();
|
||||
if (q) {
|
||||
pcs.push(q);
|
||||
}
|
||||
}
|
||||
if (pcs.length > 0) {
|
||||
query = pcs.join("&");
|
||||
query = pcs.join('&');
|
||||
}
|
||||
}
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
type: 'POST',
|
||||
url: u,
|
||||
data: query,
|
||||
success: function (msg) {
|
||||
if (t) {
|
||||
if (t == ':eval') eval(msg);
|
||||
else if(typeof t == 'string') $("#" + t).html(msg);
|
||||
else if (typeof t == 'string') $('#' + t).html(msg);
|
||||
else t(msg);
|
||||
}
|
||||
}
|
||||
@@ -76,7 +80,9 @@
|
||||
*/
|
||||
/*adds btn class to buttons*/
|
||||
$('button:not([class^="btn"])', target).addClass('btn');
|
||||
$('form input[type="submit"]:not([class^="btn"]), form input[type="button"]:not([class^="btn"])', target).addClass('btn');
|
||||
$(
|
||||
'form input[type="submit"]:not([class^="btn"]), form input[type="button"]:not([class^="btn"])',
|
||||
target).addClass('btn');
|
||||
/* javascript for PasswordWidget*/
|
||||
$('input[type=password][data-w2p_entropy]', target).each(function () {
|
||||
web2py.validate_entropy($(this));
|
||||
@@ -93,7 +99,7 @@
|
||||
/* the line we clicked on was deleted, just add to end of list */
|
||||
new_line.appendTo(ul);
|
||||
}
|
||||
new_line.find(":text").focus();
|
||||
new_line.find(':text').focus();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -106,21 +112,22 @@
|
||||
|
||||
function ml(ul) {
|
||||
/* clone the first field */
|
||||
var line = $(ul).find("li:first").clone(true);
|
||||
var line = $(ul).find('li:first').clone(true);
|
||||
line.find(':text').val('');
|
||||
return line;
|
||||
}
|
||||
|
||||
function rel(ul) {
|
||||
/* keep only as many as needed*/
|
||||
$(ul).find("li").each(function() {
|
||||
$(ul).find('li').each(function () {
|
||||
var trimmed = $.trim($(this.firstChild).val());
|
||||
if(trimmed == '') $(this).remove();
|
||||
if (trimmed === '') $(this).remove();
|
||||
else $(this.firstChild).val(trimmed);
|
||||
});
|
||||
}
|
||||
var ul = this;
|
||||
$(ul).find(":text").after('<a href="#">+</a> <a href="#">-</a>').keypress(function(e) {
|
||||
$(ul).find(':text').after('<a href="#">+</a> <a href="#">-</a>').keypress(
|
||||
function (e) {
|
||||
return (e.which == 13) ? pe(ul, e) : true;
|
||||
}).next().click(function (e) {
|
||||
pe(ul, e);
|
||||
@@ -151,7 +158,7 @@
|
||||
eval(decodeURIComponent(command));
|
||||
}
|
||||
if (flash) {
|
||||
web2py.flash(decodeURIComponent(flash))
|
||||
web2py.flash(decodeURIComponent(flash));
|
||||
}
|
||||
},
|
||||
event_handlers: function () {
|
||||
@@ -161,7 +168,7 @@
|
||||
* and require no dom manipulations
|
||||
*/
|
||||
var doc = $(document);
|
||||
doc.on('click', '.w2p_flash', function(e) {
|
||||
doc.on('click', '.w2p_flash', function () {
|
||||
var t = $(this);
|
||||
if (t.css('top') == '0px') t.slideUp('slow');
|
||||
else t.fadeOut();
|
||||
@@ -171,36 +178,39 @@
|
||||
if (this.value != nvalue) this.value = nvalue;
|
||||
});
|
||||
doc.on('keyup', 'input.double, input.decimal', function () {
|
||||
var nvalue = this.value.reverse().replace(/[^0-9\-\.,]|[\-](?=.)|[\.,](?=[0-9]*[\.,])/g, '').reverse();
|
||||
var nvalue = this.value.reverse().replace(
|
||||
/[^0-9\-\.,]|[\-](?=.)|[\.,](?=[0-9]*[\.,])/g, '').reverse();
|
||||
if (this.value != nvalue) this.value = nvalue;
|
||||
});
|
||||
var confirm_message = (typeof w2p_ajax_confirm_message != 'undefined') ? w2p_ajax_confirm_message : "Are you sure you want to delete this object?";
|
||||
doc.on('click', "input[type='checkbox'].delete", function() {
|
||||
var confirm_message = !web2py.isUndefined(w2p_ajax_confirm_message) ? w2p_ajax_confirm_message :
|
||||
'Are you sure you want to delete this object?';
|
||||
doc.on('click', 'input[type="checkbox"].delete', function () {
|
||||
if (this.checked)
|
||||
if (!web2py.confirm(confirm_message)) this.checked = false;
|
||||
});
|
||||
var datetime_format = (typeof w2p_ajax_datetime_format != 'undefined') ? w2p_ajax_datetime_format : "%Y-%m-%d %H:%M:%S";
|
||||
doc.on('click', "input.datetime", function() {
|
||||
var datetime_format = !web2py.isUndefined(w2p_ajax_datetime_format) ? w2p_ajax_datetime_format :
|
||||
'%Y-%m-%d %H:%M:%S';
|
||||
doc.on('click', 'input.datetime', function () {
|
||||
var tformat = $(this).data('w2p_datetime_format');
|
||||
var active = $(this).data('w2p_datetime');
|
||||
var format = (typeof tformat != 'undefined') ? tformat : datetime_format;
|
||||
var format = !web2py.isUndefined(tformat) ? tformat : datetime_format;
|
||||
if (active === undefined) {
|
||||
Calendar.setup({
|
||||
inputField: this,
|
||||
ifFormat: format,
|
||||
showsTime: true,
|
||||
timeFormat: "24"
|
||||
timeFormat: '24'
|
||||
});
|
||||
$(this).attr('autocomplete', 'off');
|
||||
$(this).data('w2p_datetime', 1);
|
||||
$(this).trigger('click');
|
||||
}
|
||||
});
|
||||
var date_format = (typeof w2p_ajax_date_format != 'undefined') ? w2p_ajax_date_format : "%Y-%m-%d";
|
||||
doc.on('click', "input.date", function() {
|
||||
var date_format = !web2py.isUndefined(w2p_ajax_date_format) ? w2p_ajax_date_format : '%Y-%m-%d';
|
||||
doc.on('click', 'input.date', function () {
|
||||
var tformat = $(this).data('w2p_date_format');
|
||||
var active = $(this).data('w2p_date');
|
||||
var format = (typeof tformat != 'undefined') ? tformat : date_format;
|
||||
var format = !web2py.isUndefined(tformat) ? tformat : date_format;
|
||||
if (active === undefined) {
|
||||
Calendar.setup({
|
||||
inputField: this,
|
||||
@@ -212,9 +222,9 @@
|
||||
$(this).trigger('click');
|
||||
}
|
||||
});
|
||||
doc.on('focus', "input.time", function() {
|
||||
doc.on('focus', 'input.time', function () {
|
||||
var active = $(this).data('w2p_time');
|
||||
if(active === undefined) {
|
||||
if (web2py.isUndefined(active)) {
|
||||
$(this).timeEntry({
|
||||
spinnerImage: ''
|
||||
}).attr('autocomplete', 'off');
|
||||
@@ -225,16 +235,21 @@
|
||||
$(doc).on('submit', 'form', function () {
|
||||
var submit_button = $(this).find(web2py.formInputClickSelector);
|
||||
web2py.disableElement(submit_button);
|
||||
/* safeguard in case the form doesn't trigger a refresh,
|
||||
see https://github.com/web2py/web2py/issues/1100 */
|
||||
setTimeout(function () {
|
||||
web2py.enableElement(submit_button);
|
||||
}, 5000);
|
||||
});
|
||||
doc.ajaxSuccess(function (e, xhr) {
|
||||
var redirect = xhr.getResponseHeader('web2py-redirect-location');
|
||||
if (redirect !== null) {
|
||||
window.location = redirect;
|
||||
};
|
||||
}
|
||||
/* run this here only if this Ajax request is NOT for a web2py component. */
|
||||
if(xhr.getResponseHeader('web2py-component-content') == null) {
|
||||
if (xhr.getResponseHeader('web2py-component-content') === null) {
|
||||
web2py.after_ajax(xhr);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
doc.ajaxError(function (e, xhr, settings, exception) {
|
||||
@@ -252,21 +267,21 @@
|
||||
},
|
||||
trap_form: function (action, target) {
|
||||
/* traps any LOADed form */
|
||||
$('#' + target + ' form').each(function(i) {
|
||||
$('#' + target + ' form').each(function () {
|
||||
var form = $(this);
|
||||
if (form.hasClass('no_trap')) {
|
||||
return;
|
||||
}
|
||||
|
||||
var w2p_target = $(this).attr('data-w2p_target');
|
||||
if (typeof w2p_target === typeof undefined || w2p_target === false) {
|
||||
if (web2py.isUndefined(w2p_target) || w2p_target === false) {
|
||||
form.attr('data-w2p_target', target);
|
||||
} else {
|
||||
target = w2p_target;
|
||||
}
|
||||
|
||||
var url = form.attr('action');
|
||||
if((url === "") || (url === "#") || (typeof url === 'undefined')) {
|
||||
if ((url === '') || (url === '#') || web2py.isUndefined(url)) {
|
||||
/* form has no action. Use component url. */
|
||||
url = action;
|
||||
}
|
||||
@@ -280,9 +295,9 @@
|
||||
form.on('click', web2py.formInputClickSelector, function (e) {
|
||||
e.preventDefault();
|
||||
var input_name = $(this).attr('name');
|
||||
if(input_name != undefined) {
|
||||
if (!web2py.isUndefined(input_name)) {
|
||||
$('<input type="hidden" />').attr('name', input_name)
|
||||
.attr('value', $(this).val()).appendTo(form)
|
||||
.attr('value', $(this).val()).appendTo(form);
|
||||
}
|
||||
form.trigger('submit');
|
||||
});
|
||||
@@ -290,9 +305,9 @@
|
||||
},
|
||||
ajax_page: function (method, action, data, target, element) {
|
||||
/* element is a new parameter, but should be put be put in front */
|
||||
if(element == undefined) element = $(document);
|
||||
if (web2py.isUndefined(element)) element = $(document);
|
||||
/* if target is not there, fill it with something that there isn't in the page*/
|
||||
if(target == undefined || target == '') target = 'w2p_none';
|
||||
if (web2py.isUndefined(target) || target === '') target = 'w2p_none';
|
||||
if (web2py.fire(element, 'ajax:before', null, target)) { /*test a usecase, should stop here if returns false */
|
||||
$.ajax({
|
||||
'type': method,
|
||||
@@ -330,9 +345,9 @@
|
||||
component: function (action, target, timeout, times, el) {
|
||||
/* element is a new parameter, but should be put in front */
|
||||
$(function () {
|
||||
var jelement = $("#" + target);
|
||||
var jelement = $('#' + target);
|
||||
var element = jelement.get(0);
|
||||
var statement = "jQuery('#" + target + "').get(0).reload();";
|
||||
var statement = 'jQuery("#' + target + '").get(0).reload();';
|
||||
element.reload = function () {
|
||||
/* Continue if times is Infinity or
|
||||
* the times limit is not reached
|
||||
@@ -435,17 +450,17 @@
|
||||
}
|
||||
}
|
||||
var entropy = mystring.length * Math.log(score) / 0.6931471805599453;
|
||||
return Math.round(entropy * 100) / 100
|
||||
return Math.round(entropy * 100) / 100;
|
||||
},
|
||||
validate_entropy: function (myfield, req_entropy) {
|
||||
if(myfield.data('w2p_entropy') != undefined) req_entropy = myfield.data('w2p_entropy');
|
||||
if (!web2py.isUndefined(myfield.data('w2p_entropy'))) req_entropy = myfield.data('w2p_entropy');
|
||||
var validator = function () {
|
||||
var v = (web2py.calc_entropy(myfield.val()) || 0) / req_entropy;
|
||||
var r = 0,
|
||||
g = 0,
|
||||
b = 0,
|
||||
rs = function (x) {
|
||||
return Math.round(x * 15).toString(16)
|
||||
return Math.round(x * 15).toString(16);
|
||||
};
|
||||
if (v <= 0.5) {
|
||||
r = 1.0;
|
||||
@@ -456,13 +471,14 @@
|
||||
}
|
||||
var color = '#' + rs(r) + rs(g) + rs(b);
|
||||
myfield.css('background-color', color);
|
||||
entropy_callback = myfield.data('entropy_callback');
|
||||
var entropy_callback = myfield.data('entropy_callback');
|
||||
if (entropy_callback) entropy_callback(v);
|
||||
}
|
||||
if(!myfield.hasClass('entropy_check')) myfield.on('keyup', validator).on('keydown', validator).addClass('entropy_check');
|
||||
};
|
||||
if (!myfield.hasClass('entropy_check')) myfield.on('keyup', validator).on('keydown', validator)
|
||||
.addClass('entropy_check');
|
||||
},
|
||||
web2py_websocket: function (url, onmessage, onopen, onclose) {
|
||||
if("WebSocket" in window) {
|
||||
if ('WebSocket' in window) {
|
||||
var ws = new WebSocket(url);
|
||||
ws.onopen = onopen ? onopen : (function () {});
|
||||
ws.onmessage = onmessage;
|
||||
@@ -497,19 +513,24 @@
|
||||
/* replace element's html with the 'data-disable-with' after storing original html
|
||||
* and prevent clicking on it */
|
||||
disableElement: function (el) {
|
||||
if (!web2py.isUndefined(el.data('w2p_disable'))) {
|
||||
return false;
|
||||
}
|
||||
el.addClass('disabled');
|
||||
var method = el.is('input') ? 'val' : 'html';
|
||||
//method = el.attr('name') ? 'html' : 'val';
|
||||
var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working...";
|
||||
var disable_with_message = (!web2py.isUndefined(w2p_ajax_disable_with_message)) ?
|
||||
w2p_ajax_disable_with_message : 'Working...';
|
||||
/*store enabled state if not already disabled */
|
||||
if(el.data('w2p_enable_with') === undefined) {
|
||||
if (web2py.isUndefined(el.data('w2p_enable_with'))) {
|
||||
el.data('w2p_enable_with', el[method]());
|
||||
}
|
||||
/*if you don't want to see "working..." on buttons, replace the following
|
||||
* two lines with this one
|
||||
* el.data('w2p_disable_with', el[method]());
|
||||
*/
|
||||
if((el.data('w2p_disable_with') == 'default') || (el.data('w2p_disable_with') === undefined)) {
|
||||
if ((el.data('w2p_disable_with') == 'default') || (web2py.isUndefined(el.data(
|
||||
'w2p_disable_with')))) {
|
||||
el.data('w2p_disable_with', disable_with_message);
|
||||
}
|
||||
|
||||
@@ -524,7 +545,7 @@
|
||||
/* restore element to its original state which was disabled by 'disableElement' above*/
|
||||
enableElement: function (el) {
|
||||
var method = el.is('input') ? 'val' : 'html';
|
||||
if(el.data('w2p_enable_with') !== undefined) {
|
||||
if (!web2py.isUndefined(el.data('w2p_enable_with'))) {
|
||||
/* set to old enabled state */
|
||||
el[method](el.data('w2p_enable_with'));
|
||||
el.removeData('w2p_enable_with');
|
||||
@@ -570,7 +591,7 @@
|
||||
for (var name in triggers) {
|
||||
$('#' + name, target).change(show_if).keyup(show_if);
|
||||
show_if.call($('#' + name, target));
|
||||
};
|
||||
}
|
||||
},
|
||||
component_handler: function (target) {
|
||||
$('div[data-w2p_remote]', target).each(function () {
|
||||
@@ -581,7 +602,7 @@
|
||||
timeout = el.data('w2p_timeout');
|
||||
target = el.attr('id');
|
||||
web2py.component(remote, target, timeout, times, $(this));
|
||||
})
|
||||
});
|
||||
},
|
||||
a_handler: function (el, e) {
|
||||
e.preventDefault();
|
||||
@@ -591,19 +612,20 @@
|
||||
var confirm_message = el.data('w2p_confirm');
|
||||
|
||||
var pre_call = el.data('w2p_pre_call');
|
||||
if(pre_call != undefined) {
|
||||
if (!web2py.isUndefined(pre_call)) {
|
||||
eval(pre_call);
|
||||
}
|
||||
if (confirm_message) {
|
||||
if(confirm_message == 'default')
|
||||
if (confirm_message == 'default') {
|
||||
confirm_message = w2p_ajax_confirm_message ||
|
||||
'Are you sure you want to delete this object?';
|
||||
}
|
||||
if (!web2py.confirm(confirm_message)) {
|
||||
web2py.stopEverything(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(target == undefined) {
|
||||
if (web2py.isUndefined(target)) {
|
||||
if (method == 'GET') {
|
||||
web2py.ajax_page('get', action, [], '', el);
|
||||
} else if (method == 'POST') {
|
||||
@@ -623,10 +645,10 @@
|
||||
web2py.a_handler($(this), e);
|
||||
});
|
||||
/* removal of element should happen only on success */
|
||||
el.on('ajax:success', 'a[data-w2p_method][data-w2p_remove]', function(e) {
|
||||
el.on('ajax:success', 'a[data-w2p_method][data-w2p_remove]', function () {
|
||||
var el = $(this);
|
||||
var toremove = el.data('w2p_remove');
|
||||
if(toremove != undefined) {
|
||||
if (!web2py.isUndefined(toremove)) {
|
||||
toremove = el.closest(toremove);
|
||||
if (!toremove.length) {
|
||||
/*this enables removal of whatever selector if a closest is not found */
|
||||
@@ -635,11 +657,11 @@
|
||||
toremove.remove();
|
||||
}
|
||||
});
|
||||
el.on('ajax:beforeSend', 'a[data-w2p_method][data-w2p_disable_with]', function(e) {
|
||||
el.on('ajax:beforeSend', 'a[data-w2p_method][data-w2p_disable_with]', function () {
|
||||
web2py.disableElement($(this));
|
||||
});
|
||||
/*re-enable click on completion*/
|
||||
el.on('ajax:complete', 'a[data-w2p_method][data-w2p_disable_with]', function(e) {
|
||||
el.on('ajax:complete', 'a[data-w2p_method][data-w2p_disable_with]', function () {
|
||||
web2py.enableElement($(this));
|
||||
});
|
||||
},
|
||||
@@ -653,10 +675,14 @@
|
||||
var element = $(this),
|
||||
method = element.is('button') ? 'html' : 'val';
|
||||
var disable_with = element.data('w2p_disable_with');
|
||||
if(disable_with == undefined) {
|
||||
element.data('w2p_disable_with', element[method]())
|
||||
var disable = element.data('w2p_disable');
|
||||
if (!web2py.isUndefined(disable)) {
|
||||
return false;
|
||||
}
|
||||
if(element.data('w2p_enable_with') === undefined) {
|
||||
if (web2py.isUndefined(disable_with)) {
|
||||
element.data('w2p_disable_with', element[method]());
|
||||
}
|
||||
if (web2py.isUndefined(element.data('w2p_enable_with'))) {
|
||||
element.data('w2p_enable_with', element[method]());
|
||||
}
|
||||
element[method](element.data('w2p_disable_with'));
|
||||
@@ -681,10 +707,10 @@
|
||||
},
|
||||
form_handlers: function () {
|
||||
var el = $(document);
|
||||
el.on('ajax:beforeSend', 'form[data-w2p_target]', function(e) {
|
||||
el.on('ajax:beforeSend', 'form[data-w2p_target]', function () {
|
||||
web2py.disableFormElements($(this));
|
||||
});
|
||||
el.on('ajax:complete', 'form[data-w2p_target]', function(e) {
|
||||
el.on('ajax:complete', 'form[data-w2p_target]', function () {
|
||||
web2py.enableFormElements($(this));
|
||||
});
|
||||
},
|
||||
@@ -693,7 +719,7 @@
|
||||
invalidate: function (target) {
|
||||
$('div[data-w2p_remote]', target).each(function () {
|
||||
var el = $('#' + $(this).attr('id')).get(0);
|
||||
if(el.timing !== undefined) { // Block triggering regular routines
|
||||
if (!web2py.isUndefined(el.timing)) { // Block triggering regular routines
|
||||
clearInterval(el.timing);
|
||||
}
|
||||
});
|
||||
@@ -708,7 +734,7 @@
|
||||
web2py.a_handlers();
|
||||
web2py.form_handlers();
|
||||
}
|
||||
}
|
||||
};
|
||||
/*end of functions */
|
||||
/*main hook*/
|
||||
$(function () {
|
||||
|
||||
@@ -7,11 +7,11 @@
|
||||
* Released under the MIT license
|
||||
*
|
||||
*/
|
||||
'use strict';
|
||||
if ($.web2py !== undefined) {
|
||||
$.error('web2py.js has already been loaded!');
|
||||
}
|
||||
|
||||
|
||||
String.prototype.reverse = function () {
|
||||
return this.split('').reverse().join('');
|
||||
};
|
||||
@@ -19,9 +19,13 @@
|
||||
|
||||
$.web2py = web2py = {
|
||||
|
||||
isUndefined: function (obj) {
|
||||
/* grabbed from underscore.js */
|
||||
return obj === void 0;
|
||||
},
|
||||
popup: function (url) {
|
||||
/* popup a window */
|
||||
newwindow = window.open(url, 'name', 'height=400,width=600');
|
||||
var newwindow = window.open(url, 'name', 'height=400,width=600');
|
||||
if (window.focus) newwindow.focus();
|
||||
return false;
|
||||
},
|
||||
@@ -36,33 +40,33 @@
|
||||
},
|
||||
ajax: function (u, s, t) {
|
||||
/*simple ajax function*/
|
||||
query = '';
|
||||
if(typeof s == "string") {
|
||||
d = $(s).serialize();
|
||||
var query = '';
|
||||
if (typeof s == 'string') {
|
||||
var d = $(s).serialize();
|
||||
if (d) {
|
||||
query = d;
|
||||
}
|
||||
} else {
|
||||
pcs = [];
|
||||
if(s != null && s != undefined)
|
||||
for(i = 0; i < s.length; i++) {
|
||||
q = $("[name=" + s[i] + "]").serialize();
|
||||
var pcs = [];
|
||||
if (s !== null && !web2py.isUndefined(s))
|
||||
for (var i = 0; i < s.length; i++) {
|
||||
var q = $('[name=' + s[i] + ']').serialize();
|
||||
if (q) {
|
||||
pcs.push(q);
|
||||
}
|
||||
}
|
||||
if (pcs.length > 0) {
|
||||
query = pcs.join("&");
|
||||
query = pcs.join('&');
|
||||
}
|
||||
}
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
type: 'POST',
|
||||
url: u,
|
||||
data: query,
|
||||
success: function (msg) {
|
||||
if (t) {
|
||||
if (t == ':eval') eval(msg);
|
||||
else if(typeof t == 'string') $("#" + t).html(msg);
|
||||
else if (typeof t == 'string') $('#' + t).html(msg);
|
||||
else t(msg);
|
||||
}
|
||||
}
|
||||
@@ -76,7 +80,9 @@
|
||||
*/
|
||||
/*adds btn class to buttons*/
|
||||
$('button:not([class^="btn"])', target).addClass('btn');
|
||||
$('form input[type="submit"]:not([class^="btn"]), form input[type="button"]:not([class^="btn"])', target).addClass('btn');
|
||||
$(
|
||||
'form input[type="submit"]:not([class^="btn"]), form input[type="button"]:not([class^="btn"])',
|
||||
target).addClass('btn');
|
||||
/* javascript for PasswordWidget*/
|
||||
$('input[type=password][data-w2p_entropy]', target).each(function () {
|
||||
web2py.validate_entropy($(this));
|
||||
@@ -93,7 +99,7 @@
|
||||
/* the line we clicked on was deleted, just add to end of list */
|
||||
new_line.appendTo(ul);
|
||||
}
|
||||
new_line.find(":text").focus();
|
||||
new_line.find(':text').focus();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -106,21 +112,22 @@
|
||||
|
||||
function ml(ul) {
|
||||
/* clone the first field */
|
||||
var line = $(ul).find("li:first").clone(true);
|
||||
var line = $(ul).find('li:first').clone(true);
|
||||
line.find(':text').val('');
|
||||
return line;
|
||||
}
|
||||
|
||||
function rel(ul) {
|
||||
/* keep only as many as needed*/
|
||||
$(ul).find("li").each(function() {
|
||||
$(ul).find('li').each(function () {
|
||||
var trimmed = $.trim($(this.firstChild).val());
|
||||
if(trimmed == '') $(this).remove();
|
||||
if (trimmed === '') $(this).remove();
|
||||
else $(this.firstChild).val(trimmed);
|
||||
});
|
||||
}
|
||||
var ul = this;
|
||||
$(ul).find(":text").after('<a href="#">+</a> <a href="#">-</a>').keypress(function(e) {
|
||||
$(ul).find(':text').after('<a href="#">+</a> <a href="#">-</a>').keypress(
|
||||
function (e) {
|
||||
return (e.which == 13) ? pe(ul, e) : true;
|
||||
}).next().click(function (e) {
|
||||
pe(ul, e);
|
||||
@@ -151,7 +158,7 @@
|
||||
eval(decodeURIComponent(command));
|
||||
}
|
||||
if (flash) {
|
||||
web2py.flash(decodeURIComponent(flash))
|
||||
web2py.flash(decodeURIComponent(flash));
|
||||
}
|
||||
},
|
||||
event_handlers: function () {
|
||||
@@ -161,7 +168,7 @@
|
||||
* and require no dom manipulations
|
||||
*/
|
||||
var doc = $(document);
|
||||
doc.on('click', '.w2p_flash', function(e) {
|
||||
doc.on('click', '.w2p_flash', function () {
|
||||
var t = $(this);
|
||||
if (t.css('top') == '0px') t.slideUp('slow');
|
||||
else t.fadeOut();
|
||||
@@ -171,36 +178,39 @@
|
||||
if (this.value != nvalue) this.value = nvalue;
|
||||
});
|
||||
doc.on('keyup', 'input.double, input.decimal', function () {
|
||||
var nvalue = this.value.reverse().replace(/[^0-9\-\.,]|[\-](?=.)|[\.,](?=[0-9]*[\.,])/g, '').reverse();
|
||||
var nvalue = this.value.reverse().replace(
|
||||
/[^0-9\-\.,]|[\-](?=.)|[\.,](?=[0-9]*[\.,])/g, '').reverse();
|
||||
if (this.value != nvalue) this.value = nvalue;
|
||||
});
|
||||
var confirm_message = (typeof w2p_ajax_confirm_message != 'undefined') ? w2p_ajax_confirm_message : "Are you sure you want to delete this object?";
|
||||
doc.on('click', "input[type='checkbox'].delete", function() {
|
||||
var confirm_message = !web2py.isUndefined(w2p_ajax_confirm_message) ? w2p_ajax_confirm_message :
|
||||
'Are you sure you want to delete this object?';
|
||||
doc.on('click', 'input[type="checkbox"].delete', function () {
|
||||
if (this.checked)
|
||||
if (!web2py.confirm(confirm_message)) this.checked = false;
|
||||
});
|
||||
var datetime_format = (typeof w2p_ajax_datetime_format != 'undefined') ? w2p_ajax_datetime_format : "%Y-%m-%d %H:%M:%S";
|
||||
doc.on('click', "input.datetime", function() {
|
||||
var datetime_format = !web2py.isUndefined(w2p_ajax_datetime_format) ? w2p_ajax_datetime_format :
|
||||
'%Y-%m-%d %H:%M:%S';
|
||||
doc.on('click', 'input.datetime', function () {
|
||||
var tformat = $(this).data('w2p_datetime_format');
|
||||
var active = $(this).data('w2p_datetime');
|
||||
var format = (typeof tformat != 'undefined') ? tformat : datetime_format;
|
||||
var format = !web2py.isUndefined(tformat) ? tformat : datetime_format;
|
||||
if (active === undefined) {
|
||||
Calendar.setup({
|
||||
inputField: this,
|
||||
ifFormat: format,
|
||||
showsTime: true,
|
||||
timeFormat: "24"
|
||||
timeFormat: '24'
|
||||
});
|
||||
$(this).attr('autocomplete', 'off');
|
||||
$(this).data('w2p_datetime', 1);
|
||||
$(this).trigger('click');
|
||||
}
|
||||
});
|
||||
var date_format = (typeof w2p_ajax_date_format != 'undefined') ? w2p_ajax_date_format : "%Y-%m-%d";
|
||||
doc.on('click', "input.date", function() {
|
||||
var date_format = !web2py.isUndefined(w2p_ajax_date_format) ? w2p_ajax_date_format : '%Y-%m-%d';
|
||||
doc.on('click', 'input.date', function () {
|
||||
var tformat = $(this).data('w2p_date_format');
|
||||
var active = $(this).data('w2p_date');
|
||||
var format = (typeof tformat != 'undefined') ? tformat : date_format;
|
||||
var format = !web2py.isUndefined(tformat) ? tformat : date_format;
|
||||
if (active === undefined) {
|
||||
Calendar.setup({
|
||||
inputField: this,
|
||||
@@ -212,9 +222,9 @@
|
||||
$(this).trigger('click');
|
||||
}
|
||||
});
|
||||
doc.on('focus', "input.time", function() {
|
||||
doc.on('focus', 'input.time', function () {
|
||||
var active = $(this).data('w2p_time');
|
||||
if(active === undefined) {
|
||||
if (web2py.isUndefined(active)) {
|
||||
$(this).timeEntry({
|
||||
spinnerImage: ''
|
||||
}).attr('autocomplete', 'off');
|
||||
@@ -225,16 +235,21 @@
|
||||
$(doc).on('submit', 'form', function () {
|
||||
var submit_button = $(this).find(web2py.formInputClickSelector);
|
||||
web2py.disableElement(submit_button);
|
||||
/* safeguard in case the form doesn't trigger a refresh,
|
||||
see https://github.com/web2py/web2py/issues/1100 */
|
||||
setTimeout(function () {
|
||||
web2py.enableElement(submit_button);
|
||||
}, 5000);
|
||||
});
|
||||
doc.ajaxSuccess(function (e, xhr) {
|
||||
var redirect = xhr.getResponseHeader('web2py-redirect-location');
|
||||
if (redirect !== null) {
|
||||
window.location = redirect;
|
||||
};
|
||||
}
|
||||
/* run this here only if this Ajax request is NOT for a web2py component. */
|
||||
if(xhr.getResponseHeader('web2py-component-content') == null) {
|
||||
if (xhr.getResponseHeader('web2py-component-content') === null) {
|
||||
web2py.after_ajax(xhr);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
doc.ajaxError(function (e, xhr, settings, exception) {
|
||||
@@ -252,21 +267,21 @@
|
||||
},
|
||||
trap_form: function (action, target) {
|
||||
/* traps any LOADed form */
|
||||
$('#' + target + ' form').each(function(i) {
|
||||
$('#' + target + ' form').each(function () {
|
||||
var form = $(this);
|
||||
if (form.hasClass('no_trap')) {
|
||||
return;
|
||||
}
|
||||
|
||||
var w2p_target = $(this).attr('data-w2p_target');
|
||||
if (typeof w2p_target === typeof undefined || w2p_target === false) {
|
||||
if (web2py.isUndefined(w2p_target) || w2p_target === false) {
|
||||
form.attr('data-w2p_target', target);
|
||||
} else {
|
||||
target = w2p_target;
|
||||
}
|
||||
|
||||
var url = form.attr('action');
|
||||
if((url === "") || (url === "#") || (typeof url === 'undefined')) {
|
||||
if ((url === '') || (url === '#') || web2py.isUndefined(url)) {
|
||||
/* form has no action. Use component url. */
|
||||
url = action;
|
||||
}
|
||||
@@ -280,9 +295,9 @@
|
||||
form.on('click', web2py.formInputClickSelector, function (e) {
|
||||
e.preventDefault();
|
||||
var input_name = $(this).attr('name');
|
||||
if(input_name != undefined) {
|
||||
if (!web2py.isUndefined(input_name)) {
|
||||
$('<input type="hidden" />').attr('name', input_name)
|
||||
.attr('value', $(this).val()).appendTo(form)
|
||||
.attr('value', $(this).val()).appendTo(form);
|
||||
}
|
||||
form.trigger('submit');
|
||||
});
|
||||
@@ -290,9 +305,9 @@
|
||||
},
|
||||
ajax_page: function (method, action, data, target, element) {
|
||||
/* element is a new parameter, but should be put be put in front */
|
||||
if(element == undefined) element = $(document);
|
||||
if (web2py.isUndefined(element)) element = $(document);
|
||||
/* if target is not there, fill it with something that there isn't in the page*/
|
||||
if(target == undefined || target == '') target = 'w2p_none';
|
||||
if (web2py.isUndefined(target) || target === '') target = 'w2p_none';
|
||||
if (web2py.fire(element, 'ajax:before', null, target)) { /*test a usecase, should stop here if returns false */
|
||||
$.ajax({
|
||||
'type': method,
|
||||
@@ -330,9 +345,9 @@
|
||||
component: function (action, target, timeout, times, el) {
|
||||
/* element is a new parameter, but should be put in front */
|
||||
$(function () {
|
||||
var jelement = $("#" + target);
|
||||
var jelement = $('#' + target);
|
||||
var element = jelement.get(0);
|
||||
var statement = "jQuery('#" + target + "').get(0).reload();";
|
||||
var statement = 'jQuery("#' + target + '").get(0).reload();';
|
||||
element.reload = function () {
|
||||
/* Continue if times is Infinity or
|
||||
* the times limit is not reached
|
||||
@@ -435,17 +450,17 @@
|
||||
}
|
||||
}
|
||||
var entropy = mystring.length * Math.log(score) / 0.6931471805599453;
|
||||
return Math.round(entropy * 100) / 100
|
||||
return Math.round(entropy * 100) / 100;
|
||||
},
|
||||
validate_entropy: function (myfield, req_entropy) {
|
||||
if(myfield.data('w2p_entropy') != undefined) req_entropy = myfield.data('w2p_entropy');
|
||||
if (!web2py.isUndefined(myfield.data('w2p_entropy'))) req_entropy = myfield.data('w2p_entropy');
|
||||
var validator = function () {
|
||||
var v = (web2py.calc_entropy(myfield.val()) || 0) / req_entropy;
|
||||
var r = 0,
|
||||
g = 0,
|
||||
b = 0,
|
||||
rs = function (x) {
|
||||
return Math.round(x * 15).toString(16)
|
||||
return Math.round(x * 15).toString(16);
|
||||
};
|
||||
if (v <= 0.5) {
|
||||
r = 1.0;
|
||||
@@ -456,13 +471,14 @@
|
||||
}
|
||||
var color = '#' + rs(r) + rs(g) + rs(b);
|
||||
myfield.css('background-color', color);
|
||||
entropy_callback = myfield.data('entropy_callback');
|
||||
var entropy_callback = myfield.data('entropy_callback');
|
||||
if (entropy_callback) entropy_callback(v);
|
||||
}
|
||||
if(!myfield.hasClass('entropy_check')) myfield.on('keyup', validator).on('keydown', validator).addClass('entropy_check');
|
||||
};
|
||||
if (!myfield.hasClass('entropy_check')) myfield.on('keyup', validator).on('keydown', validator)
|
||||
.addClass('entropy_check');
|
||||
},
|
||||
web2py_websocket: function (url, onmessage, onopen, onclose) {
|
||||
if("WebSocket" in window) {
|
||||
if ('WebSocket' in window) {
|
||||
var ws = new WebSocket(url);
|
||||
ws.onopen = onopen ? onopen : (function () {});
|
||||
ws.onmessage = onmessage;
|
||||
@@ -497,19 +513,24 @@
|
||||
/* replace element's html with the 'data-disable-with' after storing original html
|
||||
* and prevent clicking on it */
|
||||
disableElement: function (el) {
|
||||
if (!web2py.isUndefined(el.data('w2p_disable'))) {
|
||||
return false;
|
||||
}
|
||||
el.addClass('disabled');
|
||||
var method = el.is('input') ? 'val' : 'html';
|
||||
//method = el.attr('name') ? 'html' : 'val';
|
||||
var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working...";
|
||||
var disable_with_message = (!web2py.isUndefined(w2p_ajax_disable_with_message)) ?
|
||||
w2p_ajax_disable_with_message : 'Working...';
|
||||
/*store enabled state if not already disabled */
|
||||
if(el.data('w2p_enable_with') === undefined) {
|
||||
if (web2py.isUndefined(el.data('w2p_enable_with'))) {
|
||||
el.data('w2p_enable_with', el[method]());
|
||||
}
|
||||
/*if you don't want to see "working..." on buttons, replace the following
|
||||
* two lines with this one
|
||||
* el.data('w2p_disable_with', el[method]());
|
||||
*/
|
||||
if((el.data('w2p_disable_with') == 'default') || (el.data('w2p_disable_with') === undefined)) {
|
||||
if ((el.data('w2p_disable_with') == 'default') || (web2py.isUndefined(el.data(
|
||||
'w2p_disable_with')))) {
|
||||
el.data('w2p_disable_with', disable_with_message);
|
||||
}
|
||||
|
||||
@@ -524,7 +545,7 @@
|
||||
/* restore element to its original state which was disabled by 'disableElement' above*/
|
||||
enableElement: function (el) {
|
||||
var method = el.is('input') ? 'val' : 'html';
|
||||
if(el.data('w2p_enable_with') !== undefined) {
|
||||
if (!web2py.isUndefined(el.data('w2p_enable_with'))) {
|
||||
/* set to old enabled state */
|
||||
el[method](el.data('w2p_enable_with'));
|
||||
el.removeData('w2p_enable_with');
|
||||
@@ -570,7 +591,7 @@
|
||||
for (var name in triggers) {
|
||||
$('#' + name, target).change(show_if).keyup(show_if);
|
||||
show_if.call($('#' + name, target));
|
||||
};
|
||||
}
|
||||
},
|
||||
component_handler: function (target) {
|
||||
$('div[data-w2p_remote]', target).each(function () {
|
||||
@@ -581,7 +602,7 @@
|
||||
timeout = el.data('w2p_timeout');
|
||||
target = el.attr('id');
|
||||
web2py.component(remote, target, timeout, times, $(this));
|
||||
})
|
||||
});
|
||||
},
|
||||
a_handler: function (el, e) {
|
||||
e.preventDefault();
|
||||
@@ -591,19 +612,20 @@
|
||||
var confirm_message = el.data('w2p_confirm');
|
||||
|
||||
var pre_call = el.data('w2p_pre_call');
|
||||
if(pre_call != undefined) {
|
||||
if (!web2py.isUndefined(pre_call)) {
|
||||
eval(pre_call);
|
||||
}
|
||||
if (confirm_message) {
|
||||
if(confirm_message == 'default')
|
||||
if (confirm_message == 'default') {
|
||||
confirm_message = w2p_ajax_confirm_message ||
|
||||
'Are you sure you want to delete this object?';
|
||||
}
|
||||
if (!web2py.confirm(confirm_message)) {
|
||||
web2py.stopEverything(e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(target == undefined) {
|
||||
if (web2py.isUndefined(target)) {
|
||||
if (method == 'GET') {
|
||||
web2py.ajax_page('get', action, [], '', el);
|
||||
} else if (method == 'POST') {
|
||||
@@ -623,10 +645,10 @@
|
||||
web2py.a_handler($(this), e);
|
||||
});
|
||||
/* removal of element should happen only on success */
|
||||
el.on('ajax:success', 'a[data-w2p_method][data-w2p_remove]', function(e) {
|
||||
el.on('ajax:success', 'a[data-w2p_method][data-w2p_remove]', function () {
|
||||
var el = $(this);
|
||||
var toremove = el.data('w2p_remove');
|
||||
if(toremove != undefined) {
|
||||
if (!web2py.isUndefined(toremove)) {
|
||||
toremove = el.closest(toremove);
|
||||
if (!toremove.length) {
|
||||
/*this enables removal of whatever selector if a closest is not found */
|
||||
@@ -635,11 +657,11 @@
|
||||
toremove.remove();
|
||||
}
|
||||
});
|
||||
el.on('ajax:beforeSend', 'a[data-w2p_method][data-w2p_disable_with]', function(e) {
|
||||
el.on('ajax:beforeSend', 'a[data-w2p_method][data-w2p_disable_with]', function () {
|
||||
web2py.disableElement($(this));
|
||||
});
|
||||
/*re-enable click on completion*/
|
||||
el.on('ajax:complete', 'a[data-w2p_method][data-w2p_disable_with]', function(e) {
|
||||
el.on('ajax:complete', 'a[data-w2p_method][data-w2p_disable_with]', function () {
|
||||
web2py.enableElement($(this));
|
||||
});
|
||||
},
|
||||
@@ -653,10 +675,14 @@
|
||||
var element = $(this),
|
||||
method = element.is('button') ? 'html' : 'val';
|
||||
var disable_with = element.data('w2p_disable_with');
|
||||
if(disable_with == undefined) {
|
||||
element.data('w2p_disable_with', element[method]())
|
||||
var disable = element.data('w2p_disable');
|
||||
if (!web2py.isUndefined(disable)) {
|
||||
return false;
|
||||
}
|
||||
if(element.data('w2p_enable_with') === undefined) {
|
||||
if (web2py.isUndefined(disable_with)) {
|
||||
element.data('w2p_disable_with', element[method]());
|
||||
}
|
||||
if (web2py.isUndefined(element.data('w2p_enable_with'))) {
|
||||
element.data('w2p_enable_with', element[method]());
|
||||
}
|
||||
element[method](element.data('w2p_disable_with'));
|
||||
@@ -681,10 +707,10 @@
|
||||
},
|
||||
form_handlers: function () {
|
||||
var el = $(document);
|
||||
el.on('ajax:beforeSend', 'form[data-w2p_target]', function(e) {
|
||||
el.on('ajax:beforeSend', 'form[data-w2p_target]', function () {
|
||||
web2py.disableFormElements($(this));
|
||||
});
|
||||
el.on('ajax:complete', 'form[data-w2p_target]', function(e) {
|
||||
el.on('ajax:complete', 'form[data-w2p_target]', function () {
|
||||
web2py.enableFormElements($(this));
|
||||
});
|
||||
},
|
||||
@@ -693,7 +719,7 @@
|
||||
invalidate: function (target) {
|
||||
$('div[data-w2p_remote]', target).each(function () {
|
||||
var el = $('#' + $(this).attr('id')).get(0);
|
||||
if(el.timing !== undefined) { // Block triggering regular routines
|
||||
if (!web2py.isUndefined(el.timing)) { // Block triggering regular routines
|
||||
clearInterval(el.timing);
|
||||
}
|
||||
});
|
||||
@@ -708,7 +734,7 @@
|
||||
web2py.a_handlers();
|
||||
web2py.form_handlers();
|
||||
}
|
||||
}
|
||||
};
|
||||
/*end of functions */
|
||||
/*main hook*/
|
||||
$(function () {
|
||||
|
||||
Reference in new issue
Block a user