public
Description: Ground-up rewrite of Beast, a Ruby on Rails forum.
Homepage: http://activereload.lighthouseapp.com/projects/7537-altered-beast/
Clone URL: git://github.com/courtenay/altered_beast.git
make the post js more unobtrusive...  less obtrusive?
technoweenie (author)
Thu Feb 14 10:47:07 -0800 2008
commit  5842d45f5c4a595ed6d93cd82b74c9a1354c41ad
tree    801753855332db7ce4ff00d3afee702d5373ee15
parent  b4e02ee698497f36107de68eff7081c9ee8cd06e
...
50
51
52
53
 
54
55
56
...
60
61
62
63
 
64
65
66
...
50
51
52
 
53
54
55
56
...
60
61
62
 
63
64
65
66
0
@@ -50,7 +50,7 @@ class PostsController < ApplicationController
0
         format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
0
       else
0
         flash[:notice] = 'Post was successfully created.'
0
- format.html { redirect_to(forum_topic_post_path(@forum, @topic, @post)) }
0
+ format.html { redirect_to(forum_topic_post_path(@forum, @topic, @post, :anchor => dom_id(@post))) }
0
         format.xml { render :xml => @post, :status => :created, :location => forum_topic_post_url(@forum, @topic, @post) }
0
       end
0
     end
0
@@ -60,7 +60,7 @@ class PostsController < ApplicationController
0
     respond_to do |format|
0
       if @post.update_attributes(params[:post])
0
         flash[:notice] = 'Post was successfully updated.'
0
- format.html { redirect_to(forum_topic_path(@forum, @topic)) }
0
+ format.html { redirect_to(forum_topic_path(@forum, @topic, :anchor => dom_id(@post))) }
0
         format.xml { head :ok }
0
       else
0
         format.html { render :action => "edit" }
...
1
2
3
4
5
 
 
6
7
8
9
10
11
12
13
14
15
...
29
30
31
32
 
33
34
35
...
1
2
 
 
 
3
4
5
6
7
8
9
10
 
11
12
13
...
27
28
29
 
30
31
32
33
0
@@ -1,15 +1,13 @@
0
 <div id="edit" class="editbox">
0
 <div class="container">
0
- <% remote_form_for :post, :url => forum_topic_post_path(@post.forum, @post.topic, @post),
0
- :html => { :method => :put }, :before => "$('editbox_spinner').show();" do |f| -%>
0
-
0
+ <% form_for :post, :url => forum_topic_post_path(@post.forum, @post.topic, @post),
0
+ :html => { :method => :put, :id => 'edit-form' } do |f| -%>
0
   <table width="100%" border="0" cellpadding="0" cellspacing="0">
0
     <tr>
0
       <td rowspan="2" width="70%">
0
         <%= f.text_area :body, :rows => 10, :id => "edit_post_body", :tabindex => 1 %>
0
       </td>
0
       <td valign="top">
0
-
0
         <%= link_to('delete post'[], forum_topic_post_path(@post.forum, @post.topic, @post, :page => current_page),
0
               :class => "utility", :method => :delete, :confirm => "Delete this post? Are you sure?"[:delete_post_conf]) %>
0
 
0
@@ -29,7 +27,7 @@
0
     <tr>
0
       <td valign="bottom" style="padding-bottom:15px;">
0
         <%= ajax_spinner_for "editbox", "spinner_black.gif" %>
0
- <%= submit_tag 'Save Changes'[], :tabindex => 2 %> or <%= link_to_function('cancel'[], "EditForm.cancel()") %>
0
+ <%= submit_tag 'Save Changes'[], :tabindex => 2 %> or <%= link_to('cancel'[], '#', :id => 'edit-cancel') %>
0
       </td>
0
     </tr>
0
   </table>
...
1
2
3
4
5
6
7
 
8
...
1
 
 
 
 
 
2
3
4
0
@@ -1,6 +1,2 @@
0
 page.replace :edit, :partial => "edit"
0
-page.edit_form.set_reply_id @post.id
0
-page["edit-post-#{@post.id}_spinner"].hide
0
-page.delay(0.25) do
0
- page[:edit_post_body].focus
0
-end
0
\ No newline at end of file
0
+page.post_form.edit_post @post.id
0
\ No newline at end of file
...
106
107
108
109
110
111
112
 
113
114
115
...
144
145
146
147
 
148
149
150
...
174
175
176
177
 
178
179
180
181
182
183
184
185
186
187
...
106
107
108
 
 
 
 
109
110
111
112
...
141
142
143
 
144
145
146
147
...
171
172
173
 
174
175
176
177
178
179
180
 
181
182
183
0
@@ -106,10 +106,7 @@
0
     <p>
0
       <span class="edit">
0
         <%= ajax_spinner_for "edit-post-#{post.id}", "spinner_bounce.gif" %>
