/files/w2ui/1.2/w2ui.js
JavaScript | 1394 lines | 1167 code | 117 blank | 110 comment | 509 complexity | a60c9a2f24978dcc48c7f55f5d1f353d MD5 | raw file
- var w2ui = w2ui || {};
- var w2obj = w2obj || {}; // expose object to be able to overwrite default functions
- /************************************************
- * Library: Web 2.0 UI for jQuery
- * - Following objects are defines
- * - w2ui - object that contains all created objects
- * - w2utils - basic utilities
- * - w2ui.w2evet - generic event object
- * - Dependencies: jQuery
- *
- * == NICE TO HAVE ==
- *
- ************************************************/
- var w2utils = (function () {
- var obj = {
- settings : {
- locale : "en-us",
- date_format : "mm/dd/yyyy",
- date_display : "Mon dd, yyyy",
- time_format : "hh:mi pm",
- currency : "^[\$\€\£\¥]?[-]?[0-9]*[\.]?[0-9]+$",
- float : "^[-]?[0-9]*[\.]?[0-9]+$",
- shortmonths : ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"],
- fullmonths : ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
- shortdays : ["M", "T", "W", "T", "F", "S","S"],
- fulldays : ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"],
- RESTfull : false,
- phrases : {} // empty object for english phrases
- },
- isInt : isInt,
- isFloat : isFloat,
- isMoney : isMoney,
- isHex : isHex,
- isAlphaNumeric : isAlphaNumeric,
- isEmail : isEmail,
- isDate : isDate,
- isTime : isTime,
- size : size,
- age : age,
- formatDate : formatDate,
- date : date,
- stripTags : stripTags,
- encodeTags : encodeTags,
- escapeId : escapeId,
- base64encode : base64encode,
- base64decode : base64decode,
- transition : transition,
- getSize : getSize,
- lang : lang,
- locale : locale
- }
- return obj;
-
- function isInt (val) {
- var re = /^[-]?[0-9]+$/;
- return re.test(val);
- }
-
- function isFloat (val) {
- var re = new RegExp(w2utils.settings.float);
- return re.test(val);
- }
- function isMoney (val) {
- var re = new RegExp(w2utils.settings.currency);
- return re.test(val);
- }
-
- function isHex (val) {
- var re = /^[a-fA-F0-9]+$/;
- return re.test(val);
- }
-
- function isAlphaNumeric (val) {
- var re = /^[a-zA-Z0-9_-]+$/;
- return re.test(val);
- }
-
- function isEmail (val) {
- var email = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
- return email.test(val);
- }
- function isDate (val, format) {
- if (typeof val == 'undefined' || val == null) return false;
- if (typeof format == 'undefined' || format == null) format = w2utils.settings.date_format;
- // European format dd/mm/yyyy
- if (format.toLowerCase() == 'dd/mm/yyyy' || format.toLowerCase() == 'dd-mm-yyyy' || format.toLowerCase() == 'dd.mm.yyyy') {
- val = val.replace(/-/g, '/').replace(/\./g, '/');
- if (val.split("/").length != 3) return false;
- var month = val.split("/")[1];
- var day = val.split("/")[0];
- var year = val.split("/")[2];
- var obj = new Date(year, month-1, day);
- if ((obj.getMonth()+1 != month) || (obj.getDate() != day) || (obj.getFullYear() != year)) return false;
- return true;
- }
- // US Format will get converted normally
- var dt = new Date(val.replace(/-/g, '/').replace(/\./g, '/'));
- if (dt == 'Invalid Date') return false;
- // make sure it is in correct format
- if (typeof format != 'undefined') {
- var dt = this.formatDate(dt, format);
- if (dt != val) return false;
- }
- return true;
- }
- function isTime (val) {
- // Both formats 10:20pm and 22:20
- if (String(val) == 'undefined') return false;
- var max;
- // -- process american foramt
- val = val.toUpperCase();
- if (val.indexOf('PM') >= 0 || val.indexOf('AM') >= 0) max = 12; else max = 23;
- val = $.trim(val.replace('AM', ''));
- val = $.trim(val.replace('PM', ''));
- // ---
- var tmp = val.split(':');
- if (tmp.length != 2) { return false; }
- if (tmp[0] == '' || parseInt(tmp[0]) < 0 || parseInt(tmp[0]) > max || !this.isInt(tmp[0])) { return false; }
- if (tmp[1] == '' || parseInt(tmp[1]) < 0 || parseInt(tmp[1]) > 59 || !this.isInt(tmp[1])) { return false; }
- return true;
- }
- function formatDate (dateStr, format) { // IMPORTANT dateStr HAS TO BE valid JavaScript Date String
- var months = w2utils.settings.shortmonths;
- var fullMonths = w2utils.settings.fullmonths;
- if (typeof format == 'undefined') format = this.settings.date_format;
- if (typeof dateStr == 'undefined' || dateStr == '' || dateStr == null) return '';
- var dt = new Date(dateStr);
- if (w2utils.isInt(dateStr)) dt = new Date(Number(dateStr)); // for unix timestamps
- var tmp = String(dateStr).split('-');
- if (tmp.length == 3) dt = new Date(tmp[0], Number(tmp[1])-1, tmp[2]); // yyyy-mm-dd
- var tmp = String(dateStr).split('/');
- if (tmp.length == 3) dt = new Date(tmp[2], Number(tmp[0])-1, tmp[1]); // mm/dd/yyyy
- if (dt == 'Invalid Date') return '';
- var year = dt.getFullYear();
- var month = dt.getMonth();
- var date = dt.getDate();
- var res = format.toLowerCase()
- .replace('yyyy', year)
- .replace('mm', month+1)
- .replace('dd', date)
- .replace('mon', months[month])
- .replace('month', fullMonths[month]);
- return res;
- }
-
- function date (dateStr) {
- var months = w2utils.settings.shortmonths;
- if (dateStr == '' || typeof dateStr == 'undefined' || dateStr == null) return '';
- if (w2utils.isInt(dateStr)) dateStr = Number(dateStr); // for unix timestamps
-
- var d1 = new Date(dateStr);
- if (w2utils.isInt(dateStr)) d1 = new Date(Number(dateStr)); // for unix timestamps
- var tmp = String(dateStr).split('-');
- if (tmp.length == 3) d1 = new Date(tmp[0], Number(tmp[1])-1, tmp[2]); // yyyy-mm-dd
- var tmp = String(dateStr).split('/');
- if (tmp.length == 3) d1 = new Date(tmp[2], Number(tmp[0])-1, tmp[1]); // mm/dd/yyyy
- if (d1 == 'Invalid Date') return '';
- var d2 = new Date(); // today
- var d3 = new Date();
- d3.setTime(d3.getTime() - 86400000); // yesterday
-
- var dd1 = months[d1.getMonth()] + ' ' + d1.getDate() + ', ' + d1.getFullYear();
- var dd2 = months[d2.getMonth()] + ' ' + d2.getDate() + ', ' + d2.getFullYear();
- var dd3 = months[d3.getMonth()] + ' ' + d3.getDate() + ', ' + d3.getFullYear();
-
- var time = (d1.getHours() - (d1.getHours() > 12 ? 12 :0)) + ':' + (d1.getMinutes() < 10 ? '0' : '') + d1.getMinutes() + ' ' + (d1.getHours() >= 12 ? 'pm' : 'am');
- var time2= (d1.getHours() - (d1.getHours() > 12 ? 12 :0)) + ':' + (d1.getMinutes() < 10 ? '0' : '') + d1.getMinutes() + ':' + (d1.getSeconds() < 10 ? '0' : '') + d1.getSeconds() + ' ' + (d1.getHours() >= 12 ? 'pm' : 'am');
- var dsp = dd1;
- if (dd1 == dd2) dsp = time;
- if (dd1 == dd3) dsp = w2utils.lang('Yesterday');
- return '<span title="'+ dd1 +' ' + time2 +'">'+ dsp +'</span>';
- }
- function age (timeStr) {
- if (timeStr == '' || typeof timeStr == 'undefined' || timeStr == null) return '';
- if (w2utils.isInt(timeStr)) timeStr = Number(timeStr); // for unix timestamps
-
- var d1 = new Date(timeStr);
- if (w2utils.isInt(timeStr)) d1 = new Date(Number(timeStr)); // for unix timestamps
- var tmp = String(timeStr).split('-');
- if (tmp.length == 3) d1 = new Date(tmp[0], Number(tmp[1])-1, tmp[2]); // yyyy-mm-dd
- var tmp = String(timeStr).split('/');
- if (tmp.length == 3) d1 = new Date(tmp[2], Number(tmp[0])-1, tmp[1]); // mm/dd/yyyy
- if (d1 == 'Invalid Time') return '';
- var d2 = new Date();
- var sec = (d2.getTime() - d1.getTime()) / 1000;
- var amount = '';
- var type = '';
-
- if (sec < 60) {
- amount = Math.floor(sec);
- type = 'sec';
- } else if (sec < 60*60) {
- amount = Math.floor(sec/60);
- type = 'min';
- } else if (sec < 24*60*60) {
- amount = Math.floor(sec/60/60);
- type = 'hour';
- } else if (sec < 30*24*60*60) {
- amount = Math.floor(sec/24/60/60);
- type = 'day';
- } else if (sec < 12*30*24*60*60) {
- amount = Math.floor(sec/30/24/60/60);
- type = 'month';
- } else if (sec >= 12*30*24*60*60) {
- amount = Math.floor(sec/12/30/24/60/60);
- type = 'year';
- }
- return amount + ' ' + type + (amount > 1 ? 's' : '');
- }
-
- function size (sizeStr) {
- if (!w2utils.isFloat(sizeStr) || sizeStr == '') return '';
- sizeStr = parseFloat(sizeStr);
- var sizes = ['Bt', 'KB', 'MB', 'GB', 'TB'];
- var i = parseInt( Math.floor( Math.log(sizeStr) / Math.log(1024) ) );
- return (Math.floor(sizeStr / Math.pow(1024, i) * 10) / 10).toFixed(i == 0 ? 0 : 1) + ' ' + sizes[i];
- }
- function stripTags (html) {
- if (html == null) return html;
- switch (typeof html) {
- case 'number':
- break;
- case 'string':
- html = $.trim(String(html).replace(/(<([^>]+)>)/ig, ""));
- break;
- case 'object':
- for (var a in html) html[a] = this.stripTags(html[a]);
- break;
- }
- return html;
- }
- function encodeTags (html) {
- if (html == null) return html;
- switch (typeof html) {
- case 'number':
- break;
- case 'string':
- html = String(html).replace(/&/g, "&").replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);
- break;
- case 'object':
- for (var a in html) html[a] = this.encodeTags(html[a]);
- break;
- }
- return html;
- }
- function escapeId (id) {
- if (typeof id == 'undefined' || id == '' || id == null) return '';
- return String(id).replace(/([;&,\.\+\*\~'`:"\!\^#$%@\[\]\(\)=<>\|\/? {}\\])/g, '\\$1');
- }
- function base64encode (input) {
- var output = "";
- var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
- var i = 0;
- var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
- input = utf8_encode(input);
- while (i < input.length) {
- chr1 = input.charCodeAt(i++);
- chr2 = input.charCodeAt(i++);
- chr3 = input.charCodeAt(i++);
- enc1 = chr1 >> 2;
- enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
- enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
- enc4 = chr3 & 63;
- if (isNaN(chr2)) {
- enc3 = enc4 = 64;
- } else if (isNaN(chr3)) {
- enc4 = 64;
- }
- output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
- }
- function utf8_encode (string) {
- var string = String(string).replace(/\r\n/g,"\n");
- var utftext = "";
- for (var n = 0; n < string.length; n++) {
- var c = string.charCodeAt(n);
- if (c < 128) {
- utftext += String.fromCharCode(c);
- }
- else if((c > 127) && (c < 2048)) {
- utftext += String.fromCharCode((c >> 6) | 192);
- utftext += String.fromCharCode((c & 63) | 128);
- }
- else {
- utftext += String.fromCharCode((c >> 12) | 224);
- utftext += String.fromCharCode(((c >> 6) & 63) | 128);
- utftext += String.fromCharCode((c & 63) | 128);
- }
- }
- return utftext;
- }
- return output;
- }
- function base64decode (input) {
- var output = "";
- var chr1, chr2, chr3;
- var enc1, enc2, enc3, enc4;
- var i = 0;
- var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
- input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
- while (i < input.length) {
- enc1 = keyStr.indexOf(input.charAt(i++));
- enc2 = keyStr.indexOf(input.charAt(i++));
- enc3 = keyStr.indexOf(input.charAt(i++));
- enc4 = keyStr.indexOf(input.charAt(i++));
- chr1 = (enc1 << 2) | (enc2 >> 4);
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
- chr3 = ((enc3 & 3) << 6) | enc4;
- output = output + String.fromCharCode(chr1);
- if (enc3 != 64) {
- output = output + String.fromCharCode(chr2);
- }
- if (enc4 != 64) {
- output = output + String.fromCharCode(chr3);
- }
- }
- output = utf8_decode(output);
- function utf8_decode (utftext) {
- var string = "";
- var i = 0;
- var c = 0, c1 = 0, c2 = 0;
- while ( i < utftext.length ) {
- c = utftext.charCodeAt(i);
- if (c < 128) {
- string += String.fromCharCode(c);
- i++;
- }
- else if((c > 191) && (c < 224)) {
- c2 = utftext.charCodeAt(i+1);
- string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
- i += 2;
- }
- else {
- c2 = utftext.charCodeAt(i+1);
- c3 = utftext.charCodeAt(i+2);
- string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
- i += 3;
- }
- }
- return string;
- }
- return output;
- }
-
- function transition (div_old, div_new, type, callBack) {
- var width = $(div_old).width();
- var height = $(div_old).height();
- var time = 0.5;
-
- if (!div_old || !div_new) {
- console.log('ERROR: Cannot do transition when one of the divs is null');
- return;
- }
-
- div_old.parentNode.style.cssText += cross('perspective', '700px') +'; overflow: hidden;';
- div_old.style.cssText += '; position: absolute; z-index: 1019; '+ cross('backface-visibility', 'hidden');
- div_new.style.cssText += '; position: absolute; z-index: 1020; '+ cross('backface-visibility', 'hidden');
-
- switch (type) {
- case 'slide-left':
- // init divs
- div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d('+ width + 'px, 0, 0)', 'translate('+ width +'px, 0)');
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time+'s') +';'+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- div_old.style.cssText += cross('transition', time+'s') +';'+ cross('transform', 'translate3d(-'+ width +'px, 0, 0)', 'translate(-'+ width +'px, 0)');
- }, 1);
- break;
- case 'slide-right':
- // init divs
- div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(-'+ width +'px, 0, 0)', 'translate(-'+ width +'px, 0)');
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0px, 0, 0)', 'translate(0px, 0)');
- div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d('+ width +'px, 0, 0)', 'translate('+ width +'px, 0)');
- }, 1);
- break;
- case 'slide-down':
- // init divs
- div_old.style.cssText += 'overflow: hidden; z-index: 1; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- div_new.style.cssText += 'overflow: hidden; z-index: 0; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, '+ height +'px, 0)', 'translate(0, '+ height +'px)');
- }, 1);
- break;
- case 'slide-up':
- // init divs
- div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, '+ height +'px, 0)', 'translate(0, '+ height +'px)');
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- }, 1);
- break;
- case 'flip-left':
- // init divs
- div_old.style.cssText += 'overflow: hidden; '+ cross('-transform', 'rotateY(0deg)');
- div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateY(-180deg)');
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(0deg)');
- div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(180deg)');
- }, 1);
- break;
- case 'flip-right':
- // init divs
- div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateY(0deg)');
- div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateY(180deg)');
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(0deg)');
- div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateY(-180deg)');
- }, 1);
- break;
- case 'flip-down':
- // init divs
- div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(0deg)');
- div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(180deg)');
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(0deg)');
- div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(-180deg)');
- }, 1);
- break;
- case 'flip-up':
- // init divs
- div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(0deg)');
- div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'rotateX(-180deg)');
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(0deg)');
- div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'rotateX(180deg)');
- }, 1);
- break;
- case 'pop-in':
- // init divs
- div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') + '; '+ cross('transform', 'scale(.8)') + '; opacity: 0;';
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'scale(1)') +'; opacity: 1;';
- div_old.style.cssText += cross('transition', time+'s') +';';
- }, 1);
- break;
- case 'pop-out':
- // init divs
- div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') +'; '+ cross('transform', 'scale(1)') +'; opacity: 1;';
- div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') +'; opacity: 0;';
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time+'s') +'; opacity: 1;';
- div_old.style.cssText += cross('transition', time+'s') +'; '+ cross('transform', 'scale(1.7)') +'; opacity: 0;';
- }, 1);
- break;
- default:
- // init divs
- div_old.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)');
- div_new.style.cssText += 'overflow: hidden; '+ cross('transform', 'translate3d(0, 0, 0)', 'translate(0, 0)') +'; opacity: 0;';
- $(div_new).show();
- // -- need a timing function because otherwise not working
- window.setTimeout(function() {
- div_new.style.cssText += cross('transition', time +'s') +'; opacity: 1;';
- div_old.style.cssText += cross('transition', time +'s');
- }, 1);
- break;
- }
-
- setTimeout(function () {
- if (type == 'slide-down') {
- $(div_old).css('z-index', '1019');
- $(div_new).css('z-index', '1020');
- }
- if (div_new) {
- $(div_new).css({
- 'opacity': '1',
- '-webkit-transition': '',
- '-moz-transition': '',
- '-ms-transition': '',
- '-o-transition': '',
- '-webkit-transform': '',
- '-moz-transform': '',
- '-ms-transform': '',
- '-o-transform': '',
- '-webkit-backface-visibility': '',
- '-moz-backface-visibility': '',
- '-ms-backface-visibility': '',
- '-o-backface-visibility': ''
- });
- }
- if (div_old) {
- $(div_old).css({
- 'opacity': '1',
- '-webkit-transition': '',
- '-moz-transition': '',
- '-ms-transition': '',
- '-o-transition': '',
- '-webkit-transform': '',
- '-moz-transform': '',
- '-ms-transform': '',
- '-o-transform': '',
- '-webkit-backface-visibility': '',
- '-moz-backface-visibility': '',
- '-ms-backface-visibility': '',
- '-o-backface-visibility': ''
- });
- if (div_old.parentNode) $(div_old.parentNode).css({
- '-webkit-perspective': '',
- '-moz-perspective': '',
- '-ms-perspective': '',
- '-o-perspective': ''
- });
- }
- if (typeof callBack == 'function') callBack();
- }, time * 1000);
-
- function cross(property, value, none_webkit_value) {
- if (!$.browser.webkit && typeof none_webkit_value != 'undefined') value = none_webkit_value;
- return ';'+ property +': '+ value +'; -webkit-'+ property +': '+ value +'; -moz-'+ property +': '+ value +'; '+
- '-ms-'+ property +': '+ value +'; -o-'+ property +': '+ value +';';
- }
- }
-
- function getSize (el, type) {
- var bwidth = {
- left: parseInt($(el).css('border-left-width')) || 0,
- right: parseInt($(el).css('border-right-width')) || 0,
- top: parseInt($(el).css('border-top-width')) || 0,
- bottom: parseInt($(el).css('border-bottom-width')) || 0
- }
- var mwidth = {
- left: parseInt($(el).css('margin-left')) || 0,
- right: parseInt($(el).css('margin-right')) || 0,
- top: parseInt($(el).css('margin-top')) || 0,
- bottom: parseInt($(el).css('margin-bottom')) || 0
- }
- var pwidth = {
- left: parseInt($(el).css('padding-left')) || 0,
- right: parseInt($(el).css('padding-right')) || 0,
- top: parseInt($(el).css('padding-top')) || 0,
- bottom: parseInt($(el).css('padding-bottom')) || 0
- }
- switch (type) {
- case 'top': return bwidth.top + mwidth.top + pwidth.top;
- case 'bottom': return bwidth.bottom + mwidth.bottom + pwidth.bottom;
- case 'left': return bwidth.left + mwidth.left + pwidth.left;
- case 'right': return bwidth.right + mwidth.right + pwidth.right;
- case 'width': return bwidth.left + bwidth.right + mwidth.left + mwidth.right + pwidth.left + pwidth.right + parseInt($(el).width());
- case 'height': return bwidth.top + bwidth.bottom + mwidth.top + mwidth.bottom + pwidth.top + pwidth.bottom + parseInt($(el).height());
- case '+width': return bwidth.left + bwidth.right + mwidth.left + mwidth.right + pwidth.left + pwidth.right;
- case '+height': return bwidth.top + bwidth.bottom + mwidth.top + mwidth.bottom + pwidth.top + pwidth.bottom;
- }
- return 0;
- }
- function lang (phrase) {
- var translation = this.settings.phrases[phrase];
- if (typeof translation == 'undefined') return phrase; else return translation;
- }
- function locale (localParam) {
- var settings = w2utils.settings;
- var param;
- if (typeof localParam == 'string') localParam = { lang: localParam };
- $.extend( { path : '', lang : 'en-us' }, localParam);
- var result = localParam['lang'].toLowerCase().match(/^([a-z]{2})\-([a-z]{2})$/);
- if (result !== null && result.length === 3) {
- param = result[1] + '-' + result[2].toUpperCase();
- if ( param === settings.locale) {
- return param;
- }
- param = localParam.path + "locale/" + param.toLowerCase() + ".json";
- $.ajax({
- url: param,
- type: "GET",
- dataType: "json",
- cache : false,
- async : false,
- }).done(function( data) {
- $.extend( settings, data);
- }).fail(function(jqXHR, textStatus) {
- alert( "Request failed: " + textStatus );
- });
- }
- return settings.locale;
- }
- })();
- /***********************************************************
- * Generic Event Object
- * --- This object is reused across all other
- * --- widgets in w2ui.
- *
- *********************************************************/
- $.w2event = {
- on: function (eventData, handler) {
- if (!$.isPlainObject(eventData)) eventData = { type: eventData };
- eventData = $.extend({ type: null, execute: 'before', target: null, onComplete: null }, eventData);
-
- if (typeof eventData.type == 'undefined') { console.log('ERROR: You must specify event type when calling .on() method of '+ this.name); return; }
- if (typeof handler == 'undefined') { console.log('ERROR: You must specify event handler function when calling .on() method of '+ this.name); return; }
- this.handlers.push({ event: eventData, handler: handler });
- },
-
- off: function (eventData, handler) {
- if (!$.isPlainObject(eventData)) eventData = { type: eventData };
- eventData = $.extend({}, { type: null, execute: 'before', target: null, onComleted: null }, eventData);
-
- if (typeof eventData.type == 'undefined') { console.log('ERROR: You must specify event type when calling .off() method of '+ this.name); return; }
- if (typeof handler == 'undefined') { handler = null; }
- // remove handlers
- var newHandlers = [];
- for (var h in this.handlers) {
- var t = this.handlers[h];
- if ( (t.event.type == eventData.type || eventData.type == '*')
- && (t.event.target == eventData.target || eventData.target == null)
- && (t.handler == handler || handler == null)) {
- // match
- } else {
- newHandlers.push(t);
- }
- }
- this.handlers = newHandlers;
- },
-
- trigger: function (eventData) {
- var eventData = $.extend({ type: null, phase: 'before', target: null, stop: false }, eventData,
- { preventDefault: function () { this.stop = true; } });
- if (typeof eventData.target == 'undefined') eventData.target = null;
- // process events in REVERSE order
- for (var h = this.handlers.length-1; h >= 0; h--) {
- var t = this.handlers[h];
- if ( (t.event.type == eventData.type || t.event.type == '*')
- && (t.event.target == eventData.target || t.event.target == null)
- && (t.event.execute == eventData.phase || t.event.execute == '*' || t.event.phase == '*') ) {
- var ret = t.handler.call(this, eventData.target, eventData);
- if ($.isPlainObject(ret)) {
- $.extend(eventData, ret);
- if (eventData.stop === true) return eventData;
- }
- }
- }
- // main object events
- if (eventData.phase == 'before'
- && typeof this['on' + eventData.type.substr(0,1).toUpperCase() + eventData.type.substr(1)] == 'function') {
- var ret = this['on' + eventData.type.substr(0,1).toUpperCase() + eventData.type.substr(1)].call(this, eventData.target, eventData);
- if ($.isPlainObject(ret)) {
- $.extend(eventData, ret);
- if (eventData.stop === true) return eventData;
- }
- }
- // item object events
- if (typeof eventData.object != 'undefined' && eventData.object != null && eventData.phase == 'before'
- && typeof eventData.object['on' + eventData.type.substr(0,1).toUpperCase() + eventData.type.substr(1)] == 'function') {
- var ret = eventData.object['on' + eventData.type.substr(0,1).toUpperCase() + eventData.type.substr(1)].call(this, eventData.target, eventData);
- if ($.isPlainObject(ret)) {
- $.extend(eventData, ret);
- if (eventData.stop === true) return eventData;
- }
- }
- // execute onComplete
- if (eventData.phase == 'after' && eventData.onComplete != null) eventData.onComplete.call(this, eventData);
-
- return eventData;
- }
- };
- /***********************************************************
- * Commonly used plugins
- * --- used primarily in grid and form
- *
- *********************************************************/
- (function () {
- $.fn.w2render = function (name) {
- if ($(this).length > 0) {
- if (typeof name == 'string' && w2ui[name]) w2ui[name].render($(this)[0]);
- if (typeof name == 'object') name.render($(this)[0]);
- }
- }
- $.fn.w2destroy = function (name) {
- if (typeof name == 'undefined' && this.length > 0) name = this.data('w2name');
- if (typeof name == 'string' && w2ui[name]) w2ui[name].destroy();
- if (typeof name == 'object') name.destroy();
- }
- $.fn.w2lite = function () {
- }
-
- // -- w2tag - appears on the right side from element, there can be multiple on screen at a time
- $.fn.w2tag = function (text, options) {
- if (!$.isPlainObject(options)) options = {};
- if (!$.isPlainObject(options.css)) options.css = {};
- if (typeof options['class'] == 'undefined') options['class'] = '';
- // remove all tags
- if ($(this).length == 0) {
- $('.global-tag').each(function (index, elem) {
- var opt = $(elem).data('options');
- if (typeof opt == 'undefined') opt = {};
- $($(elem).data('taged-el')).removeClass( opt['class'] );
- clearInterval($(elem).data('timer'));
- $(elem).remove();
- });
- return;
- }
- return $(this).each(function (index, el) {
- // show or hide tag
- var tagOrigID = el.id;
- var tagID = w2utils.escapeId(el.id);
- if (text == '' || text == null || typeof text == 'undefined') {
- $('#global-tag-'+tagID).css('opacity', 0);
- setTimeout(function () {
- // remmove element
- clearInterval($('#global-tag-'+tagID).data('timer'));
- $('#global-tag-'+tagID).remove();
- }, 300);
- } else {
- // remove elements
- clearInterval($('#global-tag-'+tagID).data('timer'));
- $('#global-tag-'+tagID).remove();
- // insert
- $('body').append('<div id="global-tag-'+ tagOrigID +'" class="global-tag" '+
- ' style="position: absolute; z-index: 1701; opacity: 0; -webkit-transition: opacity .3s; -moz-transition: opacity .3s; -ms-transition: opacity .3s; -o-transition: opacity .3s"></div>');
- var timer = setInterval(function () {
- // monitor if destroyed
- if ($(el).length == 0 || ($(el).offset().left == 0 && $(el).offset().top == 0)) {
- clearInterval($('#global-tag-'+tagID).data('timer'));
- tmp_hide();
- return;
- }
- // monitor if moved
- if ($('#global-tag-'+tagID).data('position') != ($(el).offset().left + el.offsetWidth) + 'x' + $(el).offset().top) {
- $('#global-tag-'+tagID).css({
- '-webkit-transition': '.2s',
- '-moz-transition': '.2s',
- '-ms-transition': '.2s',
- '-o-transition': '.2s',
- left: ($(el).offset().left + el.offsetWidth) + 'px',
- top: $(el).offset().top + 'px'
- }).data('position', ($(el).offset().left + el.offsetWidth) + 'x' + $(el).offset().top);
- }
- }, 100);
- setTimeout(function () {
- if (!$(el).offset()) return;
- $('#global-tag-'+tagID).css({
- opacity: '1',
- left: ($(el).offset().left + el.offsetWidth) + 'px',
- top: $(el).offset().top + 'px'
- }).html('<div style="margin-top: -2px 0px 0px -2px; white-space: nowrap;"> <div class="bubble-tag">'+ text +'</div> </div>')
- .data('text', text)
- .data('taged-el', el)
- .data('options', options)
- .data('position', ($(el).offset().left + el.offsetWidth) + 'x' + $(el).offset().top)
- .data('timer', timer);
- $(el).off('keypress', tmp_hide).on('keypress', tmp_hide).off('change', tmp_hide).on('change', tmp_hide)
- .css(options.css).addClass(options['class']);
- if (typeof options.onShow == 'function') options.onShow();
- }, 1);
- var originalCSS = '';
- if ($(el).length > 0) originalCSS = $(el)[0].style.cssText;
- // bind event to hide it
- function tmp_hide() {
- if ($('#global-tag-'+tagID).length <= 0) return;
- clearInterval($('#global-tag-'+tagID).data('timer'));
- $('#global-tag-'+tagID).remove();
- $(el).off('keypress', tmp_hide).removeClass(options['class']);
- if ($(el).length > 0) $(el)[0].style.cssText = originalCSS;
- if (typeof options.onHide == 'function') options.onHide();
- }
- }
- });
- }
-
- // w2overlay - appears under the element, there can be only one at a time
- $.fn.w2overlay = function (html, options) {
- var isOpened = false;
- if (!$.isPlainObject(options)) options = {};
- if (!$.isPlainObject(options.css)) options.css = {};
- if (this.length == 0 || html == '' || typeof html == 'undefined') {
- if (typeof options.onHide == 'function') options.onHide();
- $('#w2ui-overlay').remove();
- $(document).off('click', hide);
- return;
- }
- // insert (or re-insert) overlay
- if ($('#w2ui-overlay').length > 0) { isOpened = true; $(document).off('click', hide); $('#w2ui-overlay').remove(); }
- $('body').append('<div id="w2ui-overlay" class="w2ui-reset w2ui-overlay"><div></div></div>');
- // init
- var obj = this;
- var div = $('#w2ui-overlay div');
- div.css(options.css).html(html);
- if (typeof options['class'] != 'undefined') div.addClass(options['class']);
- if (typeof options.top == 'undefined') options.top = 0;
- if (typeof options.left == 'undefined') options.left = 0;
- if (typeof options.width == 'undefined') options.width = 100;
- if (typeof options.height == 'undefined') options.height = 0;
- // pickup bg color of first div
- var bc = div.css('background-color');
- var div = $('#w2ui-overlay');
- if (typeof bc != 'undefined' && bc != 'rgba(0, 0, 0, 0)' && bc != 'transparent') div.css('background-color', bc);
- div.css({
- display : 'none',
- left : ($(obj).offset().left + options.left) + 'px',
- top : ($(obj).offset().top + w2utils.getSize($(obj), 'height') + 3 + options.top) + 'px',
- 'min-width' : (options.width ? options.width : 'auto'),
- 'min-height' : (options.height ? options.height : 'auto')
- })
- .fadeIn('fast')
- .data('position', ($(obj).offset().left) + 'x' + ($(obj).offset().top + obj.offsetHeight))
- .on('click', function (event) {
- if (event.stopPropagation) event.stopPropagation(); else event.cancelBubble = true;
- });
- // click anywhere else hides the drop down
- var hide = function () {
- if (typeof options.onHide == 'function') options.onHide();
- $('#w2ui-overlay').remove();
- $(document).off('click', hide);
- }
- // need time to display
- setTimeout(function () {
- $(document).on('click', hide);
- if (typeof options.onShow == 'function') options.onShow();
- }, 1);
- }
- })();
- /************************************************************************
- * Library: Web 2.0 UI for jQuery (using prototypical inheritance)
- * - Following objects defined
- * - w2ui.w2grid - grid widget
- * - $.w2grid - jQuery wrapper
- * - Dependencies: jQuery, w2utils, w2toolbar, w2fields, w2popup
- *
- * == NICE TO HAVE ==
- * - global search apply types and drop downs
- * - editable fields (list) - better inline editing
- * - move doExpand into the record
- * - infinite scroll (buffered scroll)
- * - frozen columns
- * - column autosize based on largest content
- *
- ************************************************************************/
- (function () {
- var w2grid = function(options) {
- // public properties
- this.name = null;
- this.box = null; // HTML element that hold this element
- this.header = '';
- this.url = '';
- this.columns = []; // { field, caption, size, attr, render, hidden, gridMinWidth, [editable: {type, inTag, outTag, style, items}] }
- this.columnGroups = []; // { span: int, caption: 'string', master: true/false }
- this.records = []; // { recid: int(requied), field1: 'value1', ... fieldN: 'valueN', style: 'string', editable: true/false, summary: true/false }
- this.searches = []; // { type, caption, field, inTag, outTag, default, items, hidden }
- this.searchData = [];
- this.sortData = [];
- this.postData = {};
- this.toolbar = {}; // if not empty object; then it is toolbar object
- this.show = {
- header : false,
- toolbar : false,
- footer : false,
- columnHeaders : true,
- lineNumbers : false,
- expandColumn : false,
- selectColumn : false,
- emptyRecords : true,
- toolbarReload : true,
- toolbarColumns : true,
- toolbarSearch : true,
- toolbarAdd : false,
- toolbarDelete : false,
- toolbarSave : false
- },
- this.fixedBody = true; // if false; then grid grows with data
- this.fixedRecord = true; // if false; then record height grows with data
- this.multiSearch = true;
- this.multiSelect = true;
- this.multiSort = true;
- this.keyboard = true; // if user clicks on the list; it will bind all events from the keyboard for that list
- this.total = 0; // total number of records
- this.page = 0; // current page
- this.recordsPerPage = 50;
- this.style = '';
- this.msgDelete = w2utils.lang('Are you sure you want to delete selected records?');
- this.msgNotJSON = w2utils.lang('Returned data is not in valid JSON format.');
- this.msgRefresh = w2utils.lang('Refreshing...');
- // events
- this.onRequest = null; // called on any server event
- this.onLoad = null;
- this.onAdd = null;
- this.onDelete = null;
- this.onSave = null;
- this.onSelect = null;
- this.onUnselect = null;
- this.onClick = null;
- this.onDblClick = null;
- this.onSort = null;
- this.onSearch = null;
- this.onChange = null; // called when editable record is changed
- this.onExpand = null;
- this.onError = null;
- this.onRender = null;
- this.onRefresh = null;
- this.onResize = null;
- this.onDestroy = null;
- // internal
- this.last = {
- field : 'all',
- caption : w2utils.lang('All Fields'),
- logic : 'OR',
- search : '',
- multi : false,
- scrollTop : 0,
- scrollLeft : 0,
- selected : [],
- sortData : null,
- sortCount : 0,
- xhr : null
- }
- this.isIOS = (navigator.userAgent.toLowerCase().indexOf('iphone') != -1 ||
- navigator.userAgent.toLowerCase().indexOf('ipod') != -1 ||
- navigator.userAgent.toLowerCase().indexOf('ipad') != -1) ? true : false;
- $.extend(true, this, options);
- };
- // ====================================================
- // -- Registers as a jQuery plugin
- $.fn.w2grid = function(method) {
- if (typeof method === 'object' || !method ) {
- // check required parameters
- if (!method || typeof method.name == 'undefined') {
- console.log('ERROR: The parameter "name" is required but not supplied in $().w2grid().');
- return;
- }
- if (typeof w2ui[method.name] != 'undefined') {
- console.log('ERROR: The parameter "name" is not unique. There are other objects already created with the same name (obj: '+ method.name +').');
- return;
- }
- if (!w2utils.isAlphaNumeric(method.name)) {
- console.log('ERROR: The parameter "name" has to be alpha-numeric (a-z, 0-9, dash and underscore). ');
- return;
- }
- // remember items
- var columns = method.columns;
- var columnGroups= method.columnGroups;
- var records = method.records;
- var searches = method.searches;
- var searchData = method.searchData;
- var sortData = method.sortData;
- var postData = method.postData;
- var toolbar = method.toolbar;
- // extend items
- var object = new w2grid(method);
- $.extend(object, { postData: {}, records: [], columns: [], searches: [], toolbar: {}, sortData: [], searchData: [], handlers: [] });
- if (object.onExpand != null) object.show.expandColumn = true;
- $.extend(true, object.toolbar, toolbar);
- // reassign variables
- for (var p in columns) object.columns[p] = $.extend({}, columns[p]);
- for (var p in columnGroups) object.columnGroups[p] = $.extend({}, columnGroups[p]);
- for (var p in searches) object.searches[p] = $.extend({}, searches[p]);
- for (var p in searchData) object.searchData[p] = $.extend({}, searchData[p]);
- for (var p in sortData) object.sortData[p] = $.extend({}, sortData[p]);
- for (var p in postData) object.postData[p] = $.extend({}, postData[p]);
- // check if there are records without recid
- for (var r in records) {
- if (records[r].recid == null || typeof records[r].recid == 'undefined') {
- console.log('ERROR: Cannot add records without recid. (obj: '+ object.name +')');
- return;
- }
- object.records[r] = $.extend({}, records[r]);
- }
- if (object.records.length > 0) object.total = object.records.length;
- // add searches
- for (var c in object.columns) {
- var col = object.columns[c];
- if (typeof col.searchable == 'undefined' || object.getSearch(col.field) != null) continue;
- var stype = col.searchable;
- var attr = '';
- if (col.searchable === true) { stype = 'text'; attr = 'size="20"'; }
- object.addSearch({ field: col.field, caption: col.caption, type: stype, attr: attr });
- }
- // init toolbar
- object.initToolbar();
- // render if necessary
- if ($(this).length != 0) {
- object.render($(this)[0]);
- }
- // register new object
- w2ui[object.name] = object;
- return object;
- } else if (typeof $(this).data('w2name') != 'undefined') {
- var obj = w2ui[$(this).data('w2name')];
- obj[method].apply(obj, Array.prototype.slice.call(arguments, 1));
- return this;
- } else {
- console.log('ERROR: Method ' + method + ' does not exist on jQuery.w2grid');
- }
- }
- // ====================================================
- // -- Implementation of core functionality
- w2grid.prototype = {
- add: function (record) {
- if (!$.isArray(record)) record = [record];
- var added = 0;
- for (var o in record) {
- if (record[o].recid == null || typeof record[o].recid == 'undefined') {
- console.log('ERROR: Cannot add record without recid. (obj: '+ this.name +')');
- continue;
- }
- this.records.push(record[o]);
- added++;
- }
- this.total = this.records.length;
- if (this.url == '') {
- this.localSearch();
- this.localSort();
- }
- this.refresh(); // ?? should it be reload?
- return added;
- },
- find: function (obj, returnRecords) {
- if (typeof obj == 'undefined' || obj == null) obj = {};
- var recs = [];
- for (var i=0; i<this.records.length; i++) {
- var match = true;
- for (var o in obj) if (obj[o] != this.records[i][o]) match = false;
- if (match && returnRecords !== true) recs.push(this.records[i].recid);
- if (match && returnRecords === true) recs.push(this.records[i]);
- }
- return recs;
- },
- set: function (recid, record) { // does not delete existing, but overrides on top of it
- var ind = this.get(recid, true);
- var record;
- $.extend(this.records[ind], record);
- // refresh only that record
- var tr = $('#grid_'+ this.name +'_rec_'+ w2utils.escapeId(recid));
- if (tr.length != 0) {
- var line = tr.attr('line');
- var j = 0;
- while (true) {
- var col = this.columns[j];
- if (col.hidden) { j++; if (typeof this.columns[j] == 'undefined') break; else continue; }
- var field = '';
- if (String(col.field).indexOf('.') > -1) {
- var tmp = String(col.field).split('.');
- field = this.records[ind][tmp[0]];
- if (typeof field == 'object' && field != null) {
- field = field[tmp[1]];
- }
- } else {
- field = this.records[ind][col.field];
- }
- if (typeof col.render != 'undefined') {
- if (typeof col.render == 'function') field = col.render.call(this, this.records[ind], ind);
- if (typeof col.render == 'object') field = col.render[this.records[ind][col.field]];
- }
- if (field == null || typeof field == 'undefined') field = '';
- // common render functions
- if (typeof col.render == 'string') {
- switch (col.render.toLowerCase()) {
- case 'url':
- var pos = field.indexOf('/', 8);
- field = '<a href="'+ field +'" target="_blank">'+ field.substr(0, pos) +'</a>';
- break;
- case 'repeat':
- if (ind > 0 && this.records[ind][col.field] == this.records[ind-1][col.field] && this.records[ind][col.field] != '') {
- field = '-- // --';
- }
- break;
- }
- }
- $(tr).find('#grid_'+ this.name +'_cell_'+ line + '_'+ j +' > div').html(field);
- // field
- j++;
- if (typeof this.columns[j] == 'undefined') break;
- }
- }
- },
- get: function (recid, returnIndex) {
- for (var i=0; i<this.records.length; i++) {
- if (this.records[i].recid == recid) {
- if (returnIndex === true) return i; else return this.records[i];
- }
- }
- return null;
- },
- remove: function () {
- var removed = 0;
- for (var a in arguments) {
- for (var r = this.records.length-1; r >= 0; r--) {
- if (this.records[r].recid == arguments[a]) { this.records.splice(r, 1); removed++; }
- }
- }
- if (this.url == '') {
- this.total = this.records.length;
- this.localSearch();
- this.localSort();
- }
- this.refresh();
- return removed;
- },
- addColumn: function (before, column) {
- if (arguments.length == 1) {
- column = before;
- before = this.columns.length;
- } else {
- before = this.getColumn(before, true);
- if (before === null) before = this.columns.length;
- }
- if (!$.isArray(column)) column = [column];
- for (var o in column) {
- this.columns.splice(before, 0, column[o]);
- before++;
- }
- this.initColumnOnOff();
- this.refresh();
- },
- removeColumn: function () {
- var removed = 0;
- for (var a in arguments) {
- for (var r = this.columns.length-1; r >= 0; r--) {
- if (this.columns[r].field == arguments[a]) { this.columns.splice(r, 1); removed++; }
- }
- }
- this.initColumnOnOff();
- this.refresh();
- return removed;
- },
- getColumn: function (field, returnIndex) {
- for (var i=0; i<this.columns.length; i++) {
- if (this.columns[i].field == field) {
- if (returnIndex === true) return i; else return this.columns[i];
- }
- }
- return null;
- },
- showColumn: function () {
- var shown = 0;
- for (var a in arguments) {
- for (var r = this.columns.length-1; r >= 0; r--) {
- if (this.columns[r].field == arguments[a] && this.columns[r].hidden !== false) {
- this.columns[r].hidden = false;
- shown++;
- }
- }
- }
- this.resize();
- return shown;
- },
- hideColumn: function () {
- var hidden = 0;
- for (var a in arguments) {
- for (var r = this.columns.length-1; r >= 0; r--) {
- if (this.columns[r].field == arguments[a] && this.columns[r].hidden !== true) {
- this.columns[r].hidden = true;
- hidden++;
- }
- }
- }
- this.resize();
- return hidden;
- },
- addSearch: function (before, search) {
- if (arguments.length == 1) {
- search = before;
- before = this.searches.length;
- } else {
- before = this.getSearch(before, true);
- if (before === null) before = this.searches.length;
- }
- if (!$.isArray(search)) search = [search];
- for (var o in search) {
- this.searches.splice(before, 0, search[o]);
- before++;
- }
- this.searchClose();
- },
- removeSearch: function () {
- var removed = 0;
- for (var a in arguments) {
- for (var r = this.searches.length-1; r >= 0; r--) {
- if (this.searches[r].field == arguments[a]) { this.searches.splice(r, 1); removed++; }
- }
- }
- this.searchClose();
- return removed;
- },
- getSearch: function (field, returnIndex) {
- for (var i=0; i<this.searches.length; i++) {
- if (this.searches[i].field == field) {
- if (returnIndex === true) return i; else return this.searches[i];
- }
- }
- return null;
- },
- showSearch: function () {
- var shown = 0;
- for (var a in arguments) {
- for (var r = this.searches.length-1; r >= 0; r--) {
- if (this.searches[r].field == arguments[a] && this.searches[r].hidden !== false) {
- this.searches[r].hidden = false;
- shown++;
- }
- }
- }
- this.searchClose();
- return shown;
- },
- hideSearch: function () {
- var hidden = 0;
- for (var a in arguments) {
- for (var r = this.searches.length-1; r >= 0; r--) {
- if (this.searches[r].field == arguments[a] && this.searches[r].hidden !== true) {
- this.searches[r].hidden = true;
- hidden++;
- }
- }
- }
- this.searchClose();
- return hidden;
- },
- getSearchData: function (field) {
- for (var s in this.searchData) {
- if (this.searchData[s].field == field) return this.searchData[s];
- }
- return null;
- },
- clear: function () {
- this.records = [];
- this.total = 0;
- this.refresh();
- },
- localSort: function () {
- var obj = this;
- this.records.sort(function (a, b) {
- // summary records
- if (a.summary && b.summary) {
- if (a.recid > b.recid) return 1;
- if (a.recid <= b.recid) return -1;
- }
- var ret = 0;
- for (var s in obj.sortData) {
- var aa = a[obj.sortData[s].field];
- var bb = b[obj.sortData[s].field];
- if (typeof aa == 'string') aa = $.trim(aa.toLowerCase());
- if (typeof bb == 'string') bb = $.trim(bb.toLowerCase());
- if (aa > bb) ret = (obj.sortData[s].direction == 'asc' ? 1 : -1);
- if (aa < bb) ret = (obj.sortData[s].direction == 'asc' ? -1 : 1);
- if (typeof aa != 'object' && typeof bb == 'object') ret = -1;
- if (typeof bb != 'object' && typeof aa == 'object') ret = 1;
- if (ret != 0) break;
- }
- return ret;
- });
- this.last.sortCount = this.records.length;
- },
- localSearch: function () {
- // local search
- var obj = this;
- this.total = this.records.length;
- // mark all records as shown
- for (var r in this.records) { this.records[r].hidden = false; }
- // hide records that did not match
- if (this.searchData.length > 0) {
- this.total = 0;
- for (var r in this.records) {
- var rec = this.records[r];
- var fl = 0;
- for (var s in this.searches) {
- var search = this.searches[s];
- var sdata = this.getSearchData(search.field);
- if (sdata == null) continue;
- var val1;
- try { val1 = eval('rec.'+ search.field); } catch (e) {}
- val1 = String(val1).toLowerCase();
- if (typeof sdata.value != 'undefined') {
- if (!$.isArray(sdata.value)) {
- var val2 = String(sdata.value).toLowerCase();
- } else {
- var val2 = sdata.value[0];
- var val3 = sdata.value[1];
- }
- }
- switch (sdata.operator) {
- case 'is':
- if (rec[search.field] == sdata.value) fl++; // do not hide record
- if (search.type == 'text' && val1 == val2) fl++;
- if (search.type == 'date') {
- var da = new Date(val1);
- var db = new Date(val2);
- d0 = Date.UTC(da.getFullYear(), da.getMonth(), da.getDate());
- d1 = Date.UTC(db.getFullYear(), db.getMonth(), db.getDate());
- if (d0 == d1) fl++;
- }
- break;
- case 'between':
-