Skip to content

Commit

Permalink
Adds a demonstration. Added custom function to display an element.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericabouaf committed Oct 17, 2007
1 parent 2169a2c commit 47e1b0e
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 9 deletions.
47 changes: 47 additions & 0 deletions gim-demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<title>GearsInMotion v0.2 demo</title>

<!--CSS file (default YUI Sam Skin) -->
<link type="text/css" rel="stylesheet" href="http://yui.yahooapis.com/2.3.1/build/datatable/assets/skins/sam/datatable.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.3.1/build/container/assets/skins/sam/container.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.3.1/build/reset/reset-min.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.3.1/build/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.3.1/build/grids/grids-min.css" />
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.3.0/build/button/assets/button.css">

<!-- YUI Dependencies -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.3.1/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.3.1/build/element/element-beta-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.3.1/build/datasource/datasource-beta-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.3.1/build/dragdrop/dragdrop-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.3.1/build/container/container-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.3.1/build/button/button-beta-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.3.1/build/menu/menu-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.3.1/build/datatable/datatable-beta-min.js"></script>

<!-- Google gears -->
<script type="text/javascript" src="lib/gears_init.js"></script>

<!-- GearsInMotion -->
<link rel="stylesheet" type="text/css" href="css/gearsinmotion.css"/>
<script type="text/javascript" src="js/helpers.js"></script>
<script type="text/javascript" src="js/gearsinmotion.js"></script>
<script type="text/javascript" src="js/sql.js"></script>
<script type="text/javascript" src="js/gim_datatable_by_sql.js"></script>

<!-- THE DEMO IS IN THIS FILE : -->
<script type="text/javascript" src="js/demo.js"></script>

<script>
if (!window.google || !google.gears) {
window.location.href = "http://gears.google.com/?action=install&return="+window.location.href;
}
</script>

</head>
<body style="background-color: #eee; overflow: hidden;" class="yui-skin-sam">
<button id="showGIM" onclick="GIM.toggle();">show GIM</button>
</body>
</html>
81 changes: 81 additions & 0 deletions js/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@

var struct = [

{
name: "countries",
fields: {
id: "int",
name: "varchar(255)",
supporters: "int"
},
elements: [
{id: 1, name: "France", supporters: 64102000},
{id: 2, name: "South Africa", supporters: 47850700},
{id: 3, name: "Argentina", supporters: 39921833},
{id: 4, name: "England", supporters: 49138831}
]
},
{
name: "clubs",
fields: {
id: "int",
name: "varchar(255)",
country_id: "int"
},
elements: [
{id: 1, name: "Sale", country_id: 4},
{id: 2, name: "Natal Sharks", country_id: 2},
{id: 3, name: "Toulouse", country_id: 1},
{id: 4, name: "Hindu Club", country_id: 3},
{id: 5, name: "Stade Francais", country_id: 1},
{id: 6, name: "Newcastle", country_id: 4},
{id: 7, name: "Blue Bulls", country_id: 2}
]
},
{
name: "players",
fields: {
id: "int",
name: "varchar(255)",
picture: "varchar(255)",
country_id: "int"
},
elements: [
{id: 1, name: "Sebastien Chabal", country_id: 1, picture: "http://www.lequipe.fr/Rugby/RugbyImage848.jpg"},
{id: 2, name: "Lionel Beauxis", country_id: 1, picture: "http://www.lequipe.fr/Rugby/RugbyImage4000000000004631.jpg"},
{id: 3, name: "Frederic Michalak", country_id: 1, picture: "http://www.lequipe.fr/Rugby/RugbyImage3548.jpg"},
{id: 4, name: "Nicolas Fernandez Miranda", country_id: 3, picture: "http://www.lequipe.fr/Rugby/RugbyImage1456.jpg"},
{id: 5, name: "Jonny Wilkinson", country_id: 4, picture: "http://www.lequipe.fr/Rugby/RugbyImage956.jpg"},
{id: 6, name: "Bryan Habana", country_id: 2, picture: "http://www.lequipe.fr/Rugby/RugbyImage6000000000004667.jpg"}
],
buildElementDom: function(elmt) {
var div = cn('div');
div.appendChild(cn('p',null,null,elmt.name));
div.appendChild(cn('img',{src: elmt.picture}));
return div;
}
},
{
name: "clubs_players",
fields: {
player_id: "int",
club_id: "int"
},
elements: [
{player_id: 1, club_id: 1},
{player_id: 2, club_id: 5},
{player_id: 3, club_id: 2},
{player_id: 4, club_id: 4},
{player_id: 5, club_id: 6},
{player_id: 6, club_id: 7}
]
}
];