0
- <%= link_to_remote('Edit post'[],
0
- {:url => edit_forum_topic_post_path(@forum, @topic, post), :method => :get,
0
- :before => "EditForm.init(#{post.id});", :condition => "!EditForm.isEditing(#{post.id})" },
0
- {:href => edit_forum_topic_post_path(@forum, @topic, post, :page => current_page), :class => "utility"}) %>
0
+ <%= link_to 'Edit post'[], edit_forum_topic_post_path(@forum, @topic, post, :page => current_page), :class => 'utility' %>
0
       </span>
0
     </p>
0
     <% end -%>
0
@@ -144,7 +141,7 @@
0
 </p>
0
 <% else -%>
0
 
0
-<p><%= link_to_function 'Reply to topic'[], "ReplyForm.init()", :class => "utility" %></p>
0
+<p><%= link_to 'Reply to topic'[], "#reply", :class => "utility", :id => 'reply-link' %></p>
0
 
0
 <div id="reply" class="editbox">
0
 <div class="container">
0
@@ -174,14 +171,13 @@
0
     </tr>
0
     <tr>
0
       <td valign="bottom" style="padding-bottom:15px;">
0
- <%= submit_tag "Save Reply"[] %><span class="button_or">or <%= link_to_function 'cancel'[], "$('reply').hide()" %></span>
0
+ <%= submit_tag "Save Reply"[] %><span class="button_or">or <%= link_to 'cancel'[], '#', :id => 'reply-cancel' %></span>
0
      </td>
0
    </tr>
0
   </table>
0
   <% end -%>
0
 </div>
0
 </div>
0
-<%= javascript_tag "$('reply').hide();"%>
0
 <% end %>
0
 <% end %>
0
 
...
1
2
3
4
5
...
18
19
20
21
22
23
24
25
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
28
29
30
 
 
31
 
32
 
 
 
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 
53
54
55
...
58
59
60
61
62
63
64
65
66
 
 
 
67
68
69
70
71
72
73
74
75
 
 
 
76
77
78
79
80
81
82
83
84
 
 
 
 
85
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
88
89
90
91
92
93
94
95
96
97
98
...
 
 
1
2
3
...
16
17
18
 
 
 
 
 
 
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 
 
52
53
54
55
56
57
58
59
60
 
 
 
 
 
61
 
 
 
 
 
 
 
 
 
 
62
63
 
64
65
66
67
...
70
71
72
 
 
 
 
 
 
73
74
75
76
 
 
 
 
 
 
 
 
77
78
79
80
81
82
 
 
 
 
 
 
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
 
 
 
 
 
 
 
 
 
