Skip to content

Commit

Permalink
Reset css, rails jquery base
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoliy Chakkaev committed Jan 15, 2011
1 parent 78a8cbb commit 1bc3be0
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 6 deletions.
17 changes: 13 additions & 4 deletions bin/railway.js
Expand Up @@ -48,6 +48,10 @@ function create_file (filename, contents) {
}
}

function create_file_by_template (filename, template) {
create_file(filename, fs.readFileSync(__dirname + '/../templates/' + template));
}

function create_parents(ns, d) {
ns.forEach(function (dir) {
d += dir + '/';
Expand Down Expand Up @@ -134,12 +138,17 @@ case 'init':
'app/helpers/',
'app/views/',
'config/',
'config/initializers/'
'config/initializers/',
'public/',
'public/stylesheets/',
'public/javascripts/'
].forEach(create_dir);
create_file('config/routes.js', 'exports.routes = function (map) {\n};');
create_file('config/requirements.json', fs.readFileSync(__dirname + '/../templates/requirements.json'));
create_file('Jakefile', fs.readFileSync(__dirname + '/../templates/tasks.js'));
create_file('app/views/application_layout.ejs', fs.readFileSync(__dirname + '/../templates/layout.ejs'));
create_file_by_template('config/requirements.json', 'requirements.json');
create_file_by_template('Jakefile', 'tasks.js');
create_file_by_template('app/views/application_layout.ejs', 'layout.ejs');
create_file_by_template('public/stylesheets/reset.css', 'reset.css');
create_file_by_template('public/javascripts/rails.js', 'rails.js');

// patch app.js
var filename = process.cwd() + '/app.js';
Expand Down
Empty file added templates/base.js
Empty file.
4 changes: 2 additions & 2 deletions templates/layout.ejs
Expand Up @@ -2,8 +2,8 @@
<html>
<head>
<title><%= title %></title>
<%- stylesheet_link_tag('style') %>
<%- javascript_include_tag('application', 'https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js') %>
<%- stylesheet_link_tag('reset', 'style') %>
<%- javascript_include_tag('https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', 'rails', 'application') %>
</head>
<body>
<%- body %>
Expand Down
132 changes: 132 additions & 0 deletions templates/rails.js
@@ -0,0 +1,132 @@
jQuery(function ($) {
var csrf_token = $('meta[name=csrf-token]').attr('content'),
csrf_param = $('meta[name=csrf-param]').attr('content');

$.fn.extend({
/**
* Triggers a custom event on an element and returns the event result
* this is used to get around not being able to ensure callbacks are placed
* at the end of the chain.
*
* TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our
* own events and placing ourselves at the end of the chain.
*/
triggerAndReturn: function (name, data) {
var event = new $.Event(name);
this.trigger(event, data);

return event.result !== false;
},

/**
* Handles execution of remote calls firing overridable events along the way
*/
callRemote: function () {
var el = this,
method = el.attr('method') || el.attr('data-method') || 'GET',
url = el.attr('action') || el.attr('href'),
dataType = el.attr('data-type') || 'script';

if (url === undefined) {
throw "No URL specified for remote call (action or href must be present).";
} else {
if (el.triggerAndReturn('ajax:before')) {
var data = el.is('form') ? el.serializeArray() : [];
$.ajax({
url: url,
data: data,
dataType: dataType,
type: method.toUpperCase(),
beforeSend: function (xhr) {
el.trigger('ajax:loading', xhr);
},
success: function (data, status, xhr) {
el.trigger('ajax:success', [data, status, xhr]);
},
complete: function (xhr) {
el.trigger('ajax:complete', xhr);
},
error: function (xhr, status, error) {
el.trigger('ajax:failure', [xhr, status, error]);
}
});
}

el.trigger('ajax:after');
}
}
});

/**
* confirmation handler
*/
$('a[data-confirm],input[data-confirm]').live('click', function () {
var el = $(this);
if (el.triggerAndReturn('confirm')) {
if (!confirm(el.attr('data-confirm'))) {
return false;
}
}
});


/**
* remote handlers
*/
$('form[data-remote]').live('submit', function (e) {
$(this).callRemote();
e.preventDefault();
});

$('a[data-remote],input[data-remote]').live('click', function (e) {
$(this).callRemote();
e.preventDefault();
});

$('a[data-method]:not([data-remote])').live('click', function (e){
var link = $(this),
href = link.attr('href'),
method = link.attr('data-method'),
form = $('<form method="post" action="'+href+'"></form>'),
metadata_input = '<input name="_method" value="'+method+'" type="hidden" />';

if (csrf_param != null && csrf_token != null) {
metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />';
}

form.hide()
.append(metadata_input)
.appendTo('body');

e.preventDefault();
form.submit();
});

/**
* disable-with handlers
*/
var disable_with_input_selector = 'input[data-disable-with]';
var disable_with_form_remote_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')';
var disable_with_form_not_remote_selector = 'form:not([data-remote]):has(' + disable_with_input_selector + ')';

var disable_with_input_function = function () {
$(this).find(disable_with_input_selector).each(function () {
var input = $(this);
input.data('enable-with', input.val())
.attr('value', input.attr('data-disable-with'))
.attr('disabled', 'disabled');
});
};

$(disable_with_form_remote_selector).live('ajax:before', disable_with_input_function);
$(disable_with_form_not_remote_selector).live('submit', disable_with_input_function);

$(disable_with_form_remote_selector).live('ajax:complete', function () {
$(this).find(disable_with_input_selector).each(function () {
var input = $(this);
input.removeAttr('disabled')
.val(input.data('enable-with'));
});
});

});
52 changes: 52 additions & 0 deletions templates/reset.css
@@ -0,0 +1,52 @@
/* v1.0 | 20080212 */

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, font, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
vertical-align: baseline;
background: transparent;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}

/* remember to define focus styles! */
:focus {
outline: 0;
}

/* remember to highlight inserts somehow! */
ins {
text-decoration: none;
}
del {
text-decoration: line-through;
}

/* tables still need 'cellspacing="0"' in the markup */
table {
border-collapse: collapse;
border-spacing: 0;
}

0 comments on commit 1bc3be0

Please sign in to comment.