Xataface HTML Reports Module 0.2
HTML Reports Module for Xataface
js/xataface/modules/htmlreports/schemabrowser.js
Go to the documentation of this file.
00001 /*
00002  * Xataface htmlreports Module
00003  * Copyright (C) 2011  Steve Hannah <steve@weblite.ca>
00004  * 
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  * 
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * 
00015  * You should have received a copy of the GNU Library General Public
00016  * License along with this library; if not, write to the
00017  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
00018  * Boston, MA  02110-1301, USA.
00019  *
00020  */
00021  
00022 //require <jquery.packed.js>
00023 //require <jquery.jstree.js>
00024 //require <xatajax.core.js>
00025 //require <xatajax.ui.tk/ToolBar.js>
00026 //require <xatajax.ui.tk/Button.js>
00027 //require <xatajax.ui.tk/ButtonGroup.js>
00028 //require-css <xataface/modules/htmlreports/schemabrowser.css>
00029 //require <xataface/modules/htmlreports/__init__.js>
00030 (function(){
00031         
00032         var $ = jQuery;
00033         var ToolBar = XataJax.ui.tk.ToolBar;
00034         var Button = XataJax.ui.tk.Button;
00035         var ButtonGroup = XataJax.ui.tk.ButtonGroup;
00036         var Component = XataJax.ui.tk.Component;
00037         
00038         
00039         var htmlreports = window.xataface.modules.htmlreports;
00040         htmlreports.SchemaBrowser = SchemaBrowser;
00041         
00042         // We need to set the themes directory for jstree
00043         $.jstree._themes = XATAFACE_MODULES_HTMLREPORTS_URL+'/css/jstree/themes/';
00044         
00045         
00046         
00083         function SchemaBrowser(o){
00084                 XataJax.extend(this, new Component(o));
00085                 XataJax.publicAPI(this, {
00086                         update:update
00087                 });
00088                 //this.setLayout(new BorderLayout());
00089                 $.extend(this, o);
00090                 if ( !this.el ) this.el = document.createElement('div');
00091                 
00092 
00093                 
00094                 
00095                 initToolBar(this);
00096                 
00097                 
00098                 init(this);
00099                 
00100                 
00101                 
00102         
00103         }
00104         
00110         function init(sb){
00111                 $(sb.el).html(sb.template);
00112                 initTree(sb);
00113                 //$('xf-schemabrowser-actions', sb.el).append(sb.toolbar.getElement());
00114                 
00115                 
00116         }
00117         
00118         
00125         function initToolBar(sb){
00126         
00127                 sb.toolbar = new ToolBar({});
00128                 
00129                 var g = new ButtonGroup();
00130                 sb.navButtonGroup = g;
00131                 var b = new Button({});
00132                 b.setLabel('Prev');
00133                 b.setIcon('triangle-1-w');
00134                 b.setIconStyle('solo');
00135                 g.add(b);
00136                 //b.setDisabled(false);
00137                 $(b.getElement()).click( function(){
00138                         var cursor = parseInt(sb.query['-cursor']||0);
00139                         sb.query['-cursor'] = cursor-1;
00140                         if ( cursor < 0 ){
00141                                 cursor = 0;
00142                         }
00143                         refreshTreePreview(sb, sb.el);
00144                 });
00145                 
00146                 
00147                 
00148                 
00149                 
00150                 sb.prevButton = b;
00151                 
00152                 b = new Button();
00153                 b.setLabel('X/Y');
00154                 
00155                 g.add(b);
00156                 sb.statusButton = b;
00157                 
00158                 
00159                 b = new Button({});
00160                 b.setLabel('Next');
00161                 b.setIcon('triangle-1-e');
00162                 b.setIconStyle('solo');
00163                 
00164                 $(b.getElement()).click( function(){
00165                         var cursor = parseInt(sb.query['-cursor']||0);
00166                         sb.query['-cursor'] = cursor+1;
00167                         if ( cursor < 0 ){
00168                                 cursor = 0;
00169                         }
00170                         refreshTreePreview(sb, sb.el);
00171                 });
00172                 
00173                 
00174                 
00175                 g.add(b);
00176                 sb.toolbar.add(g);
00177                 //sb.toolbar.update();
00178                 sb.add(sb.toolbar);
00179                 
00180                 
00181                 
00182                 
00183                 
00184         }
00185         
00193         function initTree(sb){
00194         
00195                 var q = {
00196                         '-table': sb.query['-table'],
00197                         '-action': 'htmlreports_schemabrowser_getschema'
00198                 
00199                 };
00200                 
00201                 $(sb.el)
00202                         .bind('loaded.jstree', function(event, data){
00203                                 // Now let's load the previews for it
00204                                 
00205                                 initTreePreview(sb, this);
00206                                 initTreeEvents(sb, this);
00207                         
00208                         })
00209                         .jstree({
00210                         'plugins': ['themes','json_data'],
00211                         'json_data': {
00212                                 'ajax': {
00213                                         url: DATAFACE_SITE_HREF,
00214                                         data: q,
00215                                         success: function(res){
00216                                                 try {
00217                                                         if ( res.code == 200 ) return res.schema;
00218                                                         else if ( res.message ) throw res.message;
00219                                                         else throw 'Faild to load fields.  See server log for details.';
00220                                                 } catch(e){
00221                                                         alert(e);
00222                                                 }
00223                                         }
00224                                 }
00225                         }
00226                 });
00227                 
00228                 
00229         }
00230         
00237         function initTreeEvents(sb, el){
00238         
00239                 $('li a').click(function(event){
00240                         var a = this;
00241                         sb.trigger('nodeClicked', {
00242                                 sourceEvent: event,
00243                                 sourceElement: a,
00244                                 sourceBrowser: sb
00245                         });
00246                         return false;
00247                 });
00248         
00249         
00250                 $('li[xf-htmlreports-macro] a').click(function(event){
00251                         var a = this;
00252                         sb.trigger('fieldClicked', {
00253                                 sourceEvent: event,
00254                                 sourceElement: a,
00255                                 sourceBrowser: sb,
00256                                 fieldName: $(a).parent('li').attr('xf-htmlreports-fieldname'),
00257                                 macro: $(a).parent('li').attr('xf-htmlreports-macro')
00258                         });
00259                         return false;
00260                 });
00261         }
00262         
00272         function initTreePreview(sb, el){
00273         
00274                 $('li[xf-htmlreports-macro]', el).append('<span class="xf-preview">...</span>');
00275                 refreshTreePreview(sb,el);
00276         }
00277         
00278         
00291         function refreshTreePreview(sb, el){
00292         
00293                 var url = DATAFACE_SITE_HREF;
00294                 var q = {
00295                         '-table': sb.query['-table'],
00296                         '-action': 'htmlreports_schemabrowser_preview_row',
00297                         '-cursor': sb.query['-cursor'] || 0
00298                 };
00299                 $.get(url, q, function(res){
00300                         try {
00301                                 if ( res.code == 200 ){
00302                                         var values = res.values;
00303                                         sb.previewValues = values;
00304                                         $('li[xf-htmlreports-macro]', el).each(function(){
00305                                                 var macro = $(this).attr('xf-htmlreports-macro');
00306                                                 if ( typeof(values[macro]) != 'undefined' ){
00307                                                         $('span.xf-preview', this).html(createPreview(values[macro]));
00308                                                 } else {
00309                                                         $('span.xf-preview', this).text('');
00310                                                 }
00311                                         });
00312                                         sb.statusButton.setLabel("Now Showing Record #"+(parseInt(sb.query['-cursor']||0)+1));
00313                                 } else if ( res.message ){
00314                                         throw res.message;
00315                                 } else {
00316                                         throw 'Failed to load preview.  See server error log for details.';
00317                                 }
00318                         } catch (e){
00319                                 alert(e);
00320                         }
00321                 });
00322         }
00323         
00333         function createPreview(val){
00334                 if ( val == null ) return '';
00335                 val = (''+val).replace(/<[^>]+>/, '');
00336                 var sval = val.substring(0,20);
00337                 if ( sval.length < val.length ) sval +='...';
00338                 return sval;
00339         }
00340         
00341         
00347         function update(){
00348         
00349 
00350                 this.getSuper(Component).update();
00351                 var el = this.getElement();
00352                 $(el).html('');
00353                 $.each(this.getChildComponents(), function(){
00354                         $(el).append(this.getElement());
00355                 });
00356         
00357                 $(el).append(this.el);
00358                 
00359                 
00360                 
00361         }
00362         
00366         SchemaBrowser.prototype = {
00367         
00368                 template:  @@(xataface/modules/htmlreports/schemabrowser/template.html),
00369                 query: null,
00370                 el: null,
00371                 toolbar: null
00372         };
00373         
00407 })();
 All Data Structures Files Functions Variables Enumerations