104
105
0
@@ -1,5 +1,3 @@
0
-var Beast = {}
0
-
0
 var TopicForm = {
0
   editNewTitle: function(txtField) {
0
     $('new_topic').innerHTML = (txtField.value.length > 5) ? txtField.value : 'New Topic';
0
@@ -18,38 +16,52 @@ var LoginForm = {
0
   }
0
 }
0
 
0
-var EditForm = {
0
- // show the form
0
- init: function(postId) {
0
- $('edit-post-' + postId + '_spinner').show();
0
- this.clearReplyId();
0
- },
0
+var PostForm = {
0
+ postId: null,
0
+
0
+ reply: Behavior.create({
0
+ onclick:function() {
0
+ PostForm.cancel();
0
+ $('reply').toggle();
0
+ $('post_body').focus();
0
+ }
0
+ }),
0
+
0
+ edit: Behavior.create(Remote.Link, {
0
+ initialize: function($super, postId) {
0
+ this.postId = postId;
0
+ return $super();
0
+ },
0
+ onclick: function($super) {
0
+ $('edit-post-' + this.postId + '_spinner').show();
0
+ PostForm.clearPostId();
0
+ return $super();
0
+ }
0
+ }),
0
+
0
+ cancel: Behavior.create({
0
+ onclick: function() {
0
+ PostForm.clearPostId();
0
+ $('edit').hide()
0
+ $('reply').hide()
0
+ return false;
0
+ }
0
+ }),
0
 
0
   // sets the current post id we're editing
0
- setReplyId: function(postId) {
0
- $('edit').setAttribute('post_id', postId.toString());
0
+ editPost: function(postId) {
0
+ this.postId = postId;
0
     $('post_' + postId + '-row').addClassName('editing');
0
+ $('edit-post-' + postId + '_spinner').hide()
0
     if($('reply')) $('reply').hide();
0
+ this.cancel.attach($('edit-cancel'))
0
+ $('edit-form').observe('submit', function() { $('editbox_spinner').show() })
0
+ setTimeout("$('edit_post_body').focus()", 250)
0
   },
0
-
0
- // clears the current post id
0
- clearReplyId: function() {
0
- var currentId = this.currentReplyId()
0
- if(!currentId || currentId == '') return;
0
 
0
- var row = $('post_' + currentId + '-row');
0
- if(row) row.removeClassName('editing');
0
- $('edit').setAttribute('post_id', '');
0
- },
0
-
0
- // gets the current post id we're editing
0
- currentReplyId: function() {
0
- return $('edit').getAttribute('post_id');
0
- },
0
-
0
   // checks whether we're editing this post already
0
   isEditing: function(postId) {
0
- if (this.currentReplyId() == postId.toString())
0
+ if (PostForm.postId == postId.toString())
0
     {
0
       $('edit').show();
0
       $('edit_post_body').focus();
0
@@ -58,40 +70,35 @@ var EditForm = {
0
     return false;
0
   },
0
 
0
- // close reply, clear current reply id
0
- cancel: function() {
0
- this.clearReplyId();
0
- $('edit').hide()
0
- }
0
-}
0
+ clearPostId: function() {
0
+ var currentId = PostForm.postId;
0
+ if(!currentId) return;
0
 
0
-var ReplyForm = {
0
- // yes, i use setTimeout for a reason
0
- init: function() {
0
- EditForm.cancel();
0
- $('reply').toggle();
0
- $('post_body').focus();
0
- // for Safari which is sometime weird
0
-// setTimeout('$(\"post_body\").focus();',50);
0
+ var row = $('post_' + currentId + '-row');
0
+ if(row) row.removeClassName('editing');
0
+ PostForm.postId = null;
0
   }
0
 }
0
 
0
-Ghost = Behavior.create({
0
- initialize: function() { this.element.hide() }
0
-})
0
-
0
-SearchLink = Behavior.create({
0
- onclick: function() {
0
+Event.addBehavior({
0
+ 'span.time': toTimeAgoInWords,
0
+ '#search, #reply': function() { this.hide() },
0
+ '#search-link:click': function() {
0
     $('search').toggle();
0
     $('search_box').focus();
0
+ return false
0
+ },
0
+
0
+ 'tr.post': function() {
0
+ var postId = this.id.match(/^post_(\d+)-/)[1]
0
+ PostForm.edit.attach(this.down('.edit a'), postId);
0
+ },
0
+
0
+ '#reply-link': function() {
0
+ PostForm.reply.attach(this)
0
+ },
0
+
0
+ '#reply-cancel': function() {
0
+ PostForm.cancel.attach(this)
0
   }
0
-})
0
-
0
-Event.addBehavior({
0
- '#search': Ghost,
0
- '#search-link': SearchLink,
0
- 'span.time': RelativeTime
0
-})
0
-
0
-document.observe("dom:loaded", function() {
0
 })
0
\ No newline at end of file
...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
 
 
 
88
...
70
71
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
74
75
76
77
0
@@ -70,17 +70,6 @@ Date.parseUTC = function(value) {
0
   return new Date(utcSeconds);
0
 }
0
 
0
-var TimeFormatter = Behavior.create({
0
- initialize: function(timeFormat) {
0
- this.timeFormat = timeFormat
0
- this.element.update(this.formatTime(Date.parseUTC(this.element.innerHTML)))
0
- },
0
-
0
- formatTime: function(time) {
0
- return time.strftime(this.timeFormat)
0
- }
0
-});
0
-
0
-var RelativeTime = Behavior.create(TimeFormatter, {
0
- formatTime: function(time) { return time.timeAgoInWords() }
0
-})
0
\ No newline at end of file
0
+function toTimeAgoInWords() {
0
+ this.update(Date.parseUTC(this.innerHTML).timeAgoInWords())
0
+}
0
\ No newline at end of file
...
187
188
189
190
 
191
192
193
...
245
246
247
248
 
249
250
251
...
187
188
189
 
190
191
192
193
...
245
246
247
 
248
249
250
251
0
@@ -187,7 +187,7 @@ describe PostsController, "POST #create" do
0
     end
0
     
0
     it_assigns :post, :forum, :topic, :parent => lambda { @topic }, :flash => { :notice => :not_nil }
0
- it_redirects_to { forum_topic_post_path(@forum, @topic, @post) }
0
+ it_redirects_to { forum_topic_post_path(@forum, @topic, @post, :anchor => "post_#{@post.id}") }
0
   end
0
 
0
   describe PostsController, "(unsuccessful creation)" do
0
@@ -245,7 +245,7 @@ describe PostsController, "PUT #update" do
0
     end
0
     
0
     it_assigns :post, :forum, :topic, :parent => lambda { @topic }, :flash => { :notice => :not_nil }
0
- it_redirects_to { forum_topic_path(@forum, @topic) }
0
+ it_redirects_to { forum_topic_path(@forum, @topic, :anchor => "post_#{@post.id}") }
0
   end
0
 
0
   describe PostsController, "(unsuccessful save)" do

Comments

    No one has commented yet.