ujsos 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +3 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/lib/assets/javascripts/jquery_ujsos.js +133 -0
- data/lib/ujsos/dom_updater.rb +38 -0
- data/lib/ujsos/htmlf_responder.rb +23 -0
- data/lib/ujsos/rails_engine.rb +40 -0
- data/lib/ujsos/version.rb +3 -0
- data/lib/ujsos.rb +3 -0
- data/ujsos.gemspec +21 -0
- metadata +77 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
$(function($) {
|
2
|
+
$.domupdater = null;
|
3
|
+
|
4
|
+
function setDomUpdater(element) {
|
5
|
+
receiver = undefined;
|
6
|
+
|
7
|
+
// update an element's content
|
8
|
+
if ((receiver = $(element).attr('data-ujsos-update')) != null) {
|
9
|
+
var position = $(element).attr('data-ujsos-update-position');
|
10
|
+
|
11
|
+
$.domupdater = function(data) {
|
12
|
+
if (receiver[0] != '#' && receiver[0] != '.') receiver = "#" + receiver;
|
13
|
+
$(receiver).html(data);
|
14
|
+
};
|
15
|
+
|
16
|
+
}
|
17
|
+
|
18
|
+
// replace a whole element
|
19
|
+
else if ((receiver = $(element).attr('data-ujsos-replace')) != null) {
|
20
|
+
if (receiver == 'self') {
|
21
|
+
$.domupdater = function(data) {
|
22
|
+
element.replaceWith(data);
|
23
|
+
};
|
24
|
+
}
|
25
|
+
else if (receiver == 'response') {
|
26
|
+
$.domupdater = function(data) {
|
27
|
+
var idName = null;
|
28
|
+
try {
|
29
|
+
var dataJSON = JSON.parse(data);
|
30
|
+
for (var i = 0; i < dataJSON.length; i++) {
|
31
|
+
idName = $(dataJSON[i]).attr('id');
|
32
|
+
$('#' + idName).replaceWith(dataJSON[i]);
|
33
|
+
}
|
34
|
+
}
|
35
|
+
catch(error) {
|
36
|
+
idName = $(data).attr('id');
|
37
|
+
$('#' + idName).replaceWith(data);
|
38
|
+
}
|
39
|
+
};
|
40
|
+
}
|
41
|
+
|
42
|
+
else if (receiver.substr(0, 6) == 'remove') {
|
43
|
+
$.domupdater = function(data) {
|
44
|
+
$(receiver.substring(6)).remove();
|
45
|
+
};
|
46
|
+
}
|
47
|
+
|
48
|
+
// replace an element by class
|
49
|
+
else if (receiver[0] == '.') {
|
50
|
+
var className = receiver;
|
51
|
+
$.domupdater = function(data) {
|
52
|
+
$(element).parents(className).each(function() {
|
53
|
+
$(this).replaceWith(data);
|
54
|
+
});
|
55
|
+
};
|
56
|
+
}
|
57
|
+
|
58
|
+
// replace an element by #
|
59
|
+
else if (receiver[0] == '#') {
|
60
|
+
var idName = receiver;
|
61
|
+
$.domupdater = function(data) {
|
62
|
+
$(idName).replaceWith(data);
|
63
|
+
};
|
64
|
+
}
|
65
|
+
|
66
|
+
}
|
67
|
+
}
|
68
|
+
|
69
|
+
function setRemoteFormat(element, settings) {
|
70
|
+
var format = $(element).attr('data-ujsos-format');
|
71
|
+
if (format != null) {
|
72
|
+
if (settings.data != null) {
|
73
|
+
settings.data += "&format=" + format;
|
74
|
+
}
|
75
|
+
|
76
|
+
var regEx = new RegExp('.'+format, "g");
|
77
|
+
if (settings.url.search(regEx) == -1) {
|
78
|
+
if (settings.url.search(/\?/g) != -1) {
|
79
|
+
settings.url = settings.url.replace(/\?/,'.' + format + '?');
|
80
|
+
} else {
|
81
|
+
settings.url += '.' + format;
|
82
|
+
}
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
// Bind handlers
|
88
|
+
$('a[data-remote]').live('ajax:beforeSend', function(event, xhr, settings) {
|
89
|
+
setRemoteFormat(this, settings);
|
90
|
+
});
|
91
|
+
|
92
|
+
$('form[data-remote]').live('ajax:beforeSend', function(event, xhr, settings) {
|
93
|
+
setRemoteFormat(this, settings);
|
94
|
+
});
|
95
|
+
|
96
|
+
$('a[data-remote]').live('ajax:success', function(event, data, status) {
|
97
|
+
setDomUpdater(this);
|
98
|
+
$.domupdater(data);
|
99
|
+
});
|
100
|
+
|
101
|
+
$('form[data-remote]').live('ajax:success', function(event, data, status) {
|
102
|
+
setDomUpdater(this);
|
103
|
+
$.domupdater(data);
|
104
|
+
});
|
105
|
+
|
106
|
+
$('form[data-remote]').live('ajax:error', function(xhr, status, error) {
|
107
|
+
// Server can send 422 to indicate an error and still display validation errors and such
|
108
|
+
// This can be used to further differnetiate between successful and unsuccessful requests
|
109
|
+
if (status.status == 422) {
|
110
|
+
setDomUpdater(this);
|
111
|
+
$.domupdater(status.responseText);
|
112
|
+
}
|
113
|
+
});
|
114
|
+
|
115
|
+
|
116
|
+
var turn_on_conditional_remotes;
|
117
|
+
turn_on_conditional_remotes = function() {
|
118
|
+
return $('form[data-ujsos-if]').each(function(index) {
|
119
|
+
var selector;
|
120
|
+
selector = $(this).attr('data-ujsos-if');
|
121
|
+
return $(selector).each(function(index) {
|
122
|
+
return $(this).attr('data-remote', 'true');
|
123
|
+
});
|
124
|
+
});
|
125
|
+
};
|
126
|
+
$(document).ready(function() {
|
127
|
+
return turn_on_conditional_remotes();
|
128
|
+
});
|
129
|
+
$(document).bind('ajaxComplete', function() {
|
130
|
+
return turn_on_conditional_remotes();
|
131
|
+
});
|
132
|
+
|
133
|
+
});
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Ujsos
|
2
|
+
module DomUpdater
|
3
|
+
class << self
|
4
|
+
def attributes_for_dom_update(remote_options)
|
5
|
+
return {} if remote_options.blank?
|
6
|
+
|
7
|
+
#puts "UUUUUUUUU #{remote_options.inspect}"
|
8
|
+
html_options = {}
|
9
|
+
if (if_option = remote_options[:remote_if])
|
10
|
+
html_options['data-ujsos-if'] = if_option
|
11
|
+
else
|
12
|
+
html_options['data-remote'] = 'true'
|
13
|
+
end
|
14
|
+
|
15
|
+
# support :update and :replace params
|
16
|
+
dom_id = remote_options.delete(:update)
|
17
|
+
if remote_options.is_a?(Hash) # also support extracting the update arg from :remote hash
|
18
|
+
dom_id ||= remote_options[:update]
|
19
|
+
html_options["data-ujsos-update-position"] = remote_options[:position] if remote_options[:position]
|
20
|
+
end
|
21
|
+
|
22
|
+
html_options["data-ujsos-update"] = dom_id if dom_id
|
23
|
+
|
24
|
+
# support :replace param
|
25
|
+
dom_id = remote_options.delete(:replace)
|
26
|
+
html_options["data-ujsos-replace"] = dom_id if dom_id
|
27
|
+
|
28
|
+
# support :remote => { :format => }
|
29
|
+
if remote_options.is_a?(Hash) && (f =remote_options[:format])
|
30
|
+
html_options["data-ujsos-format"] = f
|
31
|
+
end
|
32
|
+
|
33
|
+
#puts "HTML ATTRS: #{html_options.inspect}"
|
34
|
+
html_options
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Ujsos
|
2
|
+
class HtmlfResponder < ::ActionController::Responder
|
3
|
+
def to_htmlf
|
4
|
+
render :partial => controller.action_name
|
5
|
+
rescue ActionView::MissingTemplate => e
|
6
|
+
htmlf_navigation_behavior(e)
|
7
|
+
end
|
8
|
+
|
9
|
+
protected
|
10
|
+
|
11
|
+
# This is the common behavior for "navigation" requests, like :html, :iphone and so forth.
|
12
|
+
def htmlf_navigation_behavior(error)
|
13
|
+
if get?
|
14
|
+
raise error
|
15
|
+
elsif has_errors? && default_action
|
16
|
+
render :partial => default_action.to_s, :status => :unprocessable_entity
|
17
|
+
else
|
18
|
+
render :partial => 'show'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Ujsos
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
initializer "ujsos_rails.add_controller" do
|
4
|
+
config.to_prepare do
|
5
|
+
ApplicationController.send :include, Ujsos::Controller
|
6
|
+
end
|
7
|
+
|
8
|
+
Mime::Type.register_alias "text/html", :htmlf
|
9
|
+
|
10
|
+
# patch view lookups to use html files in case of htmlf as well
|
11
|
+
module ::ActionView
|
12
|
+
class LookupContext
|
13
|
+
module Details
|
14
|
+
alias_method :orig_formats=, :formats=
|
15
|
+
|
16
|
+
def formats=(values)
|
17
|
+
values << :html if !values.nil? && values == [:htmlf]
|
18
|
+
self.orig_formats=(values)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Controller
|
27
|
+
extend ActiveSupport::Concern
|
28
|
+
|
29
|
+
included do
|
30
|
+
self.responder = Ujsos::HtmlfResponder
|
31
|
+
helper_method :ujsos_attrs
|
32
|
+
end
|
33
|
+
|
34
|
+
protected
|
35
|
+
|
36
|
+
def ujsos_attrs(attrs)
|
37
|
+
Ujsos::DomUpdater.attributes_for_dom_update(attrs)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/lib/ujsos.rb
ADDED
data/ujsos.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "ujsos/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "ujsos"
|
7
|
+
s.version = Ujsos::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Christian Seiler"]
|
10
|
+
s.email = ["chr.seiler@gmail.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/ujsos"
|
12
|
+
s.summary = %q{Unobtrusive Javascript on Steroids}
|
13
|
+
s.description = %q{I need to comeup with a description}
|
14
|
+
|
15
|
+
s.rubyforge_project = "ujsos"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ujsos
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Christian Seiler
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-07-30 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: I need to comeup with a description
|
23
|
+
email:
|
24
|
+
- chr.seiler@gmail.com
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- Rakefile
|
35
|
+
- lib/assets/javascripts/jquery_ujsos.js
|
36
|
+
- lib/ujsos.rb
|
37
|
+
- lib/ujsos/dom_updater.rb
|
38
|
+
- lib/ujsos/htmlf_responder.rb
|
39
|
+
- lib/ujsos/rails_engine.rb
|
40
|
+
- lib/ujsos/version.rb
|
41
|
+
- ujsos.gemspec
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://rubygems.org/gems/ujsos
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
hash: 3
|
57
|
+
segments:
|
58
|
+
- 0
|
59
|
+
version: "0"
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project: ujsos
|
72
|
+
rubygems_version: 1.3.7
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Unobtrusive Javascript on Steroids
|
76
|
+
test_files: []
|
77
|
+
|