• This repository has been archived on 07/Feb/2022
  • Stars
    star
    121
  • Rank 293,040 (Top 6 %)
  • Language
    JavaScript
  • Created about 13 years ago
  • Updated about 13 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[ARCHIVED] fox.js allows you to use ASCII-art to create grid layouts in JavaScript.

fox.js

fox.js allows you to use ASCII-art to create grid layouts in JavaScript.

Grid layouts are often an important part of creating rich internet applications, and yet creating them is still quite difficult and verbose. The humble and experimental goal of fox.js is to see whether ASCII art provides a helpful way to design such layouts for applications easily and flexibly.

Oh, right. Table layouts are evil. Look away now if you want. I'll just say that: a) this is on the client side, so semantics for SEO and so on become less important; and b) I've plans to allow fox.js to support other layout approaches like flex and grid anyway.

In the 21st century, you shouldn't care; but fox.js is less than 1k compressed.

See some examples, or read the guide below to get started.

Usage

Include fox.js in your web app. It adds the global Fox namespace. Currently this has one function of note: Fox.makeTable().

This function takes two arguments. The first is an array of equally-lengthed strings that constitute the ASCII-art of the layout you want. The second is a configuration object of options that alters the way the function works.

By default, the function returns a DOM node for the table you've drawn. So, the simplest usage of the function is something like:

document.body.appendChild(
    Fox.makeTable([
        "+---+",
        "|A  |",
        "+---+",
    ])
);

This adds a one row, one column table to the document's body, with the letter A in it. See below for how to use the 'map' option to patch longer strings, elements, or views into the layout.

Or course you also can do something more exotic, with row and cell spanning:

document.body.appendChild(
    Fox.makeTable([
        "+---+---+---+",
        "|A  |B  |C  |",
        "|   +---+---+",
        "|   |D  |E  |",
        "|   +---+   |",
        "|   |F  |   |",
        "+---+---+---+",
    ])
);

The rules to obey when you're drawing your table are as follows:

  • All rows must be the same length
  • Vertices of cells must be drawn with a plus symbol +
  • Horizontal edges of cells must be drawn with a hyphen, -
  • Vertical edges of cells must be drawn with a pipe symbol, |
  • Every cell must be at least one character high, and one character wide
  • Label cells with a single letter

Watch for wayward or missing + symbols. Unexpected behavior may ensue.

Configuration

The configuration options (in the function's second object argument) are as follows:

output

The output option takes the following possible values:

  • 'node' (default) - the function returns the table as a HTMLTableElement node
  • 'html' - the function returns the table's HTML as a string
  • 'raw' - the function returns the table as a raw JavaScript arrays & objects

For example, to prompt the HTML string of the table:

alert(
    Fox.makeTable([
        "+---+",
        "|A  |",
        "+---+",
    ], {
        output: 'html'
    })
);

The 'raw' output mode returns an array of table rows, which in turn are arrays of cell objects, containing properties describing the cells' row and column spans, content, and styling.

(If you wish to alter these objects and then resubmit them to Fox to have them turned into an HTMLTableElement, you can use the Fox.toTableElement() function)

There's a JSON.stringify example in index.html to show what this resulting structure looks like.

map

The map option allows you to patch strings, elements, or views into your table layout. Pass in an object with properties of the same name as the letters you've placed in your ASCII art. The values of these properties can be strings, DOM elements, or any object that has an el property (such as a Backbone.js view).

For example:

document.body.appendChild(
    Fox.makeTable([
        "+---+---+",
        "|a  |b  |",
        "+---+---+",
        "|c  |d  |",
        "+---+---+",
    ], {
        map: {
            a: "The letter A",
            b: document.createElement('img'),
            c: {id:'myView', el:document.createElement('img')}
        }
    })
);

styles

Using an object in the same way as for the map above, you can also add specific styles to the tables' cells. This allows you to have control over the layout's dimensions and colors:

document.body.appendChild(
    Fox.makeTable([
        "+-----------+---+",
        "|a          |b  |",
        "|---+-------+---+",
        "|c  |d          |",
        "|   |           |",
        "|   |           |",
        "|   +---+-------+",
        "|   |e  |f      |",
        "+---+---+-------+",
    ], {
        debug:true,
        map: {
            a: "Toolbar",
            b: "Menu",
            c: "Sidebar",
            d: "Canvas",
            e: "Button",
            f: "Button"
        },
        styles: {
            table: {'border-collapse':'collapse'},
            a: {width: '80%', background: '#ccc'},
            b: {width: '20%'},
            c: {width: '20%', background: '#777'},
            d: {height: '150px', width: '80%'},
            e: {width: '20%'},
            f: {width: '60%'}
        }
    })
);

Use the string-based versions of CSS property names as the objects' properties. For example, to specify a maximum width, use 'max-width' rather than maxWidth.

You can also set a table property which sets style on the outer table as a whole. And you can also specify a special property 'fixed-width' which will generate real max-width, min-width and width properties to all have that same value.

debug

As it says on the tin. It's off by default, but set to true to log to the console the variables used while processing the table's art, as well as the outputted raw object. Not really much use to anyone except me for when things don't work properly.

Browser support

The library is developed against Chrome, and I'm sure contemporary WebKit browsers will be fine. The library doesn't do anything very clever except use forEach for a few of its loops. So certainly JavaScript 1.6 or polyfills are required for that.

Drop me a pull request if you're feeling nostalgic for less-abled browsers.

Examples

Check out the index.html file in the project directory to see the library at work with a few simple examples.

More Repositories

1

confess

[ARCHIVED] Uses PhantomJS to headlessly analyze web pages and generate manifests. You may be able to use the puppeteer-har package instead.
JavaScript
875
star
2

modernizr-server

[ARCHIVED] The modernizr-server library is a way to bring Modernizr browser data to your server scripting environment. PHP currently supported, but other server-side environments to come (and subject to demand!)
PHP
219
star
3

compios5

[ARCHIVED] A web-based compass application for iOS5 devices
45
star
4

monomi

[ARCHIVED] Middleware for node.js/Connect that provides tools for handling mobile (and other types of) browsers.
JavaScript
40
star
5

nashville

[ARCHIVED] An app of bars, restaurants and hotels in Nashville TN. Yelp throughput permitting, you can try it out (with a WebKit browser) at ...
JavaScript
16
star
6

gumstogram

[ARCHIVED] A web-based photo app in 100 lines or less - and which currently requires Opera 12 (preferably Mobile) to fire up getUserMedia. Got it already? Hit the URL below.
7
star
7

project1015

[ARCHIVED] Designed to make it easy to identify and ascertain the capabilities of different browsers accessing a web server.
PHP
6
star
8

mobiledetect

[ARCHIVED] Inspects HTTP headers presented to a PHP server and determines if the browser is likely to be a mobile device or not.
PHP
4
star
9

tweetphysics

[ARCHIVED] A JavaScript physics engine in a tweet.
JavaScript
4
star
10

wizardjs

[ARCHIVED] Just a scratch pad of some old ideas
JavaScript
4
star
11

castline

[ARCHIVED] Hackathon project to post podcasting habits to the Open Graph
Python
3
star
12

ValleyGuide

[ARCHIVED] A simple app to demonstrate Sencha Touch with a Yelp integration.
JavaScript
1
star
13

BaconOff

[ARCHIVED] A sample Facebook app hurredly built with Sencha Touch, node, IMDB, etc etc
JavaScript
1
star