include request info on transport exception, sanity check tag parser results
[ajatus.git] / js / ajatus.preferences.js
blobb0adf8a287627c85013b2d1ff31e3793682d5646
1 /*
2 * This file is part of
4 * Ajatus - Distributed CRM
5 * @requires jQuery v1.2.1
6 *
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
15 (function($){
16 $.ajatus = $.ajatus || {};
18 $.ajatus.preferences = {
19 revision: null,
20 modified: false
23 $.ajatus.preferences.local = {};
24 $.ajatus.preferences.local_defaults = {
25 user: {
26 name: 'Root Ajatus',
27 email: 'root@ajatus.info'
29 replication: {
30 enabled: false,
31 public_key: '',
32 allowed_keys: ''
34 layout: {
35 theme_name: 'default',
36 icon_set: 'default'
38 localization: {
39 language: 'en_GB'
41 backups: {
42 enabled: false,
43 auto: false
45 expansions: {
46 active: []
48 auto_save: {
49 enabled: true,
50 interval: 300
54 $.ajatus.preferences.client = {};
55 $.ajatus.preferences.client_defaults = {
56 debug: false,
57 language: null,
58 server_url: '/',
59 theme_url: 'themes/default/',
60 theme_icons_url: 'themes/default/images/icons/',
61 application_url: '/_utils/ajatus/',
62 application_database_identifier: '',
63 content_types: {},
64 custom_views: [],
65 custom_widgets: [],
66 extensions: [],
67 types: {
68 in_use: [
69 'note', 'contact', 'event', 'expense', 'hour_report'
71 system: [
72 'tag'
75 themes: {
76 available: [
77 'default'
80 developer_tools: false// ,
81 // cookies: {
82 // expire: (24 * 60 * 60) // 24hrs in seconds
83 // }
86 $.ajatus.preferences.loader = function() {
87 this.loaded = false;
89 var on_success = function(data) {
90 this.loaded = true;
92 $.ajatus.events.lock_pool.decrease();
93 $.ajatus.preferences.revision = data._rev;
95 $.ajatus.preferences.local = data.value;
96 $.each($.ajatus.preferences.local_defaults, function(i,n){
97 if (typeof $.ajatus.preferences.local[i] == 'undefined') {
98 $.ajatus.preferences.modified = true;
99 $.ajatus.preferences.local[i] = n;
101 if (typeof n == 'object') {
102 $.each(n, function(xi,xn){
103 if (typeof $.ajatus.preferences.local[i][xi] == 'undefined') {
104 $.ajatus.preferences.modified = true;
105 $.ajatus.preferences.local[i][xi] = xn;
111 return data;
114 this.dc = $.jqCouch.connection('doc', on_success);
116 $.extend($.ajatus.preferences.loader.prototype, {
117 load: function() {
118 $.ajatus.events.lock_pool.increase();
119 var data = this.dc.get($.ajatus.preferences.client.application_database + '/preferences');
120 if (! data._id) {
121 this.loaded = false;
122 $.ajatus.events.lock_pool.decrease();
123 $.ajatus.ajax_error(data.request);
128 $.ajatus.preferences.view = {
129 title: 'Preferences',
130 icon: 'preferences.png',
131 options: {},
132 sections: [
133 'user', 'localization', 'layout', 'replication', 'backups', 'expansions', 'auto_save'
136 tab: {
137 on_click: function(e)
139 $.ajatus.tabs.on_click(e);
140 $.ajatus.preferences.view.render();
144 render: function() {
145 $.ajatus.application_content_area.html('');
146 $.ajatus.layout.body.set_class('preferences');
147 $.ajatus.views.on_change("#view.preferences");
149 $.ajatus.toolbar.show();
151 var form = $('<form name="preferences" id="preferences_form"/>');
152 form.appendTo($.ajatus.application_content_area);
154 var hidden_data = {
155 _id: 'preferences',
156 _rev: $.ajatus.preferences.revision
158 var hidden_fields = function() {
159 return [
160 'input', { type: 'hidden', id: 'preferences_id', name: '_id', value: this._id }, '',
161 'input', { type: 'hidden', id: 'preferences_revision', name: '_rev', value: this._rev }, ''
164 $(form).tplAppend(hidden_data, hidden_fields);
166 var form_actions = $('<div class="form_actions"><input type="submit" name="save" value="' + $.ajatus.i10n.get('Save') + '" /><input type="submit" name="cancel" value="' + $.ajatus.i10n.get('Cancel') + '" /></div>');
168 var sections_holder = $('<div id="preference_sections" />');
169 sections_holder.appendTo(form);
171 $.each($.ajatus.preferences.view.sections, function(i,s){
172 var section_holder = $('<div class="section" id="section_' + s + '" />');
173 section_holder.appendTo(sections_holder);
175 var form_items = $.ajatus.preferences.view.generate_section_form(s);
177 if (form_items) {
178 var normalized_title = s.toString().replace('_', ' ');
179 var title = $('<h3 />').html($.ajatus.i10n.get(normalized_title));
180 title.appendTo(section_holder);
182 form_items.appendTo(section_holder);
186 form_actions.appendTo(form);
188 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Save'), 'save.png', function(){
189 $('#preferences_form input[@type=submit][name*=save]').trigger("click", []);
191 $.ajatus.toolbar.add_item($.ajatus.i10n.get('Cancel'), 'cancel.png', function(){
192 $('#preferences_form input[@type=submit][name*=cancel]').trigger("click", []);
195 $.ajatus.forms.register.custom(form, '$.ajatus.preferences.view.process_form');
198 process_form: function(form_data, form) {
199 // $.ajatus.debug('Process preferences');
200 // console.log(form_data);
202 var doc = {
203 _id: '',
204 _rev: '',
205 value: {}
207 var form_values = {};
208 var form_id = '';
210 $.each(form_data, function(i,row){
211 if (row.name.toString().match(/__(.*?)/)) {
212 return;
214 if ( row.name == '_id'
215 && ( row.value != undefined
216 && row.value != ''))
218 doc['_id'] = String(row.value);
219 form_id = doc['_id'];
221 else if( row.name == '_rev'
222 && ( typeof row.value != "undefined"
223 && row.value != ''))
225 doc['_rev'] = String(row.value);
226 } else {
227 if (row.name != 'submit') {
228 if (row.name == '_type') {
230 else if ( row.name.substr(0,6) != "widget"
231 && row.name.substr(0,8) != "metadata")
233 var item = {};
234 var widget = {};
235 var prev_val = false;
236 var name_parts = [];
237 var name_parts_count = 0;
238 if (row.name.toString().match(/;/g)) {
239 name_parts = row.name.toString().split(";");
240 name_parts_count = name_parts.length;
243 $.each(form_data, function(x,r){
244 if (r.name == 'widget['+row.name+':name]') {
245 widget['name'] = r.value;
246 } else if (r.name == 'widget['+row.name+':config]') {
247 widget['config'] = $.ajatus.converter.parseJSON(r.value);
248 } else if (r.name == 'widget['+row.name+':prev_val]') {
249 prev_val = $.ajatus.converter.parseJSON(r.value);
253 var wdgt = new $.ajatus.widget(widget['name'], widget['config']);
254 item['val'] = wdgt.value_on_save(row.value, prev_val);
255 item['widget'] = widget;
257 if (name_parts_count > 0) {
258 var prevs = [];
259 for (var i=0; i < name_parts_count; i++) {
260 var arr_keys = false;
261 var key_prefix = '';
263 if (name_parts[i].match(/\[/g)) {
264 var arr_keys = name_parts[i].split('[');
266 name_parts[i] = name_parts[i].replace(/\[/g, '"][');
267 var key = '["'+name_parts[i];
268 } else {
269 var key = "['"+name_parts[i]+"']";
272 if (prevs.length > 0) {
273 $.each(prevs, function(pi, pk){
274 key_prefix = "['" + pk + "']" + key_prefix;
276 key = key_prefix + key;
279 if (arr_keys) {
280 var tmp_key = key_prefix + "['" + arr_keys[0] + "']";
281 if (typeof eval("form_values"+tmp_key) == 'undefined') {
282 eval("form_values"+tmp_key+"=[];");
286 var multiple = false;
287 if (typeof eval("form_values"+key) == 'undefined') {
288 if (key_prefix != '') {
289 eval("form_values"+key+"=new Array();");
290 } else {
291 eval("form_values"+key+"={};");
293 } else {
294 multiple = true;
297 prevs.push(name_parts[i]);
298 if (i == name_parts_count-1) {
299 if (typeof item['val'] == 'undefined') {
300 return;
302 if (multiple) {
303 if (typeof item == 'object') {
304 // eval("form_values"+key+".push('"+item.toSource()+"');");
305 if (item.widget.name == 'boolean') {
306 eval("form_values"+key+"="+item.val+";");
307 } else {
308 eval("form_values"+key+".push('"+item.val+"');");
310 } else {
311 eval("form_values"+key+".push('"+item+"');");
313 } else {
314 if (typeof item == 'object') {
315 eval("form_values"+key+"='"+item.val+"';");
316 } else {
317 eval("form_values"+key+"='"+item.val+"';");
322 } else {
323 form_values[row.name] = item;
326 else if (row.name.substr(0,8) == "metadata")
328 if (! form_values['metadata']) {
329 form_values['metadata'] = {};
332 var re = /\bmetadata\[([a-z]+)\b/;
333 var results = re.exec(row.name);
334 var key = results[1];
336 form_values['metadata'][key] = {
337 val: row.value
343 doc['value'] = form_values;
345 doc = new $.ajatus.document(doc);
347 $.ajatus.preferences.view.save(doc.value);
350 save: function(preferences) {
351 var saved = false;
353 var on_success = function(data) {
354 saved = true;
355 $.ajatus.preferences.revision = data.rev;
356 $.ajatus.events.named_lock_pool.decrease('unsaved');
357 window.location.reload();
359 return data;
362 prefs_doc = new $.ajatus.document({
363 _id: 'preferences',
364 _rev: $.ajatus.preferences.revision,
365 value: preferences
367 prefs_doc.value._type = 'preferences';
369 var new_metadata = {
370 revised: $.ajatus.formatter.date.js_to_iso8601(new Date()),
371 revisor: $.ajatus.preferences.local.user.email
373 prefs_doc = $.ajatus.document.modify_metadata(prefs_doc, new_metadata);
375 var prefs = {
376 metadata: prefs_doc.value.metadata
378 $.each($.ajatus.preferences.local_defaults, function(k,data){
379 if (typeof prefs[k] == 'undefined') {
380 prefs[k] = {};
383 if (typeof data == 'object') {
384 $.each(data, function(sk, sdata){
385 if (typeof prefs_doc.value[k] == 'undefined') {
386 prefs[k][sk] = sdata;
387 } else {
388 if (typeof prefs_doc.value[k][sk] == 'undefined') {
389 prefs[k][sk] = sdata;
390 } else {
391 if (typeof prefs_doc.value[k][sk]['val'] == 'undefined') {
392 prefs[k][sk] = prefs_doc.value[k][sk];
393 } else {
394 prefs[k][sk] = prefs_doc.value[k][sk].val;
399 } else {
400 if (typeof prefs_doc.value[k] == 'undefined') {
401 prefs[k] = data;
402 } else {
403 if (typeof prefs_doc.value[k]['val'] == 'undefined') {
404 prefs[k] = prefs_doc.value[k];
405 } else {
406 prefs[k] = prefs_doc.value[k].val;
412 prefs_doc.value = prefs;
414 $.jqCouch.connection('doc', on_success).save($.ajatus.preferences.client.application_database, prefs_doc);
416 return saved;
419 generate_section_form: function(section) {
420 if (typeof $.ajatus.preferences.local[section] == 'undefined') {
421 return false;
424 var items = false;
425 var section_data = $.ajatus.preferences.local[section];
427 function auto_gen(section, data) {
428 var rows = $('<ul/>');
430 $.each(data, function(i,s){
431 // console.log("i: "+i+" s: ");
432 // console.log(s);
433 var s_type = typeof(s);
434 // console.log('type: '+s_type);
435 var wdgt = null;
436 var normalized_name = i.toString().replace('_', ' ');
438 switch (s_type) {
439 case 'boolean':
440 wdgt = $.ajatus.widget('boolean');
441 break;
442 case 'number':
443 wdgt = $.ajatus.widget('integer');
444 break;
445 case 'string':
446 default:
447 wdgt = $.ajatus.widget('text');
450 rows.createAppend(
451 'li', { id: '_setting_'+section+'_'+i, className: 'row' }, [
452 'label', { id: '_setting_'+section+'_'+i+'_label' }, $.ajatus.i10n.get(normalized_name),
453 'div', { id: '_setting_'+section+'_'+i+'_widget', className: wdgt.name }, wdgt.get_edit_tpl(section+';'+i, {val: s}),
454 'br', { className: 'clear_fix' }, ''
457 wdgt.init($('#_setting_'+i+'_widget',rows), true);
460 $('li:first', rows).addClass('first');
461 $('li:last', rows).addClass('last');
463 return rows;
466 function gen_localization(data) {
467 var rows = $('<ul/>');
468 var langs = $.ajatus.i10n.get_available_langs();
469 var lang_opts = {};
471 $.each(langs, function(i,l) {
472 lang_opts[i] = l;
475 wdgt = $.ajatus.widget('selection', {
476 items: lang_opts
478 rows.createAppend(
479 'li', { id: '_setting_localization_language', className: 'row' }, [
480 'label', { id: '_setting_localization_language_label' }, $.ajatus.i10n.get('Language'),
481 'div', { id: '_setting_localization_language_widget', className: wdgt.name }, wdgt.get_edit_tpl('localization;language', {val: data.language}),
482 'br', { className: 'clear_fix' }, ''
485 wdgt.init($('#_setting_localization_language_widget',rows), true);
487 return rows;
490 function gen_layout(data) {
491 var rows = $('<ul/>');
492 var themes = $.ajatus.preferences.client.themes.available;
493 var theme_opts = {};
495 $.each(themes, function(i,l) {
496 theme_opts[i] = $.ajatus.i10n.get(l);
499 wdgt = $.ajatus.widget('selection', {
500 items: theme_opts
502 rows.createAppend(
503 'li', { id: '_setting_layout_theme_name', className: 'row' }, [
504 'label', { id: '_setting_layout_theme_name_label' }, $.ajatus.i10n.get('Theme'),
505 'div', { id: '_setting_layout_theme_name_widget', className: wdgt.name }, wdgt.get_edit_tpl('layout;theme_name', {val: data.theme_name}),
506 'br', { className: 'clear_fix' }, ''
509 wdgt.init($('#_setting_layout_theme_name_widget', rows), true);
511 return rows;
514 function gen_autosave(data) {
515 var rows = $('<ul/>');
516 var section = 'auto_save';
518 $.each(data, function(i,s){
519 // console.log("i: "+i+" s: ");
520 // console.log(s);
521 var s_type = typeof(s);
522 // console.log('type: '+s_type);
523 var wdgt = null;
524 var normalized_name = i.toString().replace('_', ' ');
526 switch (i) {
527 case 'enabled':
528 wdgt = $.ajatus.widget('boolean');
529 break;
530 case 'interval':
531 wdgt = $.ajatus.widget('integer',{
532 value_suffix: 's'
534 break;
535 default:
536 wdgt = $.ajatus.widget('text');
539 rows.createAppend(
540 'li', { id: '_setting_'+section+'_'+i, className: 'row' }, [
541 'label', { id: '_setting_'+section+'_'+i+'_label' }, $.ajatus.i10n.get(normalized_name),
542 'div', { id: '_setting_'+section+'_'+i+'_widget', className: wdgt.name }, wdgt.get_edit_tpl(section+';'+i, {val: s}),
543 'br', { className: 'clear_fix' }, ''
546 wdgt.init($('#_setting_'+i+'_widget',rows), true);
549 $('li:first', rows).addClass('first');
550 $('li:last', rows).addClass('last');
552 return rows;
555 // Example how to handle arrays with checkboxes
556 // function gen_types(data) {
557 // var rows = $('<ul />');
559 // $.each($.ajatus.types.available, function(key,type){
560 // // console.log("key: "+key);
561 // // console.log(type);
563 // var selected = false;
564 // $.each(data.active, function(i,t){
565 // if (t == key) {
566 // selected = true;
567 // }
568 // });
570 // var widget = new $.ajatus.widget('boolean', {
571 // value: key
572 // });
574 // rows.createAppend(
575 // 'li', { id: '_setting_types_active_'+key, className: 'row' }, [
576 // 'label', { id: '_setting_types_active_'+key+'_label' }, $.ajatus.i10n.get(type.title),
577 // 'div', { id: '_setting_types_active_'+key+'_widget', className: widget.name }, widget.get_edit_tpl('types;active', {val: (selected ? key : '')}), //["'+key+'"]
578 // 'br', { className: 'clear_fix' }, ''
579 // ]
580 // );
581 // widget.init($('#_setting_types_active_'+key+'_widget',rows), true);
582 // });
584 // return rows;
585 // }
587 switch (section) {
588 case 'localization':
589 items = gen_localization(section_data);
590 break;
591 case 'layout':
592 items = gen_layout(section_data);
593 break;
594 case 'auto_save':
595 items = gen_autosave(section_data);
596 break;
597 case 'replication':
598 case 'backups':
599 case 'expansions':
600 break;
601 default:
602 items = auto_gen(section, section_data);
605 return items;
609 })(jQuery);