Permalink
Browse files

Add generator

  • Loading branch information...
1 parent 3ae6f1c commit b70bd28f2cd4010731b4bd5cbcdae5222bb531c8 @unicornrainbow unicornrainbow committed May 17, 2011
View
5 javascripto-rails/lib/generators/javascripto/install/USAGE
@@ -0,0 +1,5 @@
+Description:
+ Initialize Javascripto for your application.
+
+Examples:
+ `script/rails generate javascripto:rails`
View
14 javascripto-rails/lib/generators/javascripto/install/install_generator.rb
@@ -0,0 +1,14 @@
+module Javascripto
+ module Generators
+ class InstallGenerator < ::Rails::Generators::Base
+ source_root ::File.expand_path("../templates", __FILE__)
+
+ def generate
+ copy_file 'config/javascripto.yml', 'config/javascripto.yml'
+ directory 'public/javascripts'
+ empty_directory 'public/javascripts/app'
+ empty_directory 'public/javascripts/lib'
+ end
+ end
+ end
+end
View
1 javascripto-rails/lib/generators/javascripto/install/templates/config/javascripto.yml
@@ -0,0 +1 @@
+packages: []
View
8 javascripto-rails/lib/generators/javascripto/install/templates/public/javascripts/config.js
@@ -0,0 +1,8 @@
+// require jquery
+// require rails
+
+app = {
+ config: {
+ // You can put front-end configuration here.
+ }
+};
View
139 javascripto-rails/lib/generators/javascripto/install/templates/public/javascripts/rails.js
@@ -0,0 +1,139 @@
+// require jquery
+
+/**
+ * Unobtrusive scripting adapter for jQuery
+ *
+ * Requires jQuery 1.4.3 or later.
+ * https://github.com/rails/jquery-ujs
+ */
+
+(function($) {
+ // Triggers an event on an element and returns the event result
+ function fire(obj, name, data) {
+ var event = new $.Event(name);
+ obj.trigger(event, data);
+ return event.result !== false;
+ }
+
+ // Submits "remote" forms and links with ajax
+ function handleRemote(element) {
+ var method, url, data,
+ dataType = element.attr('data-type') || ($.ajaxSettings && $.ajaxSettings.dataType);
+
+ if (element.is('form')) {
+ method = element.attr('method');
+ url = element.attr('action');
+ data = element.serializeArray();
+ // memoized value from clicked submit button
+ var button = element.data('ujs:submit-button');
+ if (button) {
+ data.push(button);
+ element.data('ujs:submit-button', null);
+ }
+ } else {
+ method = element.attr('data-method');
+ url = element.attr('href');
+ data = null;
+ }
+
+ $.ajax({
+ url: url, type: method || 'GET', data: data, dataType: dataType,
+ // stopping the "ajax:beforeSend" event will cancel the ajax request
+ beforeSend: function(xhr, settings) {
+ if (settings.dataType === undefined) {
+ xhr.setRequestHeader('accept', '*/*;q=0.5, ' + settings.accepts.script);
+ }
+ return fire(element, 'ajax:beforeSend', [xhr, settings]);
+ },
+ success: function(data, status, xhr) {
+ element.trigger('ajax:success', [data, status, xhr]);
+ },
+ complete: function(xhr, status) {
+ element.trigger('ajax:complete', [xhr, status]);
+ },
+ error: function(xhr, status, error) {
+ element.trigger('ajax:error', [xhr, status, error]);
+ }
+ });
+ }
+
+ // Handles "data-method" on links such as:
+ // <a href="/users/5" data-method="delete" rel="nofollow" data-confirm="Are you sure?">Delete</a>
+ function handleMethod(link) {
+ var href = link.attr('href'),
+ method = link.attr('data-method'),
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
+ form = $('<form method="post" action="' + href + '"></form>'),
+ metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
+
+ if (csrf_param !== undefined && csrf_token !== undefined) {
+ metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
+ }
+
+ form.hide().append(metadata_input).appendTo('body');
+ form.submit();
+ }
+
+ function disableFormElements(form) {
+ form.find('input[data-disable-with]').each(function() {
+ var input = $(this);
+ input.data('ujs:enable-with', input.val())
+ .val(input.attr('data-disable-with'))
+ .attr('disabled', 'disabled');
+ });
+ }
+
+ function enableFormElements(form) {
+ form.find('input[data-disable-with]').each(function() {
+ var input = $(this);
+ input.val(input.data('ujs:enable-with')).removeAttr('disabled');
+ });
+ }
+
+ function allowAction(element) {
+ var message = element.attr('data-confirm');
+ return !message || (fire(element, 'confirm') && confirm(message));
+ }
+
+ $('a[data-confirm], a[data-method], a[data-remote]').live('click.rails', function(e) {
+ var link = $(this);
+ if (!allowAction(link)) return false;
+
+ if (link.attr('data-remote')) {
+ handleRemote(link);
+ return false;
+ } else if (link.attr('data-method')) {
+ handleMethod(link);
+ return false;
+ }
+ });
+
+ $('form').live('submit.rails', function(e) {
+ var form = $(this);
+ if (!allowAction(form)) return false;
+
+ if (form.attr('data-remote')) {
+ handleRemote(form);
+ return false;
+ } else {
+ disableFormElements(form);
+ }
+ });
+
+ $('form input[type=submit], form button[type=submit], form button:not([type])').live('click.rails', function() {
+ var button = $(this);
+ if (!allowAction(button)) return false;
+ // register the pressed submit button
+ var name = button.attr('name'), data = name ? {name:name, value:button.val()} : null;
+ button.closest('form').data('ujs:submit-button', data);
+ });
+
+ $('form').live('ajax:beforeSend.rails', function(event) {
+ if (this == event.target) disableFormElements($(this));
+ });
+
+ $('form').live('ajax:complete.rails', function(event) {
+ if (this == event.target) enableFormElements($(this));
+ });
+})( jQuery );
View
1 ...s/lib/generators/javascripto/install/templates/public/javascripts/vendor/jquery/jquery.js
@@ -0,0 +1 @@
+// use_remote_package https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js

0 comments on commit b70bd28

Please sign in to comment.