Permalink
Browse files

Remote fetching, css loading, combo handler, inline docs, broke out i…

…nto modules
  • Loading branch information...
1 parent f7737da commit cb5da0948320bcdaaaa1778c94caf43ecc118e5f @davglass davglass committed Sep 10, 2010
View
@@ -1,2 +1,3 @@
.git
sandbox
+yui3
@@ -0,0 +1 @@
+alert('Loaded..');
@@ -0,0 +1,5 @@
+YUI({ fetchCSS: false }).use('tabview', function(Y) {
+ new Y.TabView({
+ srcNode: '#demo .yui3-tabview-content'
+ }).render();
+});
@@ -0,0 +1,92 @@
+#!/usr/bin/env node
+
+var express = require('express'),
+ YUI = require('yui3').YUI;
+
+
+YUI({ debug: false, filter: 'min' }).use('express', 'node', function(Y) {
+
+ var app = express.createServer();
+
+ app.configure(function(){
+ app.use(express.methodOverride());
+ app.use(express.bodyDecoder());
+ app.use(app.router);
+ app.use(express.staticProvider(__dirname + '/assets'));
+ });
+
+ app.register('.html', YUI);
+
+ app.get('/combo', YUI.combo);
+
+ app.get('/tabview', function(req, res) {
+ YUI().use('node', function(Y) {
+ Y.Env._loader.ignoreRegistered = true;
+ Y.use('tabview', function(Y) {
+ var div = Y.Node.create('<div id="demo"></div>');
+ Y.one('title').set('innerHTML', 'YUI3 tabView Page');
+ Y.one('body').addClass('yui3-skin-sam').appendChild(div);
+
+ Y.log('Creating the TabView from script..');
+ var tabview = new Y.TabView({
+ children: [{
+ label: 'foo',
+ content: '<p>foo content</p>'
+ }, {
+ label: 'bar',
+ content: '<p>bar content</p>'
+ }, {
+ label: 'baz',
+ content: '<p>baz content</p>'
+ }]
+ });
+ tabview.render('#demo');
+
+ res.render('tabview.html', {
+ locals: {
+ instance: Y,
+ use: ['tabview'],
+ //filter: 'debug',
+ content: '#content',
+ after: function(Y) {
+ Y.Get.domScript('/tabview.js');
+ }
+ }
+ });
+ });
+ });
+ });
+
+ app.get('/', function(req, res){
+ res.render('index.html', {
+ locals: {
+ content: '#content',
+ sub: {
+ title1: 'Title #1',
+ title2: 'Title #2',
+ title3: 'Title #3',
+ title4: 'Title #4'
+ },
+ after: function(Y, options, partial) {
+ Y.Get.domScript('/main.js');
+ Y.one('title').set('innerHTML', 'This is a test');
+
+ var str = partial('test');
+ var html = '';
+ var data = ['one', 'two', 'three'];
+ data.forEach(function(v) {
+ html += Y.Lang.sub(str, { name: v })
+ });
+ Y.one('#content').prepend('<ul>' + html + '</ul>');
+ },
+ yui: function(Y) {
+ Y.one('h1').set('innerHTML', 'BooYah!!');
+ }
+ }
+ });
+ });
+
+ app.listen(3000);
+
+});
+
@@ -0,0 +1,8 @@
+<h1>{title}</h1>
+<p>This is only a test. This is only a test. This is only a test. This is only a test. This is only a test. This is only a test. </p>
+<h2>{title2}</h2>
+<p>This is only a test. This is only a test. This is only a test. This is only a test. This is only a test. This is only a test. </p>
+<h3>{title3}</h3>
+<p>This is only a test. This is only a test. This is only a test. This is only a test. This is only a test. This is only a test. </p>
+<h4>{title3}</h4>
+<p>This is only a test. This is only a test. This is only a test. This is only a test. This is only a test. This is only a test. </p>
@@ -0,0 +1,7 @@
+Above normal content
+<hr>
+<div id="content">
+
+</div>
+<hr>
+Below normal content
@@ -0,0 +1 @@
+<li>{name}</li>
@@ -0,0 +1 @@
+<p>This is only a test. This is only a test. This is only a test. This is only a test. This is only a test. This is only a test. </p>
View
@@ -0,0 +1,38 @@
+#!/usr/bin/env node
+
+var YUI = require("yui3").YUI;
+
+YUI({
+ debug: true
+}).use('node', 'io', function(Y) {
+
+ //Messing with the main page..
+ Y.one('title').set('innerHTML', 'Digg News Headlines');
+ //Creating the list that we will append the remote data to
+ var ul = Y.one('body').appendChild(Y.Node.create('<ul></ul>'));
+
+ //Creating a sandboxed instance that we will bind to the remote page that we fetch
+ YUI().use('node', function(remotePage) {
+ //The page we are fetching
+ var url = 'http://digg.com/news';
+
+ //This will call io under the hood and get the content of the URL,
+ //It will then dump the content of that page into this sandboxed document.
+ remotePage.fetch(url, function() {
+ //Get all the news items from the remote page.
+ var newsItems = remotePage.all('#story-items h3');
+ //Iterate them
+ newsItems.each(function(n) {
+ //Import this "A" node into the outside instances document
+ var a = ul.importNode(n.one('a'), true);
+ //Clean up the relative URL's of hrefs
+ a.set('href', 'http://digg.com' + a.get('href'));
+ //Append the new node to the list
+ ul.appendChild(Y.Node.create('<li></li>')).append(a);
+ });
+ //Now, we can print the "outer" instances html and drop it to the screen
+ console.log(Y.get('doc').get('outerHTML'));
+ });
+ });
+
+});
View
@@ -0,0 +1,24 @@
+/**
+* Module loads jsdom & htmlparser support into YUI. Should be autoloaded once DOM is needed.
+* @module nodejs-dom
+*/
+YUI.add('nodejs-dom', function(Y) {
+ var jsdom = YUI.require('jsdom'),
+ browser, dom;
+
+ dom = jsdom.defaultLevel;
+ browser = jsdom.windowAugmentation(dom, {
+ parser: YUI.require('htmlparser')
+ });
+
+ browser.window.eval = eval;
+
+ Y.config.doc = browser.window.document;
+ Y.config.win = browser.window;
+
+ Y.Browser = browser;
+
+ Y.processCSS();
+
+}, 'NODE', { requires: ['yui-base'], after: ['yui-base'] });
+
View
@@ -0,0 +1,195 @@
+/**
+* ExpressJS view engine for YUI3
+* @module express
+*/
+YUI.add('express', function(Y) {
+
+ /**
+ * List of docTypes that can be used in the views
+ * @static
+ * @property docTypes
+ */
+ YUI.docTypes = {
+ '5': '<!DOCTYPE html>', //DEFAULT
+ '4-strict': '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
+ '4-trans': '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
+ 'x-strict': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
+ 'x-trans': '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
+ };
+
+ /**
+ * The default docType to use when rendering content, defaults to HTML5: <!DOCTYPE html>
+ * @static
+ * @property defaultDocType
+ */
+ YUI.defaultDocType = '5';
+
+ /**
+ * Default handler for expressjs view rendering. app.register('.html', YUI);
+ * @static
+ * @method YUI.render
+ */
+ YUI.render = function(content, options) {
+
+ var eY, locals = options.locals;
+ if (locals.instance) {
+ eY = locals.instance;
+ } else {
+ eY = YUI().use('node');
+ };
+
+ if (locals.sub) {
+ content = eY.Lang.sub(content, locals.sub);
+ }
+ if (locals.instance) {
+ if (!options.isLayout) {
+ eY.one('body').prepend(content);
+ } else {
+ var html = eY.one('body').get('innerHTML');
+ eY.one('body').set('innerHTML', content);
+ content = html;
+ }
+ } else {
+ eY.one('body').set('innerHTML', content);
+ }
+
+ if (options.isLayout) {
+ var docType = YUI.defaultDocType;
+ if (locals.docType) {
+ if (YUI.docTypes[locals.docType]) {
+ docType = locals.docType;
+ }
+ }
+ docType = YUI.docTypes[docType];
+ var html = docType + "\n";
+ var content = eY.one(locals.content);
+ if (content && locals.body) {
+ content.set('innerHTML', locals.body);
+ }
+ if (locals.use) {
+ eY.Get.domScript('/combo?' + locals.use.join('&') + ((locals.filter) ? '&filter=' + locals.filter : ''));
+ }
+ if (locals.after) {
+ locals.after(eY, options, locals.partial);
+ }
+ html += eY.config.doc.outerHTML;
+ return html;
+ } else {
+
+ if (locals.yui) {
+ locals.yui(eY, options, locals.partial);
+ }
+ return eY.one('body').get('innerHTML');
+ }
+
+ };
+
+ var fs = YUI.require('fs');
+
+ /**
+ * Simple YUI based combo handler (only for YUI files and has a custom url signature)
+ */
+ YUI.comboCache = {};
+ YUI.comboSent = {};
+
+ /**
+ * Method is designed to be dropped into an express get handler, should really be "combo": app.get('/combo', YUI.combo);
+ * @method YUI.combo
+ * @static
+ */
+ YUI.combo = function(req, res) {
+ var filter = 'min';
+ if (req.query.filter) {
+ filter = req.query.filter;
+ delete req.query.filter;
+ }
+ var keys = Y.Object.keys(req.query),
+ fileCount = 0, files = [];
+
+ //This is a bug, seems that event and base are not added to the combo'd out list.
+ keys.push('yui-base', 'loader', 'event', 'base');
+
+ YUI({ debug: false, filter: 'min' }).use('loader', 'oop', function(Y) {
+
+ var loader = new Y.Loader({
+ ignoreRegistered: true,
+ require: keys,
+ force: keys.concat("yui-base", "loader", 'oop', 'yui-throttle', 'intl', 'get'),
+ allowRollup: true,
+ filter: filter,
+ loadOptional: false,
+ combine: false
+ });
+ loader.base = Y.config.base;
+ loader.calculate();
+
+ var s = loader.sorted, l = s.length, m, surl, out = [], i;
+ if (l) {
+ for (i=0; i <l; i=i+1) {
+ m = loader.moduleInfo[s[i]];
+ if (s[i].indexOf('nodejs') === -1) {
+ if (m && m.type == 'js') {
+ surl = m.fullpath || loader._url(m.path);
+ out.push(surl);
+ }
+ }
+ }
+ }
+
+ var sendRequest = function() {
+ if (fileCount == out.length) {
+ //console.log('Files are all done..');
+ var body = files.join("\n");
+ var status = 200;
+
+ if (YUI.comboSent[req.url]) {
+ //status = 304;
+ }
+ YUI.comboSent[req.url] = true;
+ res.send(body, {
+ 'Content-Type': 'application/x-javascript',
+ //'Content-Type': 'text/plain',
+ 'Content-Length': body.length,
+ 'Cache-Control': 'max-age=315360000',
+ 'Vary': 'Accept-Encoding',
+ 'Date': new Date(),
+ 'Expires': new Date((new Date()).getTime() + (60 * 60 * 1000 * 365 * 10)),
+ 'Age': '300',
+ 'X-YUI-Combo': req.url
+ }, status);
+ }
+ };
+
+ Y.each(out, function(v, k) {
+ f = v;
+ if (YUI.comboCache[f]) {
+ //console.log('File Loaded from cache (' + k + '): ' + out[k]);
+ fileCount++;
+ files[k] = YUI.comboCache[f];
+ sendRequest();
+ } else {
+ fs.readFile(f, encoding="utf8", Y.rbind(function(err, data, index, fileName) {
+ fileCount++;
+ if (err) {
+ index = data;
+ fileName = index;
+ }
+ //console.log('File Loaded from disk (' + index + '): ' + out[index]);
+ if (err) {
+ //console.log('Throw Error: ', out[index]);
+ //throwError(err, out[index]);
+ } else {
+ files[index] = data;
+ YUI.comboCache[fileName] = data;
+ }
+ sendRequest();
+ }, Y, k, f));
+ }
+ });
+
+
+ });
+
+ };
+
+}, 'NODE');
Oops, something went wrong.

0 comments on commit cb5da09

Please sign in to comment.