Xataface HTML Reports Module 0.2
HTML Reports Module for Xataface
js/xataface/modules/htmlreports/RelationshipSelector.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 <xatajax.core.js>
00024 //require <xatajax.ui.tk/Component.js>
00025 //require <xataface/modules/htmlreports/__init__.js>
00026 
00027 (function(){
00028         var $ = jQuery;
00029         
00030         var Component = XataJax.ui.tk.Component;
00031         window.xataface.modules.htmlreports.RelationshipSelector = RelationshipSelector;
00032         
00033         function RelationshipSelector(o){
00034                 XataJax.extend(this, new Component(o));
00035                 XataJax.publicAPI(this, {
00036                         setSelectedRelationship: setSelectedRelationship,
00037                         getSelectedRelationship: getSelectedRelationship,
00038                         update: update
00039                 });
00040                 
00041                 this.table = o.table;
00042                 this.el = document.createElement('div');
00043                 
00044                 
00045                 init(this);
00046         
00047         }
00048         
00049         
00057         function init(sb){
00058                 sb.select = document.createElement('select');
00059                 var q = {
00060                         '-table': sb.table,
00061                         '-action': 'htmlreports_schemabrowser_getschema'
00062                 
00063                 };
00064                 
00065                 $.ajax({
00066                         url: DATAFACE_SITE_HREF,
00067                         data: q,
00068                         success: function(res){
00069                                 try {
00070                                         if ( res.code == 200 ){
00071                                                 $.each(res.schema, function(){
00072                                                         if ( this['data-key'] == 'relationships' ){
00073                                                                 var rels = {};
00074                                                                 $.each(this.children, function(){
00075                                                                         rels[this.attr['xf-htmlreports-relationshipname']] = this.data;
00076                                                                 });
00077                                                                 fillSelect(sb, rels);
00078                                                                 sb.trigger('loaded', {
00079                                                                         relationshipSelector: sb
00080                                                                 });
00081                                                         }
00082                                                 });
00083                                         }
00084                                         else if ( res.message ) throw res.message;
00085                                         else throw 'Faild to load fields.  See server log for details.';
00086                                 } catch(e){
00087                                         alert(e);
00088                                 }
00089                         }
00090                                 
00091                 });
00092                 var el = sb.el;
00093                 $(el).append(sb.select);
00094                 
00095                 $(sb.select).change(function(){
00096                         var sel = this;
00097                         sb.trigger('relationshipSelected', {
00098                                 relationshipSelector: sb,
00099                                 relationshipName: $(sel).val(),
00100                                 relationshipLabel: $(sel.options[sel.selectedIndex]).text()
00101                         });
00102                 });
00103         
00104         }
00105         
00106         function fillSelect(sb, options){
00107                 $(sb.select).empty();
00108                 $(sb.select).append($('<option></option').text('Select Relationship'));
00109                 $.each(options, function(key,val){
00110                         $(sb.select).append($('<option></option>').attr('value',key).text(val));
00111                 });     
00112         }
00113         
00114         
00115         function setSelectedRelationship(rel){
00116                 $(this.select).val(rel);
00117         }
00118         
00119         function getSelectedRelationship(){
00120                 return $(this.select).val();
00121         }
00122         
00123         function update(){
00124                 
00125                 this.getSuper(Component).update();
00126                 var el = this.getElement();
00127                 $(el).children().detach();
00128                 $.each(this.getChildComponents(), function(){
00129                         $(el).append(this.getElement());
00130                 });
00131         
00132                 $(el).append(this.el);
00133         }
00134         
00135         
00136         
00137         
00138         
00139 })();
 All Data Structures Files Functions Variables Enumerations