Permalink
Browse files

first yui setup

  • Loading branch information...
1 parent dbe61d0 commit 573d4302c4e7c2f7f0f81f0cf876635c768e6c8e @wscherphof committed Jul 2, 2012
Showing with 146 additions and 24 deletions.
  1. +3 −0 clues.csv
  2. +97 −0 public/scripts/scripts.js
  3. +23 −24 server.js
  4. +23 −0 views/layout.jade
View
3 clues.csv
@@ -0,0 +1,3 @@
+naam,adres,woonplaats
+Wouter Scherphof,Spoorbaanweg 17,Rhenen
+Marian Scherphof,Spoorbaanweg 17,Rhenen
View
97 public/scripts/scripts.js
@@ -0,0 +1,97 @@
+YUI().use('node', 'datatable', 'transition', 'app', 'io-base', function (Y) {
+
+ var addCluesContainer = Y.one('#addClues');
+ var addClues = {
+ get: function (what) {
+ if (what === 'text') return addCluesContainer.one('#text').get('value');
+ if (what === 'fieldNames') return addCluesContainer.one('#fieldNames').get('checked');
+ if (what === 'purge') return addCluesContainer.one('#purge').get('checked');
+ },
+ submit: addCluesContainer.one('#submit'),
+ };
+
+ var displayCluesContainer = Y.one('#displayClues');
+ var displayClues = {
+ back: displayCluesContainer.one('#back'),
+ };
+
+ Y.io.header('Content-Type', 'application/json');
+ Y.ClueList = Y.Base.create('clueList', Y.ModelList, [], {
+ sync: function (action, options, callback) {
+ if (action !== 'read') return;
+ Y.io('xxxxxxxxxxxxxxxxxxxxxxxxxxxx', {
+ method: 'GET',
+ timeout: 2000,
+ on: {
+ start: function () {
+ console.log('start');
+ },
+ success: function (id, res) {
+ console.log('success');
+ console.log(res);
+ },
+ failure: function (id, res) {
+ console.log('failure');
+ console.log(res);
+ },
+ complete: function (id, res) {
+ console.log('complete');
+ console.log(res);
+ },
+ end: function () {
+ console.log('end');
+ }
+ }
+ });
+ }
+ });
+ var clueList = new Y.ClueList();
+ clueList.load();
+
+ addClues.submit.on('click', function () {
+ var clues = parseClues(
+ addClues.get('text'),
+ addClues.get('fieldNames'),
+ addClues.get('purge')
+ );
+ var table = new Y.DataTable({
+ columns: data.columns,
+ data: data.data
+ });
+ table.render('#cluesTable');
+ addCluesContainer.hide();
+ displayCluesContainer.show();
+ });
+
+ displayClues.back.on('click', function () {
+ displayCluesContainer.hide();
+ addCluesContainer.show();
+ });
+
+});
+
+
+var clues = [];
+var clueFields = [];
+function parseClues (text, fieldNames, purge) {
+ var lines = text.split('\n');
+ if (purge) clues = clueFields = [];
+ if (fieldNames) {
+ var firstLine = lines.shift();
+ if (clueFields.length === 0) clueFields = firstLine.split(',');
+ }
+ lines.forEach(function (line) {
+ if (line === '') return;
+ var clue = {};
+ var values = line.split(',');
+ values.forEach(function (value, i) {
+ var name = clueFields[i] || i;
+ clue[name] = value;
+ });
+ clues.push(clue);
+ });
+ return {
+ columns: clueFields,
+ data: clues
+ };
+}
View
47 server.js
@@ -1,37 +1,36 @@
-var http = require('http');
-var util = require('util');
var express = require('express');
var app = express.createServer();
app.configure(function () {
+ app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
});
-var clues = [];
-var clueFields = [];
-app.get('/clues', function (req, res) {
- res.render('clues.jade', {layout: false});
+
+app.all('*', function (req, res, next) {
+ console.log(req.method + ' ' + req.url);
+ next();
+});
+
+
+app.get('/', function (req, res, next) {
+ res.render('layout.jade');
+});
+
+
+var clues = {
+ columns: [],
+ data: []
+};
+
+app.get('/clues', function (req, res, next) {
+ res.json(clues);
});
-app.post('/clues', function (req, res) {
- var lines = req.body.clues.split('\r\n');
- if (req.body.purge) clues = clueFields = [];
- if (req.body.fieldNames) {
- var firstLine = lines.shift();
- if (clueFields.length === 0) clueFields = firstLine.split(',');
- }
- lines.forEach(function (line) {
- if (line === '') return;
- var clue = {};
- var values = line.split(',');
- values.forEach(function (value, i) {
- var name = clueFields[i] || i;
- clue[name] = value;
- });
- clues.push(clue);
- });
- res.end(util.inspect(clues));
+app.post('/clues', function (req, res, next) {
+ clues = req.body;
+ res.json(clues);
});
View
23 views/layout.jade
@@ -0,0 +1,23 @@
+doctype html
+html
+ head
+ body
+ header
+ h1 Welcome
+ section#addClues(style="display: none")
+ h1 Add clues
+ textarea#text(placeholder='Paste in some clues as comma-separated values')
+ br
+ input#fieldNames(type='checkbox')
+ |First line contains field names
+  
+ input#purge(type='checkbox')
+ |Purge - clear all previously defnined clues
+ br
+ button#submit Add
+ section#displayClues(style="display: none")
+ div#cluesTable
+ button#back Back
+
+ script(src="http://yui.yahooapis.com/3.5.1/build/yui/yui-min.js")
+ script(src="/scripts/scripts.js")

0 comments on commit 573d430

Please sign in to comment.