4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
7 * Copyright (c) 2007 Jerry Jalava <jerry.jalava@gmail.com>
8 * Copyright (c) 2007 Nemein Oy <http://nemein.com>
9 * Website: http://ajatus.info
10 * Licensed under the GPL license
11 * http://www.gnu.org/licenses/gpl.html
16 $.ajatus = $.ajatus || {};
21 $.ajatus.utils.to_boolean = function(value) {
22 if (typeof value == 'boolean') {
26 if (typeof value == 'string') {
27 if (value === 'true') {
29 } else if (value === '1') {
35 if (typeof value == 'number') {
46 * Renders pretty printed version from given value
47 * Original pretty_print function by Damien Katz <damien_katz@yahoo.com>
48 * Modified to work with Ajatus by Jerry Jalava <jerry.jalava@gmail.com>
49 * @param {Mixed} val Value to render
50 * @param {Number} indent Current indent level (Default: 4)
51 * @param {String} linesep Line break to be used (Default: "\n")
52 * @param {Number} depth Current line depth (Default: 1)
53 * @returns Pretty printed value
56 $.ajatus.utils.pretty_print = function(val, indent, linesep, depth) {
57 var indent = typeof indent != 'undefined' ? indent : 4;
58 var linesep = typeof linesep != 'undefined' ? linesep : "\n";
59 var depth = typeof depth != 'undefined' ? depth : 1;
61 var propsep = linesep.length ? "," + linesep : ", ";
65 for (var i = 0; i < indent * depth; i++) {
74 return $.ajatus.converter.toJSON(val);
79 if (val.constructor == Date) {
80 return $.ajatus.converter.toJSON(val);
84 if (val.constructor == Array) {
86 for (var index = 0; index < val.length; index++) {
87 buf.push(index > 0 ? propsep : linesep);
89 tab, $.ajatus.utils.pretty_print(val[index], indent, linesep, depth + 1)
94 buf.push(linesep, tab.substr(indent))
101 for (var key in val) {
102 if (! val.hasOwnProperty(key)) {
106 buf.push(index > 0 ? propsep : linesep);
108 tab, $.ajatus.converter.toJSON(key), ": ",
109 $.ajatus.utils.pretty_print(val[key], indent, linesep, depth + 1)
115 buf.push(linesep, tab.substr(indent));
126 $.ajatus.utils.base64 = {
129 'A','B','C','D','E','F','G','H',
130 'I','J','K','L','M','N','O','P',
131 'Q','R','S','T','U','V','W','X',
132 'Y','Z','a','b','c','d','e','f',
133 'g','h','i','j','k','l','m','n',
134 'o','p','q','r','s','t','u','v',
135 'w','x','y','z','0','1','2','3',
136 '4','5','6','7','8','9','+','/'
139 reverseBase64Chars: null,
141 encode: function(value) {
145 function setBase64Str(str) {
152 var inBuffer = new Array(3);
155 while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT) {
156 inBuffer[1] = readBase64();
157 inBuffer[2] = readBase64();
158 result += ($.ajatus.utils.base64.base64Chars[ inBuffer[0] >> 2 ]);
159 if (inBuffer[1] != END_OF_INPUT) {
160 result += ($.ajatus.utils.base64.base64Chars[((inBuffer[0] << 4) & 0x30) | (inBuffer[1] >> 4)]);
161 if (inBuffer[2] != END_OF_INPUT) {
162 result += ($.ajatus.utils.base64.base64Chars[((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
163 result += ($.ajatus.utils.base64.base64Chars[inBuffer[2] & 0x3F]);
165 result += ($.ajatus.utils.base64.base64Chars[((inBuffer[1] << 2) & 0x3c)]);
170 result += ($.ajatus.utils.base64.base64Chars[((inBuffer[0] << 4) & 0x30)]);
176 if (lineCount >= 76) {
184 decode: function(value) {
188 if ($.ajatus.utils.base64.reverseBase64Chars == null) {
189 for (var i=0; i < $.ajatus.utils.base64.base64Chars.length; i++) {
190 $.ajatus.utils.base64.reverseBase64Chars[$.ajatus.utils.base64.base64Chars[i]] = i;
194 function setBase64Str(str) {
199 function readReverseBase64() {
205 if (base64Count >= base64Str.length) {
208 var nextCharacter = base64Str.charAt(base64Count);
210 if ($.ajatus.utils.base64.reverseBase64Chars[nextCharacter]){
211 return $.ajatus.utils.base64.reverseBase64Chars[nextCharacter];
213 if (nextCharacter == 'A') {
222 if (n.length == 1) n="0"+n;
229 var inBuffer = new Array(4);
231 while (!done && (inBuffer[0] = readReverseBase64()) != END_OF_INPUT
232 && (inBuffer[1] = readReverseBase64()) != END_OF_INPUT)
234 inBuffer[2] = readReverseBase64();
235 inBuffer[3] = readReverseBase64();
236 result += ntos((((inBuffer[0] << 2) & 0xff)| inBuffer[1] >> 4));
237 if (inBuffer[2] != END_OF_INPUT){
238 result += ntos((((inBuffer[1] << 4) & 0xff)| inBuffer[2] >> 2));
239 if (inBuffer[3] != END_OF_INPUT){
240 result += ntos((((inBuffer[2] << 6) & 0xff) | inBuffer[3]));
255 * UTF-8 data encode / decode
256 * http://www.webtoolkit.info/
258 * @returns Encoded/Decoded string
261 $.ajatus.utils.utf8 = {
262 encode: function(string) {
263 string = string.replace(/\r\n/g,"\n");
266 for (var n = 0; n < string.length; n++) {
267 var c = string.charCodeAt(n);
270 utftext += String.fromCharCode(c);
271 } else if ( (c > 127)
274 utftext += String.fromCharCode((c >> 6) | 192);
275 utftext += String.fromCharCode((c & 63) | 128);
277 utftext += String.fromCharCode((c >> 12) | 224);
278 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
279 utftext += String.fromCharCode((c & 63) | 128);
285 decode: function(utftext) {
290 while ( i < utftext.length ) {
291 c = utftext.charCodeAt(i);
294 string += String.fromCharCode(c);
296 } else if ( (c > 191)
299 c2 = utftext.charCodeAt(i+1);
300 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
303 c2 = utftext.charCodeAt(i+1);
304 c3 = utftext.charCodeAt(i+2);
305 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
314 $.ajatus.utils.array = {
315 has_match: function(needles, haystack) {
316 if ( typeof haystack != 'object'
317 || haystack.length <= 0)
322 if (typeof needles == 'object') {
324 $.each(needles, function(i,needle){
325 if ($.inArray(needle, haystack) != -1) {
328 // if (typeof(haystack[needle]) != 'undefined') {
333 } else if (typeof needles == 'string') {
335 if ($.inArray(needles, haystack) != -1) {
345 $.ajatus.utils.object = {
346 clone: function(obj) {
347 if(obj == null || typeof(obj) != 'object') {
351 for(var key in obj) {
352 temp[key] = $.ajatus.utils.object.clone(obj[key]);
359 // Put focus on given form element (0 == first) (skips hidden fields)
360 $.fn.set_focus_on = function(eid) {
361 var elem = $('input:visible:not(:hidden)', this).get(eid);
362 var select = $('select:visible', this).get(eid);
364 if (select && elem) {
365 if (select.offsetTop < elem.offsetTop) {
370 var textarea = $('textarea:visible', this).get(eid);
371 if (textarea && elem) {
372 if (textarea.offsetTop < elem.offsetTop) {
384 $.ajatus.utils.pause = function(ms) {
385 var date = new Date();
388 do { currDate = new Date(); }
389 while(currDate - date < ms);
392 $.ajatus.utils.generate_id = function() {
393 var random_key = Math.floor(Math.random()*4013);
394 return (10016486 + (random_key * 22423));
397 $.ajatus.utils.get_url_arg = function(name) {
400 name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
401 var exp = "[\\?&]"+name+"=([^&#]*)";
402 var regex = new RegExp( exp );
403 var results = regex.exec(window.location.href);
404 if (results != null) {
411 $.ajatus.utils.load_script = function(url, callback, cb_args) {
412 $('head').append('<script type="text/javascript" charset="utf-8" src="'+url+'"></script>');
413 if (typeof callback == 'string') {
414 if ( typeof cb_args == 'undefined'
415 || typeof cb_args != 'object')
420 setTimeout('eval("var fn = eval('+callback+'); fn.apply(fn, [\''+cb_args.join("','")+'\']);")', 300);
424 $.ajatus.utils.doc_generator = function(prefix, count, type, data, value_tpl) {
430 if (typeof value_tpl != 'undefined') {
431 doc.value = value_tpl;
433 if (typeof type == 'undefined') {
434 var type = $.ajatus.preferences.client.content_types['note'];
436 if (typeof data == 'undefined') {
441 $.each(type.original_schema, function(i,n){
450 if (typeof prefix == 'undefined') {
451 var prefix = 'ajatus';
453 if (typeof count == 'undefined') {
457 function prepare_title(title, id) {
458 var full_title = title + " ";
459 full_title += (id).toString();
463 for (var i=0; i<count; i++) {
464 var new_doc = new $.ajatus.document(doc);
466 if ( typeof data.title == 'undefined'
467 && typeof new_doc.value['title'] != 'undefined')
469 new_doc.value['title'].val = $.ajatus.i10n.get("Generated doc title (%s)", [prefix]) + " " + i;
472 $.each(data, function(x,n){
473 if (typeof new_doc.value[x] != 'undefined') {
474 new_doc.value[x].val = n;
478 var now = $.ajatus.formatter.date.js_to_iso8601(new Date());
482 creator: $.ajatus.preferences.local.user.email,
484 revisor: $.ajatus.preferences.local.user.email
487 new_doc = $.ajatus.document.modify_metadata(new_doc, new_metadata);
489 new_doc._id = prefix + "_gen_doc_" + (i).toString();
490 docs.push($.ajatus.utils.object.clone(new_doc));
497 * Following functions are taken from the jquery form plugin.
498 * Plugin can be found from http://www.malsup.com/jquery/form/
500 $.fieldValue = function(el, successful) {
501 var n = el.name, t = el.type, tag = el.tagName.toLowerCase();
502 if (typeof successful == 'undefined') successful = true;
504 if (successful && (!n || el.disabled || t == 'reset' || t == 'button' ||
505 (t == 'checkbox' || t == 'radio') && !el.checked ||
506 (t == 'submit' || t == 'image') && el.form && el.form.clk != el ||
507 tag == 'select' && el.selectedIndex == -1))
510 if (tag == 'select') {
511 var index = el.selectedIndex;
512 if (index < 0) return null;
513 var a = [], ops = el.options;
514 var one = (t == 'select-one');
515 var max = (one ? index+1 : ops.length);
516 for(var i=(one ? index : 0); i < max; i++) {
519 // extra pain for IE...
520 var v = $.browser.msie && !(op.attributes['value'].specified) ? op.text : op.value;
529 $.fn.formToArray = function(semantic) {
531 if (this.length == 0) return a;
534 var els = semantic ? form.getElementsByTagName('*') : form.elements;
536 for(var i=0, max=els.length; i < max; i++) {
541 if (semantic && form.clk && el.type == "image") {
542 // handle image inputs on the fly when semantic == true
543 if(!el.disabled && form.clk == el)
544 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
548 var v = $.fieldValue(el, true);
549 if (v && v.constructor == Array) {
550 for(var j=0, jmax=v.length; j < jmax; j++)
551 a.push({name: n, value: v[j]});
553 else if (v !== null && typeof v != 'undefined')
554 a.push({name: n, value: v});
557 if (!semantic && form.clk) {
558 // input type=='image' are not found in elements array! handle them here
559 var inputs = form.getElementsByTagName("input");
560 for(var i=0, max=inputs.length; i < max; i++) {
561 var input = inputs[i];
563 if(n && !input.disabled && input.type == "image" && form.clk == input)
564 a.push({name: n+'.x', value: form.clk_x}, {name: n+'.y', value: form.clk_y});
570 * Form plugin functions end