YAHOO.util.Event.addListener(window, "load", function() {
GIM.init('rugbyDB');
// Populate the database
GIM.populateDatabase(struct);
// Reopent the demoDb (updates the table list and the liaisons)
GIM.chooseDb('rugbyDB');
});
19 changes: 15 additions & 4 deletions js/gearsinmotion.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*/
var GIM = {


elementDomFunctions: {},


/**
* Init function
* params:
Expand Down Expand Up @@ -217,8 +221,8 @@ var GIM = {
});

YAHOO.util.Event.addListener(tdOperations, 'click', function() {
if( confirm('destroy '+this.parentNode.tableName+' ?') ) {
GIM.query('drop table '+this.parentNode.tableName);
if( confirm('Drop table '+this.parentNode.tableName+' ?') ) {
GIM.dropTable(this.parentNode.tableName);
GIM.updateMenu();
}
});
Expand Down Expand Up @@ -271,8 +275,15 @@ var GIM = {

var result = this.query('select * from '+tableName+' where id=?',[id])[0];

// Display the selected element
GIM._dataTableBySql_elt = new GIM.DataTableBySql('select * from '+tableName+' where id=?',[id], this.gim_data,tableName);
if( YAHOO.lang.isFunction(GIM.elementDomFunctions[tableName]) ) {
var f = GIM.elementDomFunctions[tableName];
this.gim_data.innerHTML = "";
this.gim_data.appendChild( f(result) );
}
else {
// Display the selected element
new GIM.DataTableBySql('select * from '+tableName+' where id=?',[id], this.gim_data,tableName);
}

GIM._dataTableBySql_manytomany = [];
GIM._dataTableBySql_hasmany = [];
Expand Down
73 changes: 68 additions & 5 deletions js/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ GIM.listFieldInTable = function(tableName) {
}

var res = this.query('select * from sqlite_master where name=?',[tableName]);

var sql = res[0]["sql"];
try {
var regexpResults = sql.match(/^CREATE TABLE [^ ]*[ ]*\((.*)\)$/);
Expand All @@ -141,7 +140,9 @@ GIM.listFieldInTable = function(tableName) {


GIM.insert = function(obj,tableName) {
var fields,simpleFields = this.listFieldInTable(tableName);
var temp = this.listFieldInTable(tableName);

var fields = temp[0],simpleFields = temp[1];
var queryParams = [];
var questionMarks = [];
for(var i = 0 ; i < fields.length ; i++) {
Expand All @@ -154,7 +155,6 @@ GIM.insert = function(obj,tableName) {
};



/**
* This function exports the opened database to sql
*/
Expand All @@ -176,7 +176,7 @@ GIM.exportTablesInSQL = function() {
if( entries[k].hasOwnProperty(field) )
queryParams.push('`'+entries[k][field]+'`');
}
var query = 'insert into '+res[i]['name']+' value ('+queryParams.join(',')+')';
var query = 'insert into '+res[i]['name']+' values ('+queryParams.join(',')+')';
sql_output += query+';\n';
}

Expand Down Expand Up @@ -267,6 +267,8 @@ GIM.autoDiscoverLiaisons = function() {

}
this.liaisons = liaisons;

console.log(this.liaisons);
};

GIM.pluralizeTableName = function(singularName) {
Expand All @@ -276,9 +278,70 @@ GIM.pluralizeTableName = function(singularName) {
return singularName+'s';
};


// Handles the 'y' pluralize into ies
GIM.singularizeTableName = function(pluralName) {
if( pluralName.substr(-3,3) == 'ies' ) {
return pluralName.substr(0,pluralName.length-3)+'y';
}
return pluralName.substr(0,pluralName.length-1);
};
};

GIM.dropTable = function(tableName) {
GIM.query("DROP TABLE IF EXISTS "+tableName+";");
};

/**
*
tableObj = {
name: "users",
fields: [
{id: "integer"},
{name: "varchar(255)"}
]
}
*/
GIM.createTableFromJson = function(tableObj) {

// Drop the table if exists
GIM.dropTable(tableObj.name);

var fields = [];
for(var fieldName in tableObj.fields) {
if( tableObj.fields.hasOwnProperty(fieldName) ) {
fields.push(fieldName+" "+tableObj.fields[fieldName]);
}
}

// Create the table
GIM.query("CREATE TABLE "+tableObj.name+" ("+fields.join(',')+");");

};

GIM.populateTable = function(tableName, elements) {
for(var i = 0 ; i < elements.length ; i++) {
GIM.insert(elements[i],tableName);
}
};

/*
*/
GIM.populateDatabase = function(populatedTableList) {

for(var i = 0; i < populatedTableList.length; i++) {
var populatedTable = populatedTableList[i];

// Create the table
this.createTableFromJson(populatedTable);

// Populate it
GIM.populateTable(populatedTable.name, populatedTable.elements);

// Add the optional function
if(populatedTable.buildElementDom) GIM.elementDomFunctions[populatedTable.name] = populatedTable.buildElementDom;
}

};

0 comments on commit 47e1b0e

Please sign in to comment.