Merge pull request #510 from niphlod/fix/1985

fixes issue 1985
This commit is contained in:
mdipierro
2014-09-29 19:46:01 -05:00
3 changed files with 363 additions and 354 deletions
+121 -118
View File
@@ -1,4 +1,4 @@
(function ($, undefined) {
(function($, undefined) {
/*
* Unobtrusive scripting adapter for jQuery, largely taken from
* the wonderful https://github.com/rails/jquery-ujs
@@ -12,29 +12,29 @@
}
String.prototype.reverse = function () {
String.prototype.reverse = function() {
return this.split('').reverse().join('');
};
var web2py;
$.web2py = web2py = {
popup: function (url) {
popup: function(url) {
/* popup a window */
newwindow = window.open(url, 'name', 'height=400,width=600');
if(window.focus) newwindow.focus();
return false;
},
collapse: function (id) {
collapse: function(id) {
/* toggle an element */
$('#' + id).slideToggle();
},
fade: function (id, value) {
fade: function(id, value) {
/*fade something*/
if(value > 0) $('#' + id).hide().fadeIn('slow');
else $('#' + id).show().fadeOut('slow');
},
ajax: function (u, s, t) {
ajax: function(u, s, t) {
/*simple ajax function*/
query = '';
if(typeof s == "string") {
@@ -59,7 +59,7 @@
type: "POST",
url: u,
data: query,
success: function (msg) {
success: function(msg) {
if(t) {
if(t == ':eval') eval(msg);
else if(typeof t == 'string') $("#" + t).html(msg);
@@ -68,7 +68,7 @@
}
});
},
ajax_fields: function (target) {
ajax_fields: function(target) {
/*
*this attaches something to a newly loaded fragment/page
* Ideally all events should be bound to the document, so we can avoid calling
@@ -78,11 +78,11 @@
$('button', target).addClass('btn');
$('form input[type="submit"], form input[type="button"]', target).addClass('btn');
/* javascript for PasswordWidget*/
$('input[type=password][data-w2p_entropy]', target).each(function () {
$('input[type=password][data-w2p_entropy]', target).each(function() {
web2py.validate_entropy($(this));
});
/* javascript for ListWidget*/
$('ul.w2p_list', target).each(function () {
$('ul.w2p_list', target).each(function() {
function pe(ul, e) {
var new_line = ml(ul);
rel(ul);
@@ -113,25 +113,25 @@
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();
else $(this.firstChild).val(trimmed);
});
}
var ul = this;
$(ul).find(":text").after('<a href="#">+</a>&nbsp;<a href="#">-</a>').keypress(function (e) {
$(ul).find(":text").after('<a href="#">+</a>&nbsp;<a href="#">-</a>').keypress(function(e) {
return(e.which == 13) ? pe(ul, e) : true;
}).next().click(function (e) {
}).next().click(function(e) {
pe(ul, e);
e.preventDefault();
}).next().click(function (e) {
}).next().click(function(e) {
rl(ul, e);
e.preventDefault();
});
});
},
ajax_init: function (target) {
ajax_init: function(target) {
/*called whenever a fragment gets loaded */
$('.hidden', target).hide();
web2py.manage_errors(target);
@@ -140,10 +140,10 @@
web2py.component_handler(target);
},
/* manage errors in forms */
manage_errors: function (target) {
manage_errors: function(target) {
$('.error', target).hide().slideDown('slow');
},
after_ajax: function (xhr) {
after_ajax: function(xhr) {
/* called whenever an ajax request completes */
var command = xhr.getResponseHeader('web2py-component-command');
var flash = xhr.getResponseHeader('web2py-component-flash');
@@ -154,33 +154,33 @@
web2py.flash(decodeURIComponent(flash))
}
},
event_handlers: function () {
event_handlers: function() {
/*
* This is called once for page
* Ideally it should bound all the things that are needed
* and require no dom manipulations
*/
var doc = $(document);
doc.on('click', '.flash', function (e) {
doc.on('click', '.flash', function(e) {
var t = $(this);
if(t.css('top') == '0px') t.slideUp('slow');
else t.fadeOut();
});
doc.on('keyup', 'input.integer', function () {
var nvalue = this.value.reverse().replace(/[^0-9\-]|\-(?=.)/g, '').reverse();
if(this.value!=nvalue) this.value = nvalue;
doc.on('keyup', 'input.integer', function() {
var nvalue = this.value.reverse().replace(/[^0-9\-]|\-(?=.)/g, '').reverse();
if(this.value != nvalue) this.value = nvalue;
});
doc.on('keyup', 'input.double, input.decimal', function () {
doc.on('keyup', 'input.double, input.decimal', function() {
var nvalue = this.value.reverse().replace(/[^0-9\-\.,]|[\-](?=.)|[\.,](?=[0-9]*[\.,])/g, '').reverse();
if(this.value!=nvalue) this.value = nvalue;
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 () {
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 () {
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;
@@ -197,7 +197,7 @@
}
});
var date_format = (typeof w2p_ajax_date_format != 'undefined') ? w2p_ajax_date_format : "%Y-%m-%d";
doc.on('click', "input.date", function () {
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;
@@ -212,7 +212,7 @@
$(this).trigger('click');
}
});
doc.on('focus', "input.time", function () {
doc.on('focus', "input.time", function() {
var active = $(this).data('w2p_time');
if(active === undefined) {
$(this).timeEntry({
@@ -222,11 +222,11 @@
}
});
/* help preventing double form submission for normal form (not LOADed) */
$(doc).on('submit', 'form', function () {
$(doc).on('submit', 'form', function() {
var submit_button = $(this).find(web2py.formInputClickSelector);
web2py.disableElement(submit_button);
});
doc.ajaxSuccess(function (e, xhr) {
doc.ajaxSuccess(function(e, xhr) {
var redirect = xhr.getResponseHeader('web2py-redirect-location');
if(redirect !== null) {
window.location = redirect;
@@ -237,37 +237,37 @@
};
});
doc.ajaxError(function (e, xhr, settings, exception) {
doc.ajaxError(function(e, xhr, settings, exception) {
/*personally I don't like it.
*if there's an error it it flashed and can be removed
*as any other message
*doc.off('click', '.flash')
*/
switch(xhr.status) {
case 500:
web2py.flash(ajax_error_500);
case 500:
web2py.flash(ajax_error_500);
}
});
},
trap_form: function (action, target) {
trap_form: function(action, target) {
/* traps any LOADed form */
$('#' + target + ' form').each(function (i) {
$('#' + target + ' form').each(function(i) {
var form = $(this);
if (form.hasClass('no_trap')) {
if(form.hasClass('no_trap')) {
return;
}
form.attr('data-w2p_target', target);
var url = form.attr('action');
if ((url === "") || (url === "#")) {
if((url === "") || (url === "#") || (typeof url === 'undefined')) {
/* form has no action. Use component url. */
url = action;
}
form.submit(function (e) {
form.submit(function(e) {
web2py.disableElement(form.find(web2py.formInputClickSelector));
web2py.hide_flash();
web2py.ajax_page('post', url, form.serialize(), target, form);
@@ -277,28 +277,28 @@
},
ajax_page: function (method, action, data, target, element) {
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 target is not there, fill it with something that there isn't in the page*/
if(target == undefined || target == '') target = 'w2p_none';
if(web2py.fire(element, 'ajax:before', null, target )) { /*test a usecase, should stop here if returns false */
if(web2py.fire(element, 'ajax:before', null, target)) { /*test a usecase, should stop here if returns false */
$.ajax({
'type': method,
'url': action,
'data': data,
'beforeSend': function (xhr, settings) {
'beforeSend': function(xhr, settings) {
xhr.setRequestHeader('web2py-component-location', document.location);
xhr.setRequestHeader('web2py-component-element', target);
return web2py.fire(element, 'ajax:beforeSend', [xhr, settings], target); //test a usecase, should stop here if returns false
},
'success': function (data, status, xhr) {
'success': function(data, status, xhr) {
/*bummer for form submissions....the element is not there after complete
*because it gets replaced by the new response....
*/
web2py.fire(element, 'ajax:success', [data, status, xhr], target);
},
'error': function (xhr, status, error) {
'error': function(xhr, status, error) {
/*bummer for form submissions....in addition to the element being not there after
*complete because it gets replaced by the new response, standard form
*handling just returns the same status code for good and bad
@@ -306,9 +306,9 @@
*/
web2py.fire(element, 'ajax:error', [xhr, status, error], target);
},
'complete': function (xhr, status) {
'complete': function(xhr, status) {
web2py.fire(element, 'ajax:complete', [xhr, status], target);
web2py.updatePage(xhr, target); /* Parse and load the html received */
web2py.updatePage(xhr, target); /* Parse and load the html received */
web2py.trap_form(action, target);
web2py.ajax_init('#' + target);
web2py.after_ajax(xhr);
@@ -316,13 +316,13 @@
});
}
},
component: function (action, target, timeout, times, el) {
component: function(action, target, timeout, times, el) {
/* element is a new parameter, but should be put in front */
$(function () {
$(function() {
var jelement = $("#" + target);
var element = jelement.get(0);
var statement = "jQuery('#" + target + "').get(0).reload();";
element.reload = function () {
element.reload = function() {
/* Continue if times is Infinity or
* the times limit is not reached
*/
@@ -331,7 +331,7 @@
}
};
/* Method to check timing limit */
element.reload_check = function () {
element.reload_check = function() {
if(jelement.hasClass('w2p_component_stop')) {
clearInterval(this.timing);
return false;
@@ -377,28 +377,30 @@
}
});
},
updatePage: function (xhr, target) {
var t = $('#' + target);
var html = $.parseHTML(xhr.responseText, document, true);
var title_elements = $(html).filter('title').add($(html).find('title'));
var title = title_elements.last().text();
if (title) {
title_elements.remove(); /* Remove any title elements from the response */
document.title = $.trim(title); /* Set the new document title */
}
var content = xhr.getResponseHeader('web2py-component-content');
if(content == 'prepend') t.prepend(xhr.responseText);
else if(content == 'append') t.append(xhr.responseText);
else if(content != 'hide') t.html(html);
updatePage: function(xhr, target) {
var t = $('#' + target);
var html = $.parseHTML(xhr.responseText, document, true);
var title_elements = $(html).filter('title').add($(html).find('title'));
var title = title_elements.last().text();
if(title) {
title_elements.remove(); /* Remove any title elements from the response */
document.title = $.trim(title); /* Set the new document title */
}
var content = xhr.getResponseHeader('web2py-component-content');
if(content == 'prepend') t.prepend(xhr.responseText);
else if(content == 'append') t.append(xhr.responseText);
else if(content != 'hide') t.html(html);
},
calc_entropy: function (mystring) {
calc_entropy: function(mystring) {
/* calculate a simple entropy for a given string */
var csets = new Array(
'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'0123456789', '!@#$\%^&*()', '~`-_=+[]{}\|;:\'",.<>?/',
'0123456789abcdefghijklmnopqrstuvwxyz');
var score = 0,
other = {}, seen = {}, lastset = null,
other = {},
seen = {},
lastset = null,
mystringlist = mystring.split('');
for(var i = 0; i < mystringlist.length; i++) { /* classify this character */
var c = mystringlist[i],
@@ -424,14 +426,14 @@
var entropy = mystring.length * Math.log(score) / 0.6931471805599453;
return Math.round(entropy * 100) / 100
},
validate_entropy: function (myfield, req_entropy) {
validate_entropy: function(myfield, req_entropy) {
if(myfield.data('w2p_entropy') != undefined) req_entropy = myfield.data('w2p_entropy');
var validator = function () {
var validator = function() {
var v = (web2py.calc_entropy(myfield.val()) || 0) / req_entropy;
var r = 0,
g = 0,
b = 0,
rs = function (x) {
rs = function(x) {
return Math.round(x * 15).toString(16)
};
if(v <= 0.5) {
@@ -448,12 +450,12 @@
}
if(!myfield.hasClass('entropy_check')) myfield.on('keyup', validator).on('keydown', validator).addClass('entropy_check');
},
web2py_websocket: function (url, onmessage, onopen, onclose) {
web2py_websocket: function(url, onmessage, onopen, onclose) {
if("WebSocket" in window) {
var ws = new WebSocket(url);
ws.onopen = onopen ? onopen : (function () {});
ws.onopen = onopen ? onopen : (function() {});
ws.onmessage = onmessage;
ws.onclose = onclose ? onclose : (function () {});
ws.onclose = onclose ? onclose : (function() {});
return true; /* supported */
} else return false; /* not supported */
},
@@ -465,34 +467,36 @@
/* Form input elements re-enabled after form submission */
enableSelector: 'input:disabled, button:disabled, textarea:disabled, select:disabled',
/* Triggers an event on an element and returns false if the event result is false */
fire: function (obj, type, data, target) {
var event = $.Event(type, {'containerTarget': $('#' + target)[0]});
fire: function(obj, type, data, target) {
var event = $.Event(type, {
'containerTarget': $('#' + target)[0]
});
obj.trigger(event, data);
return event.result !== false;
},
/* Helper function, needed to provide consistent behavior in IE */
stopEverything: function (e) {
stopEverything: function(e) {
$(e.target).trigger('w2p:everythingStopped');
e.stopImmediatePropagation();
return false;
},
confirm: function (message) {
confirm: function(message) {
return confirm(message);
},
/* replace element's html with the 'data-disable-with' after storing original html
* and prevent clicking on it */
disableElement: function (el) {
disableElement: function(el) {
el.addClass('disabled');
var method = el.prop('type') == 'submit' ? 'val' : 'html';
var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working...";
/*store enabled state if not already disabled */
if (el.data('w2p:enable-with') === undefined) {
el.data('w2p:enable-with', el[method]());
if(el.data('w2p:enable-with') === undefined) {
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]());
*/
* 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)) {
el.data('w2p_disable_with', disable_with_message);
}
@@ -500,13 +504,13 @@
/* set to disabled state*/
el[method](el.data('w2p_disable_with'));
el.bind('click.w2pDisable', function (e) { /* prevent further clicking*/
el.bind('click.w2pDisable', function(e) { /* prevent further clicking*/
return web2py.stopEverything(e);
});
},
/* restore element to its original state which was disabled by 'disableElement' above*/
enableElement: function (el) {
enableElement: function(el) {
var method = el.prop('type') == 'submit' ? 'val' : 'html';
if(el.data('w2p:enable-with') !== undefined) {
/* set to old enabled state */
@@ -517,22 +521,22 @@
el.unbind('click.w2pDisable');
},
/*convenience wrapper, internal use only */
simple_component: function (action, target, element) {
simple_component: function(action, target, element) {
web2py.component(action, target, 0, 1, element);
},
/*helper for flash messages*/
flash: function (message, status) {
flash: function(message, status) {
var flash = $('.flash');
web2py.hide_flash();
flash.html(message).addClass(status);
if(flash.html()) flash.append('<span id="closeflash"> &times; </span>').slideDown();
},
hide_flash: function () {
hide_flash: function() {
$('.flash').fadeOut(0).html('');
},
show_if_handler: function (target) {
show_if_handler: function(target) {
var triggers = {};
var show_if = function () {
var show_if = function() {
var t = $(this);
var id = t.attr('id');
t.attr('value', t.val());
@@ -543,12 +547,12 @@
else tr.hide();
}
};
$('[data-show-trigger]', target).each(function () {
$('[data-show-trigger]', target).each(function() {
var name = $(this).attr('data-show-trigger');
// The field exists only when creating/editing a row
if ($('#' + name).length) {
if(!triggers[name]) triggers[name] = [];
triggers[name].push($(this).attr('id'));
if($('#' + name).length) {
if(!triggers[name]) triggers[name] = [];
triggers[name].push($(this).attr('id'));
}
});
for(var name in triggers) {
@@ -556,8 +560,8 @@
show_if.call($('#' + name, target));
};
},
component_handler: function (target) {
$('div[data-w2p_remote]', target).each(function () {
component_handler: function(target) {
$('div[data-w2p_remote]', target).each(function() {
var remote, times, timeout, target;
var el = $(this);
remote = el.data('w2p_remote');
@@ -567,7 +571,7 @@
web2py.component(remote, target, timeout, times, $(this));
})
},
a_handler: function (el, e) {
a_handler: function(el, e) {
e.preventDefault();
var method = el.data('w2p_method');
var action = el.attr('href');
@@ -599,13 +603,13 @@
}
}
},
a_handlers: function () {
a_handlers: function() {
var el = $(document);
el.on('click', 'a[data-w2p_method]', function (e) {
el.on('click', 'a[data-w2p_method]', function(e) {
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(e) {
var el = $(this);
var toremove = el.data('w2p_remove');
if(toremove != undefined) {
@@ -617,11 +621,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(e) {
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(e) {
web2py.enableElement($(this));
});
},
@@ -630,16 +634,16 @@
- Replaces element text with value of 'data-disable-with' attribute
- Sets disabled property to true
*/
disableFormElements: function (form) {
form.find(web2py.disableSelector).each(function () {
disableFormElements: function(form) {
form.find(web2py.disableSelector).each(function() {
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]())
}
if (element.data('w2p:enable-with') === undefined) {
element.data('w2p:enable-with', element[method]());
if(element.data('w2p:enable-with') === undefined) {
element.data('w2p:enable-with', element[method]());
}
element[method](element.data('w2p_disable_with'));
element.prop('disabled', true);
@@ -650,42 +654,42 @@
- Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
- Sets disabled property to false
*/
enableFormElements: function (form) {
form.find(web2py.enableSelector).each(function () {
enableFormElements: function(form) {
form.find(web2py.enableSelector).each(function() {
var element = $(this),
method = element.is('button') ? 'html' : 'val';
if(element.data('w2p:enable-with')) {
element[method](element.data('w2p:enable-with'));
element.removeData('w2p:enable-with');
element[method](element.data('w2p:enable-with'));
element.removeData('w2p:enable-with');
}
element.prop('disabled', false);
});
},
form_handlers: function () {
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(e) {
web2py.disableFormElements($(this));
});
el.on('ajax:complete', 'form[data-w2p_target]', function (e) {
el.on('ajax:complete', 'form[data-w2p_target]', function(e) {
web2py.enableFormElements($(this));
});
},
/* Invalidate and force reload of a web2py component
*/
*/
invalidate: function(target) {
$('div[data-w2p_remote]', target).each(function () {
$('div[data-w2p_remote]', target).each(function() {
var el = $('#' + $(this).attr('id')).get(0);
if (el.timing !== undefined) { // Block triggering regular routines
clearInterval(el.timing);
if(el.timing !== undefined) { // Block triggering regular routines
clearInterval(el.timing);
}
});
$.web2py.component_handler(target);
},
}
}
/*end of functions */
/*main hook*/
$(function () {
$(function() {
var flash = $('.flash');
flash.hide();
if(flash.html()) web2py.flash(flash.html());
@@ -720,5 +724,4 @@ web2py_event_handlers = jQuery.web2py.event_handlers;
web2py_trap_link = jQuery.web2py.trap_link;
web2py_calc_entropy = jQuery.web2py.calc_entropy;
*/
/* compatibility code - end*/
/* compatibility code - end*/
+121 -118
View File
@@ -1,4 +1,4 @@
(function ($, undefined) {
(function($, undefined) {
/*
* Unobtrusive scripting adapter for jQuery, largely taken from
* the wonderful https://github.com/rails/jquery-ujs
@@ -12,29 +12,29 @@
}
String.prototype.reverse = function () {
String.prototype.reverse = function() {
return this.split('').reverse().join('');
};
var web2py;
$.web2py = web2py = {
popup: function (url) {
popup: function(url) {
/* popup a window */
newwindow = window.open(url, 'name', 'height=400,width=600');
if(window.focus) newwindow.focus();
return false;
},
collapse: function (id) {
collapse: function(id) {
/* toggle an element */
$('#' + id).slideToggle();
},
fade: function (id, value) {
fade: function(id, value) {
/*fade something*/
if(value > 0) $('#' + id).hide().fadeIn('slow');
else $('#' + id).show().fadeOut('slow');
},
ajax: function (u, s, t) {
ajax: function(u, s, t) {
/*simple ajax function*/
query = '';
if(typeof s == "string") {
@@ -59,7 +59,7 @@
type: "POST",
url: u,
data: query,
success: function (msg) {
success: function(msg) {
if(t) {
if(t == ':eval') eval(msg);
else if(typeof t == 'string') $("#" + t).html(msg);
@@ -68,7 +68,7 @@
}
});
},
ajax_fields: function (target) {
ajax_fields: function(target) {
/*
*this attaches something to a newly loaded fragment/page
* Ideally all events should be bound to the document, so we can avoid calling
@@ -78,11 +78,11 @@
$('button', target).addClass('btn');
$('form input[type="submit"], form input[type="button"]', target).addClass('btn');
/* javascript for PasswordWidget*/
$('input[type=password][data-w2p_entropy]', target).each(function () {
$('input[type=password][data-w2p_entropy]', target).each(function() {
web2py.validate_entropy($(this));
});
/* javascript for ListWidget*/
$('ul.w2p_list', target).each(function () {
$('ul.w2p_list', target).each(function() {
function pe(ul, e) {
var new_line = ml(ul);
rel(ul);
@@ -113,25 +113,25 @@
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();
else $(this.firstChild).val(trimmed);
});
}
var ul = this;
$(ul).find(":text").after('<a href="#">+</a>&nbsp;<a href="#">-</a>').keypress(function (e) {
$(ul).find(":text").after('<a href="#">+</a>&nbsp;<a href="#">-</a>').keypress(function(e) {
return(e.which == 13) ? pe(ul, e) : true;
}).next().click(function (e) {
}).next().click(function(e) {
pe(ul, e);
e.preventDefault();
}).next().click(function (e) {
}).next().click(function(e) {
rl(ul, e);
e.preventDefault();
});
});
},
ajax_init: function (target) {
ajax_init: function(target) {
/*called whenever a fragment gets loaded */
$('.hidden', target).hide();
web2py.manage_errors(target);
@@ -140,10 +140,10 @@
web2py.component_handler(target);
},
/* manage errors in forms */
manage_errors: function (target) {
manage_errors: function(target) {
$('.error', target).hide().slideDown('slow');
},
after_ajax: function (xhr) {
after_ajax: function(xhr) {
/* called whenever an ajax request completes */
var command = xhr.getResponseHeader('web2py-component-command');
var flash = xhr.getResponseHeader('web2py-component-flash');
@@ -154,33 +154,33 @@
web2py.flash(decodeURIComponent(flash))
}
},
event_handlers: function () {
event_handlers: function() {
/*
* This is called once for page
* Ideally it should bound all the things that are needed
* and require no dom manipulations
*/
var doc = $(document);
doc.on('click', '.flash', function (e) {
doc.on('click', '.flash', function(e) {
var t = $(this);
if(t.css('top') == '0px') t.slideUp('slow');
else t.fadeOut();
});
doc.on('keyup', 'input.integer', function () {
var nvalue = this.value.reverse().replace(/[^0-9\-]|\-(?=.)/g, '').reverse();
if(this.value!=nvalue) this.value = nvalue;
doc.on('keyup', 'input.integer', function() {
var nvalue = this.value.reverse().replace(/[^0-9\-]|\-(?=.)/g, '').reverse();
if(this.value != nvalue) this.value = nvalue;
});
doc.on('keyup', 'input.double, input.decimal', function () {
doc.on('keyup', 'input.double, input.decimal', function() {
var nvalue = this.value.reverse().replace(/[^0-9\-\.,]|[\-](?=.)|[\.,](?=[0-9]*[\.,])/g, '').reverse();
if(this.value!=nvalue) this.value = nvalue;
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 () {
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 () {
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;
@@ -197,7 +197,7 @@
}
});
var date_format = (typeof w2p_ajax_date_format != 'undefined') ? w2p_ajax_date_format : "%Y-%m-%d";
doc.on('click', "input.date", function () {
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;
@@ -212,7 +212,7 @@
$(this).trigger('click');
}
});
doc.on('focus', "input.time", function () {
doc.on('focus', "input.time", function() {
var active = $(this).data('w2p_time');
if(active === undefined) {
$(this).timeEntry({
@@ -222,11 +222,11 @@
}
});
/* help preventing double form submission for normal form (not LOADed) */
$(doc).on('submit', 'form', function () {
$(doc).on('submit', 'form', function() {
var submit_button = $(this).find(web2py.formInputClickSelector);
web2py.disableElement(submit_button);
});
doc.ajaxSuccess(function (e, xhr) {
doc.ajaxSuccess(function(e, xhr) {
var redirect = xhr.getResponseHeader('web2py-redirect-location');
if(redirect !== null) {
window.location = redirect;
@@ -237,37 +237,37 @@
};
});
doc.ajaxError(function (e, xhr, settings, exception) {
doc.ajaxError(function(e, xhr, settings, exception) {
/*personally I don't like it.
*if there's an error it it flashed and can be removed
*as any other message
*doc.off('click', '.flash')
*/
switch(xhr.status) {
case 500:
web2py.flash(ajax_error_500);
case 500:
web2py.flash(ajax_error_500);
}
});
},
trap_form: function (action, target) {
trap_form: function(action, target) {
/* traps any LOADed form */
$('#' + target + ' form').each(function (i) {
$('#' + target + ' form').each(function(i) {
var form = $(this);
if (form.hasClass('no_trap')) {
if(form.hasClass('no_trap')) {
return;
}
form.attr('data-w2p_target', target);
var url = form.attr('action');
if ((url === "") || (url === "#")) {
if((url === "") || (url === "#") || (typeof url === 'undefined')) {
/* form has no action. Use component url. */
url = action;
}
form.submit(function (e) {
form.submit(function(e) {
web2py.disableElement(form.find(web2py.formInputClickSelector));
web2py.hide_flash();
web2py.ajax_page('post', url, form.serialize(), target, form);
@@ -277,28 +277,28 @@
},
ajax_page: function (method, action, data, target, element) {
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 target is not there, fill it with something that there isn't in the page*/
if(target == undefined || target == '') target = 'w2p_none';
if(web2py.fire(element, 'ajax:before', null, target )) { /*test a usecase, should stop here if returns false */
if(web2py.fire(element, 'ajax:before', null, target)) { /*test a usecase, should stop here if returns false */
$.ajax({
'type': method,
'url': action,
'data': data,
'beforeSend': function (xhr, settings) {
'beforeSend': function(xhr, settings) {
xhr.setRequestHeader('web2py-component-location', document.location);
xhr.setRequestHeader('web2py-component-element', target);
return web2py.fire(element, 'ajax:beforeSend', [xhr, settings], target); //test a usecase, should stop here if returns false
},
'success': function (data, status, xhr) {
'success': function(data, status, xhr) {
/*bummer for form submissions....the element is not there after complete
*because it gets replaced by the new response....
*/
web2py.fire(element, 'ajax:success', [data, status, xhr], target);
},
'error': function (xhr, status, error) {
'error': function(xhr, status, error) {
/*bummer for form submissions....in addition to the element being not there after
*complete because it gets replaced by the new response, standard form
*handling just returns the same status code for good and bad
@@ -306,9 +306,9 @@
*/
web2py.fire(element, 'ajax:error', [xhr, status, error], target);
},
'complete': function (xhr, status) {
'complete': function(xhr, status) {
web2py.fire(element, 'ajax:complete', [xhr, status], target);
web2py.updatePage(xhr, target); /* Parse and load the html received */
web2py.updatePage(xhr, target); /* Parse and load the html received */
web2py.trap_form(action, target);
web2py.ajax_init('#' + target);
web2py.after_ajax(xhr);
@@ -316,13 +316,13 @@
});
}
},
component: function (action, target, timeout, times, el) {
component: function(action, target, timeout, times, el) {
/* element is a new parameter, but should be put in front */
$(function () {
$(function() {
var jelement = $("#" + target);
var element = jelement.get(0);
var statement = "jQuery('#" + target + "').get(0).reload();";
element.reload = function () {
element.reload = function() {
/* Continue if times is Infinity or
* the times limit is not reached
*/
@@ -331,7 +331,7 @@
}
};
/* Method to check timing limit */
element.reload_check = function () {
element.reload_check = function() {
if(jelement.hasClass('w2p_component_stop')) {
clearInterval(this.timing);
return false;
@@ -377,28 +377,30 @@
}
});
},
updatePage: function (xhr, target) {
var t = $('#' + target);
var html = $.parseHTML(xhr.responseText, document, true);
var title_elements = $(html).filter('title').add($(html).find('title'));
var title = title_elements.last().text();
if (title) {
title_elements.remove(); /* Remove any title elements from the response */
document.title = $.trim(title); /* Set the new document title */
}
var content = xhr.getResponseHeader('web2py-component-content');
if(content == 'prepend') t.prepend(xhr.responseText);
else if(content == 'append') t.append(xhr.responseText);
else if(content != 'hide') t.html(html);
updatePage: function(xhr, target) {
var t = $('#' + target);
var html = $.parseHTML(xhr.responseText, document, true);
var title_elements = $(html).filter('title').add($(html).find('title'));
var title = title_elements.last().text();
if(title) {
title_elements.remove(); /* Remove any title elements from the response */
document.title = $.trim(title); /* Set the new document title */
}
var content = xhr.getResponseHeader('web2py-component-content');
if(content == 'prepend') t.prepend(xhr.responseText);
else if(content == 'append') t.append(xhr.responseText);
else if(content != 'hide') t.html(html);
},
calc_entropy: function (mystring) {
calc_entropy: function(mystring) {
/* calculate a simple entropy for a given string */
var csets = new Array(
'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'0123456789', '!@#$\%^&*()', '~`-_=+[]{}\|;:\'",.<>?/',
'0123456789abcdefghijklmnopqrstuvwxyz');
var score = 0,
other = {}, seen = {}, lastset = null,
other = {},
seen = {},
lastset = null,
mystringlist = mystring.split('');
for(var i = 0; i < mystringlist.length; i++) { /* classify this character */
var c = mystringlist[i],
@@ -424,14 +426,14 @@
var entropy = mystring.length * Math.log(score) / 0.6931471805599453;
return Math.round(entropy * 100) / 100
},
validate_entropy: function (myfield, req_entropy) {
validate_entropy: function(myfield, req_entropy) {
if(myfield.data('w2p_entropy') != undefined) req_entropy = myfield.data('w2p_entropy');
var validator = function () {
var validator = function() {
var v = (web2py.calc_entropy(myfield.val()) || 0) / req_entropy;
var r = 0,
g = 0,
b = 0,
rs = function (x) {
rs = function(x) {
return Math.round(x * 15).toString(16)
};
if(v <= 0.5) {
@@ -448,12 +450,12 @@
}
if(!myfield.hasClass('entropy_check')) myfield.on('keyup', validator).on('keydown', validator).addClass('entropy_check');
},
web2py_websocket: function (url, onmessage, onopen, onclose) {
web2py_websocket: function(url, onmessage, onopen, onclose) {
if("WebSocket" in window) {
var ws = new WebSocket(url);
ws.onopen = onopen ? onopen : (function () {});
ws.onopen = onopen ? onopen : (function() {});
ws.onmessage = onmessage;
ws.onclose = onclose ? onclose : (function () {});
ws.onclose = onclose ? onclose : (function() {});
return true; /* supported */
} else return false; /* not supported */
},
@@ -465,34 +467,36 @@
/* Form input elements re-enabled after form submission */
enableSelector: 'input:disabled, button:disabled, textarea:disabled, select:disabled',
/* Triggers an event on an element and returns false if the event result is false */
fire: function (obj, type, data, target) {
var event = $.Event(type, {'containerTarget': $('#' + target)[0]});
fire: function(obj, type, data, target) {
var event = $.Event(type, {
'containerTarget': $('#' + target)[0]
});
obj.trigger(event, data);
return event.result !== false;
},
/* Helper function, needed to provide consistent behavior in IE */
stopEverything: function (e) {
stopEverything: function(e) {
$(e.target).trigger('w2p:everythingStopped');
e.stopImmediatePropagation();
return false;
},
confirm: function (message) {
confirm: function(message) {
return confirm(message);
},
/* replace element's html with the 'data-disable-with' after storing original html
* and prevent clicking on it */
disableElement: function (el) {
disableElement: function(el) {
el.addClass('disabled');
var method = el.prop('type') == 'submit' ? 'val' : 'html';
var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working...";
/*store enabled state if not already disabled */
if (el.data('w2p:enable-with') === undefined) {
el.data('w2p:enable-with', el[method]());
if(el.data('w2p:enable-with') === undefined) {
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]());
*/
* 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)) {
el.data('w2p_disable_with', disable_with_message);
}
@@ -500,13 +504,13 @@
/* set to disabled state*/
el[method](el.data('w2p_disable_with'));
el.bind('click.w2pDisable', function (e) { /* prevent further clicking*/
el.bind('click.w2pDisable', function(e) { /* prevent further clicking*/
return web2py.stopEverything(e);
});
},
/* restore element to its original state which was disabled by 'disableElement' above*/
enableElement: function (el) {
enableElement: function(el) {
var method = el.prop('type') == 'submit' ? 'val' : 'html';
if(el.data('w2p:enable-with') !== undefined) {
/* set to old enabled state */
@@ -517,22 +521,22 @@
el.unbind('click.w2pDisable');
},
/*convenience wrapper, internal use only */
simple_component: function (action, target, element) {
simple_component: function(action, target, element) {
web2py.component(action, target, 0, 1, element);
},
/*helper for flash messages*/
flash: function (message, status) {
flash: function(message, status) {
var flash = $('.flash');
web2py.hide_flash();
flash.html(message).addClass(status);
if(flash.html()) flash.append('<span id="closeflash"> &times; </span>').slideDown();
},
hide_flash: function () {
hide_flash: function() {
$('.flash').fadeOut(0).html('');
},
show_if_handler: function (target) {
show_if_handler: function(target) {
var triggers = {};
var show_if = function () {
var show_if = function() {
var t = $(this);
var id = t.attr('id');
t.attr('value', t.val());
@@ -543,12 +547,12 @@
else tr.hide();
}
};
$('[data-show-trigger]', target).each(function () {
$('[data-show-trigger]', target).each(function() {
var name = $(this).attr('data-show-trigger');
// The field exists only when creating/editing a row
if ($('#' + name).length) {
if(!triggers[name]) triggers[name] = [];
triggers[name].push($(this).attr('id'));
if($('#' + name).length) {
if(!triggers[name]) triggers[name] = [];
triggers[name].push($(this).attr('id'));
}
});
for(var name in triggers) {
@@ -556,8 +560,8 @@
show_if.call($('#' + name, target));
};
},
component_handler: function (target) {
$('div[data-w2p_remote]', target).each(function () {
component_handler: function(target) {
$('div[data-w2p_remote]', target).each(function() {
var remote, times, timeout, target;
var el = $(this);
remote = el.data('w2p_remote');
@@ -567,7 +571,7 @@
web2py.component(remote, target, timeout, times, $(this));
})
},
a_handler: function (el, e) {
a_handler: function(el, e) {
e.preventDefault();
var method = el.data('w2p_method');
var action = el.attr('href');
@@ -599,13 +603,13 @@
}
}
},
a_handlers: function () {
a_handlers: function() {
var el = $(document);
el.on('click', 'a[data-w2p_method]', function (e) {
el.on('click', 'a[data-w2p_method]', function(e) {
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(e) {
var el = $(this);
var toremove = el.data('w2p_remove');
if(toremove != undefined) {
@@ -617,11 +621,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(e) {
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(e) {
web2py.enableElement($(this));
});
},
@@ -630,16 +634,16 @@
- Replaces element text with value of 'data-disable-with' attribute
- Sets disabled property to true
*/
disableFormElements: function (form) {
form.find(web2py.disableSelector).each(function () {
disableFormElements: function(form) {
form.find(web2py.disableSelector).each(function() {
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]())
}
if (element.data('w2p:enable-with') === undefined) {
element.data('w2p:enable-with', element[method]());
if(element.data('w2p:enable-with') === undefined) {
element.data('w2p:enable-with', element[method]());
}
element[method](element.data('w2p_disable_with'));
element.prop('disabled', true);
@@ -650,42 +654,42 @@
- Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
- Sets disabled property to false
*/
enableFormElements: function (form) {
form.find(web2py.enableSelector).each(function () {
enableFormElements: function(form) {
form.find(web2py.enableSelector).each(function() {
var element = $(this),
method = element.is('button') ? 'html' : 'val';
if(element.data('w2p:enable-with')) {
element[method](element.data('w2p:enable-with'));
element.removeData('w2p:enable-with');
element[method](element.data('w2p:enable-with'));
element.removeData('w2p:enable-with');
}
element.prop('disabled', false);
});
},
form_handlers: function () {
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(e) {
web2py.disableFormElements($(this));
});
el.on('ajax:complete', 'form[data-w2p_target]', function (e) {
el.on('ajax:complete', 'form[data-w2p_target]', function(e) {
web2py.enableFormElements($(this));
});
},
/* Invalidate and force reload of a web2py component
*/
*/
invalidate: function(target) {
$('div[data-w2p_remote]', target).each(function () {
$('div[data-w2p_remote]', target).each(function() {
var el = $('#' + $(this).attr('id')).get(0);
if (el.timing !== undefined) { // Block triggering regular routines
clearInterval(el.timing);
if(el.timing !== undefined) { // Block triggering regular routines
clearInterval(el.timing);
}
});
$.web2py.component_handler(target);
},
}
}
/*end of functions */
/*main hook*/
$(function () {
$(function() {
var flash = $('.flash');
flash.hide();
if(flash.html()) web2py.flash(flash.html());
@@ -720,5 +724,4 @@ web2py_event_handlers = jQuery.web2py.event_handlers;
web2py_trap_link = jQuery.web2py.trap_link;
web2py_calc_entropy = jQuery.web2py.calc_entropy;
*/
/* compatibility code - end*/
/* compatibility code - end*/
+121 -118
View File
@@ -1,4 +1,4 @@
(function ($, undefined) {
(function($, undefined) {
/*
* Unobtrusive scripting adapter for jQuery, largely taken from
* the wonderful https://github.com/rails/jquery-ujs
@@ -12,29 +12,29 @@
}
String.prototype.reverse = function () {
String.prototype.reverse = function() {
return this.split('').reverse().join('');
};
var web2py;
$.web2py = web2py = {
popup: function (url) {
popup: function(url) {
/* popup a window */
newwindow = window.open(url, 'name', 'height=400,width=600');
if(window.focus) newwindow.focus();
return false;
},
collapse: function (id) {
collapse: function(id) {
/* toggle an element */
$('#' + id).slideToggle();
},
fade: function (id, value) {
fade: function(id, value) {
/*fade something*/
if(value > 0) $('#' + id).hide().fadeIn('slow');
else $('#' + id).show().fadeOut('slow');
},
ajax: function (u, s, t) {
ajax: function(u, s, t) {
/*simple ajax function*/
query = '';
if(typeof s == "string") {
@@ -59,7 +59,7 @@
type: "POST",
url: u,
data: query,
success: function (msg) {
success: function(msg) {
if(t) {
if(t == ':eval') eval(msg);
else if(typeof t == 'string') $("#" + t).html(msg);
@@ -68,7 +68,7 @@
}
});
},
ajax_fields: function (target) {
ajax_fields: function(target) {
/*
*this attaches something to a newly loaded fragment/page
* Ideally all events should be bound to the document, so we can avoid calling
@@ -78,11 +78,11 @@
$('button', target).addClass('btn');
$('form input[type="submit"], form input[type="button"]', target).addClass('btn');
/* javascript for PasswordWidget*/
$('input[type=password][data-w2p_entropy]', target).each(function () {
$('input[type=password][data-w2p_entropy]', target).each(function() {
web2py.validate_entropy($(this));
});
/* javascript for ListWidget*/
$('ul.w2p_list', target).each(function () {
$('ul.w2p_list', target).each(function() {
function pe(ul, e) {
var new_line = ml(ul);
rel(ul);
@@ -113,25 +113,25 @@
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();
else $(this.firstChild).val(trimmed);
});
}
var ul = this;
$(ul).find(":text").after('<a href="#">+</a>&nbsp;<a href="#">-</a>').keypress(function (e) {
$(ul).find(":text").after('<a href="#">+</a>&nbsp;<a href="#">-</a>').keypress(function(e) {
return(e.which == 13) ? pe(ul, e) : true;
}).next().click(function (e) {
}).next().click(function(e) {
pe(ul, e);
e.preventDefault();
}).next().click(function (e) {
}).next().click(function(e) {
rl(ul, e);
e.preventDefault();
});
});
},
ajax_init: function (target) {
ajax_init: function(target) {
/*called whenever a fragment gets loaded */
$('.hidden', target).hide();
web2py.manage_errors(target);
@@ -140,10 +140,10 @@
web2py.component_handler(target);
},
/* manage errors in forms */
manage_errors: function (target) {
manage_errors: function(target) {
$('.error', target).hide().slideDown('slow');
},
after_ajax: function (xhr) {
after_ajax: function(xhr) {
/* called whenever an ajax request completes */
var command = xhr.getResponseHeader('web2py-component-command');
var flash = xhr.getResponseHeader('web2py-component-flash');
@@ -154,33 +154,33 @@
web2py.flash(decodeURIComponent(flash))
}
},
event_handlers: function () {
event_handlers: function() {
/*
* This is called once for page
* Ideally it should bound all the things that are needed
* and require no dom manipulations
*/
var doc = $(document);
doc.on('click', '.flash', function (e) {
doc.on('click', '.flash', function(e) {
var t = $(this);
if(t.css('top') == '0px') t.slideUp('slow');
else t.fadeOut();
});
doc.on('keyup', 'input.integer', function () {
var nvalue = this.value.reverse().replace(/[^0-9\-]|\-(?=.)/g, '').reverse();
if(this.value!=nvalue) this.value = nvalue;
doc.on('keyup', 'input.integer', function() {
var nvalue = this.value.reverse().replace(/[^0-9\-]|\-(?=.)/g, '').reverse();
if(this.value != nvalue) this.value = nvalue;
});
doc.on('keyup', 'input.double, input.decimal', function () {
doc.on('keyup', 'input.double, input.decimal', function() {
var nvalue = this.value.reverse().replace(/[^0-9\-\.,]|[\-](?=.)|[\.,](?=[0-9]*[\.,])/g, '').reverse();
if(this.value!=nvalue) this.value = nvalue;
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 () {
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 () {
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;
@@ -197,7 +197,7 @@
}
});
var date_format = (typeof w2p_ajax_date_format != 'undefined') ? w2p_ajax_date_format : "%Y-%m-%d";
doc.on('click', "input.date", function () {
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;
@@ -212,7 +212,7 @@
$(this).trigger('click');
}
});
doc.on('focus', "input.time", function () {
doc.on('focus', "input.time", function() {
var active = $(this).data('w2p_time');
if(active === undefined) {
$(this).timeEntry({
@@ -222,11 +222,11 @@
}
});
/* help preventing double form submission for normal form (not LOADed) */
$(doc).on('submit', 'form', function () {
$(doc).on('submit', 'form', function() {
var submit_button = $(this).find(web2py.formInputClickSelector);
web2py.disableElement(submit_button);
});
doc.ajaxSuccess(function (e, xhr) {
doc.ajaxSuccess(function(e, xhr) {
var redirect = xhr.getResponseHeader('web2py-redirect-location');
if(redirect !== null) {
window.location = redirect;
@@ -237,37 +237,37 @@
};
});
doc.ajaxError(function (e, xhr, settings, exception) {
doc.ajaxError(function(e, xhr, settings, exception) {
/*personally I don't like it.
*if there's an error it it flashed and can be removed
*as any other message
*doc.off('click', '.flash')
*/
switch(xhr.status) {
case 500:
web2py.flash(ajax_error_500);
case 500:
web2py.flash(ajax_error_500);
}
});
},
trap_form: function (action, target) {
trap_form: function(action, target) {
/* traps any LOADed form */
$('#' + target + ' form').each(function (i) {
$('#' + target + ' form').each(function(i) {
var form = $(this);
if (form.hasClass('no_trap')) {
if(form.hasClass('no_trap')) {
return;
}
form.attr('data-w2p_target', target);
var url = form.attr('action');
if ((url === "") || (url === "#")) {
if((url === "") || (url === "#") || (typeof url === 'undefined')) {
/* form has no action. Use component url. */
url = action;
}
form.submit(function (e) {
form.submit(function(e) {
web2py.disableElement(form.find(web2py.formInputClickSelector));
web2py.hide_flash();
web2py.ajax_page('post', url, form.serialize(), target, form);
@@ -277,28 +277,28 @@
},
ajax_page: function (method, action, data, target, element) {
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 target is not there, fill it with something that there isn't in the page*/
if(target == undefined || target == '') target = 'w2p_none';
if(web2py.fire(element, 'ajax:before', null, target )) { /*test a usecase, should stop here if returns false */
if(web2py.fire(element, 'ajax:before', null, target)) { /*test a usecase, should stop here if returns false */
$.ajax({
'type': method,
'url': action,
'data': data,
'beforeSend': function (xhr, settings) {
'beforeSend': function(xhr, settings) {
xhr.setRequestHeader('web2py-component-location', document.location);
xhr.setRequestHeader('web2py-component-element', target);
return web2py.fire(element, 'ajax:beforeSend', [xhr, settings], target); //test a usecase, should stop here if returns false
},
'success': function (data, status, xhr) {
'success': function(data, status, xhr) {
/*bummer for form submissions....the element is not there after complete
*because it gets replaced by the new response....
*/
web2py.fire(element, 'ajax:success', [data, status, xhr], target);
},
'error': function (xhr, status, error) {
'error': function(xhr, status, error) {
/*bummer for form submissions....in addition to the element being not there after
*complete because it gets replaced by the new response, standard form
*handling just returns the same status code for good and bad
@@ -306,9 +306,9 @@
*/
web2py.fire(element, 'ajax:error', [xhr, status, error], target);
},
'complete': function (xhr, status) {
'complete': function(xhr, status) {
web2py.fire(element, 'ajax:complete', [xhr, status], target);
web2py.updatePage(xhr, target); /* Parse and load the html received */
web2py.updatePage(xhr, target); /* Parse and load the html received */
web2py.trap_form(action, target);
web2py.ajax_init('#' + target);
web2py.after_ajax(xhr);
@@ -316,13 +316,13 @@
});
}
},
component: function (action, target, timeout, times, el) {
component: function(action, target, timeout, times, el) {
/* element is a new parameter, but should be put in front */
$(function () {
$(function() {
var jelement = $("#" + target);
var element = jelement.get(0);
var statement = "jQuery('#" + target + "').get(0).reload();";
element.reload = function () {
element.reload = function() {
/* Continue if times is Infinity or
* the times limit is not reached
*/
@@ -331,7 +331,7 @@
}
};
/* Method to check timing limit */
element.reload_check = function () {
element.reload_check = function() {
if(jelement.hasClass('w2p_component_stop')) {
clearInterval(this.timing);
return false;
@@ -377,28 +377,30 @@
}
});
},
updatePage: function (xhr, target) {
var t = $('#' + target);
var html = $.parseHTML(xhr.responseText, document, true);
var title_elements = $(html).filter('title').add($(html).find('title'));
var title = title_elements.last().text();
if (title) {
title_elements.remove(); /* Remove any title elements from the response */
document.title = $.trim(title); /* Set the new document title */
}
var content = xhr.getResponseHeader('web2py-component-content');
if(content == 'prepend') t.prepend(xhr.responseText);
else if(content == 'append') t.append(xhr.responseText);
else if(content != 'hide') t.html(html);
updatePage: function(xhr, target) {
var t = $('#' + target);
var html = $.parseHTML(xhr.responseText, document, true);
var title_elements = $(html).filter('title').add($(html).find('title'));
var title = title_elements.last().text();
if(title) {
title_elements.remove(); /* Remove any title elements from the response */
document.title = $.trim(title); /* Set the new document title */
}
var content = xhr.getResponseHeader('web2py-component-content');
if(content == 'prepend') t.prepend(xhr.responseText);
else if(content == 'append') t.append(xhr.responseText);
else if(content != 'hide') t.html(html);
},
calc_entropy: function (mystring) {
calc_entropy: function(mystring) {
/* calculate a simple entropy for a given string */
var csets = new Array(
'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'0123456789', '!@#$\%^&*()', '~`-_=+[]{}\|;:\'",.<>?/',
'0123456789abcdefghijklmnopqrstuvwxyz');
var score = 0,
other = {}, seen = {}, lastset = null,
other = {},
seen = {},
lastset = null,
mystringlist = mystring.split('');
for(var i = 0; i < mystringlist.length; i++) { /* classify this character */
var c = mystringlist[i],
@@ -424,14 +426,14 @@
var entropy = mystring.length * Math.log(score) / 0.6931471805599453;
return Math.round(entropy * 100) / 100
},
validate_entropy: function (myfield, req_entropy) {
validate_entropy: function(myfield, req_entropy) {
if(myfield.data('w2p_entropy') != undefined) req_entropy = myfield.data('w2p_entropy');
var validator = function () {
var validator = function() {
var v = (web2py.calc_entropy(myfield.val()) || 0) / req_entropy;
var r = 0,
g = 0,
b = 0,
rs = function (x) {
rs = function(x) {
return Math.round(x * 15).toString(16)
};
if(v <= 0.5) {
@@ -448,12 +450,12 @@
}
if(!myfield.hasClass('entropy_check')) myfield.on('keyup', validator).on('keydown', validator).addClass('entropy_check');
},
web2py_websocket: function (url, onmessage, onopen, onclose) {
web2py_websocket: function(url, onmessage, onopen, onclose) {
if("WebSocket" in window) {
var ws = new WebSocket(url);
ws.onopen = onopen ? onopen : (function () {});
ws.onopen = onopen ? onopen : (function() {});
ws.onmessage = onmessage;
ws.onclose = onclose ? onclose : (function () {});
ws.onclose = onclose ? onclose : (function() {});
return true; /* supported */
} else return false; /* not supported */
},
@@ -465,34 +467,36 @@
/* Form input elements re-enabled after form submission */
enableSelector: 'input:disabled, button:disabled, textarea:disabled, select:disabled',
/* Triggers an event on an element and returns false if the event result is false */
fire: function (obj, type, data, target) {
var event = $.Event(type, {'containerTarget': $('#' + target)[0]});
fire: function(obj, type, data, target) {
var event = $.Event(type, {
'containerTarget': $('#' + target)[0]
});
obj.trigger(event, data);
return event.result !== false;
},
/* Helper function, needed to provide consistent behavior in IE */
stopEverything: function (e) {
stopEverything: function(e) {
$(e.target).trigger('w2p:everythingStopped');
e.stopImmediatePropagation();
return false;
},
confirm: function (message) {
confirm: function(message) {
return confirm(message);
},
/* replace element's html with the 'data-disable-with' after storing original html
* and prevent clicking on it */
disableElement: function (el) {
disableElement: function(el) {
el.addClass('disabled');
var method = el.prop('type') == 'submit' ? 'val' : 'html';
var disable_with_message = (typeof w2p_ajax_disable_with_message != 'undefined') ? w2p_ajax_disable_with_message : "Working...";
/*store enabled state if not already disabled */
if (el.data('w2p:enable-with') === undefined) {
el.data('w2p:enable-with', el[method]());
if(el.data('w2p:enable-with') === undefined) {
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]());
*/
* 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)) {
el.data('w2p_disable_with', disable_with_message);
}
@@ -500,13 +504,13 @@
/* set to disabled state*/
el[method](el.data('w2p_disable_with'));
el.bind('click.w2pDisable', function (e) { /* prevent further clicking*/
el.bind('click.w2pDisable', function(e) { /* prevent further clicking*/
return web2py.stopEverything(e);
});
},
/* restore element to its original state which was disabled by 'disableElement' above*/
enableElement: function (el) {
enableElement: function(el) {
var method = el.prop('type') == 'submit' ? 'val' : 'html';
if(el.data('w2p:enable-with') !== undefined) {
/* set to old enabled state */
@@ -517,22 +521,22 @@
el.unbind('click.w2pDisable');
},
/*convenience wrapper, internal use only */
simple_component: function (action, target, element) {
simple_component: function(action, target, element) {
web2py.component(action, target, 0, 1, element);
},
/*helper for flash messages*/
flash: function (message, status) {
flash: function(message, status) {
var flash = $('.flash');
web2py.hide_flash();
flash.html(message).addClass(status);
if(flash.html()) flash.append('<span id="closeflash"> &times; </span>').slideDown();
},
hide_flash: function () {
hide_flash: function() {
$('.flash').fadeOut(0).html('');
},
show_if_handler: function (target) {
show_if_handler: function(target) {
var triggers = {};
var show_if = function () {
var show_if = function() {
var t = $(this);
var id = t.attr('id');
t.attr('value', t.val());
@@ -543,12 +547,12 @@
else tr.hide();
}
};
$('[data-show-trigger]', target).each(function () {
$('[data-show-trigger]', target).each(function() {
var name = $(this).attr('data-show-trigger');
// The field exists only when creating/editing a row
if ($('#' + name).length) {
if(!triggers[name]) triggers[name] = [];
triggers[name].push($(this).attr('id'));
if($('#' + name).length) {
if(!triggers[name]) triggers[name] = [];
triggers[name].push($(this).attr('id'));
}
});
for(var name in triggers) {
@@ -556,8 +560,8 @@
show_if.call($('#' + name, target));
};
},
component_handler: function (target) {
$('div[data-w2p_remote]', target).each(function () {
component_handler: function(target) {
$('div[data-w2p_remote]', target).each(function() {
var remote, times, timeout, target;
var el = $(this);
remote = el.data('w2p_remote');
@@ -567,7 +571,7 @@
web2py.component(remote, target, timeout, times, $(this));
})
},
a_handler: function (el, e) {
a_handler: function(el, e) {
e.preventDefault();
var method = el.data('w2p_method');
var action = el.attr('href');
@@ -599,13 +603,13 @@
}
}
},
a_handlers: function () {
a_handlers: function() {
var el = $(document);
el.on('click', 'a[data-w2p_method]', function (e) {
el.on('click', 'a[data-w2p_method]', function(e) {
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(e) {
var el = $(this);
var toremove = el.data('w2p_remove');
if(toremove != undefined) {
@@ -617,11 +621,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(e) {
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(e) {
web2py.enableElement($(this));
});
},
@@ -630,16 +634,16 @@
- Replaces element text with value of 'data-disable-with' attribute
- Sets disabled property to true
*/
disableFormElements: function (form) {
form.find(web2py.disableSelector).each(function () {
disableFormElements: function(form) {
form.find(web2py.disableSelector).each(function() {
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]())
}
if (element.data('w2p:enable-with') === undefined) {
element.data('w2p:enable-with', element[method]());
if(element.data('w2p:enable-with') === undefined) {
element.data('w2p:enable-with', element[method]());
}
element[method](element.data('w2p_disable_with'));
element.prop('disabled', true);
@@ -650,42 +654,42 @@
- Replaces element text with cached value from 'ujs:enable-with' data store (created in `disableFormElements`)
- Sets disabled property to false
*/
enableFormElements: function (form) {
form.find(web2py.enableSelector).each(function () {
enableFormElements: function(form) {
form.find(web2py.enableSelector).each(function() {
var element = $(this),
method = element.is('button') ? 'html' : 'val';
if(element.data('w2p:enable-with')) {
element[method](element.data('w2p:enable-with'));
element.removeData('w2p:enable-with');
element[method](element.data('w2p:enable-with'));
element.removeData('w2p:enable-with');
}
element.prop('disabled', false);
});
},
form_handlers: function () {
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(e) {
web2py.disableFormElements($(this));
});
el.on('ajax:complete', 'form[data-w2p_target]', function (e) {
el.on('ajax:complete', 'form[data-w2p_target]', function(e) {
web2py.enableFormElements($(this));
});
},
/* Invalidate and force reload of a web2py component
*/
*/
invalidate: function(target) {
$('div[data-w2p_remote]', target).each(function () {
$('div[data-w2p_remote]', target).each(function() {
var el = $('#' + $(this).attr('id')).get(0);
if (el.timing !== undefined) { // Block triggering regular routines
clearInterval(el.timing);
if(el.timing !== undefined) { // Block triggering regular routines
clearInterval(el.timing);
}
});
$.web2py.component_handler(target);
},
}
}
/*end of functions */
/*main hook*/
$(function () {
$(function() {
var flash = $('.flash');
flash.hide();
if(flash.html()) web2py.flash(flash.html());
@@ -720,5 +724,4 @@ web2py_event_handlers = jQuery.web2py.event_handlers;
web2py_trap_link = jQuery.web2py.trap_link;
web2py_calc_entropy = jQuery.web2py.calc_entropy;
*/
/* compatibility code - end*/
/* compatibility code - end*/