radiant-page_attachments-extension 1.0.0 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/HELP.md +27 -0
  2. data/README.md +34 -11
  3. data/VERSION +1 -1
  4. data/app/controllers/admin/page_attachments_controller.rb +46 -0
  5. data/app/helpers/admin/page_attachments_helper.rb +11 -0
  6. data/app/models/page_attachment_tags.rb +13 -6
  7. data/app/models/page_attachments_interface.rb +1 -0
  8. data/app/views/admin/page_attachments/edit.html.haml +21 -0
  9. data/app/views/admin/page_attachments/grid.html.haml +20 -0
  10. data/app/views/admin/page_attachments/index.html.haml +24 -0
  11. data/app/views/admin/pages/_attachment.html.erb +19 -0
  12. data/app/views/admin/pages/_attachment.html.haml +1 -1
  13. data/lib/tasks/page_attachments_extension_tasks.rake +3 -0
  14. data/page_attachments_extension.rb +17 -3
  15. data/public/images/admin/page_attachments/move_higher.png +0 -0
  16. data/public/images/admin/page_attachments/move_lower.png +0 -0
  17. data/public/images/admin/page_attachments/pdf-icon.png +0 -0
  18. data/public/javascripts/admin/lowpro.js +338 -0
  19. data/public/javascripts/admin/page_attachments.js +1 -1
  20. data/public/stylesheets/page_attachments.css +98 -0
  21. data/radiant-page_attachments-extension.gemspec +16 -5
  22. data/vendor/plugins/attachment_fu/README +7 -14
  23. data/vendor/plugins/attachment_fu/amazon_s3.yml.tpl +0 -3
  24. data/vendor/plugins/attachment_fu/install.rb +0 -2
  25. data/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu.rb +9 -18
  26. data/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/file_system_backend.rb +8 -33
  27. data/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/backends/s3_backend.rb +15 -55
  28. data/vendor/plugins/attachment_fu/lib/technoweenie/attachment_fu/processors/rmagick_processor.rb +9 -0
  29. data/vendor/plugins/attachment_fu/test/backends/file_system_test.rb +1 -64
  30. data/vendor/plugins/attachment_fu/test/basic_test.rb +3 -3
  31. data/vendor/plugins/attachment_fu/test/fixtures/attachment.rb +0 -43
  32. data/vendor/plugins/attachment_fu/test/schema.rb +1 -27
  33. metadata +25 -5
  34. data/app/models/observe_page_attachments.rb +0 -5
data/HELP.md ADDED
@@ -0,0 +1,27 @@
1
+ Managing Attachments
2
+ ---
3
+
4
+ When you login and edit a page, you'll find the "Attachments" interface below the
5
+ text editing area. To add a new attachment to the page click the **+** icon. If you
6
+ need to you can upload multiple attachments at once by clicking the **+** icon once
7
+ for each attachment you'll be adding. Attachments are **not** added or deleted until
8
+ the page is saved. Therefore, if you accidentally deleted something you meant to
9
+ keep, simply cancel the page edit.
10
+
11
+ You'll also find a list of all page attachments under the [Attachments](/admin/page_attachments) tab. There
12
+ you'll be able to find a link to each attachment, a link to it's associated page, and
13
+ some sample code for displaying the attachment.
14
+
15
+ Usage
16
+ ---
17
+
18
+ * See the "available tags" documentation built into the Radiant page admin for more details.
19
+ * Reference an attachment by name `<r:attachment name="file.txt">...</r:attachment>`
20
+ * Display an attachment's URL `<r:attachment:url name="file.jpg"/>`
21
+ * Display an attachment's `#{key}` attribute `<r:attachment:#{key} name="file.jpg"/>`
22
+ * Display the date an attachment was added `<r:attachment:date name="file.txt"/>`
23
+ * Display an attached image `<r:attachment:image name="file.jpg"/>`
24
+ * Display a link to an attachment `<r:attachment:link name="file.jpg"/>` or `<r:attachment:link name="file.jpg">Click Here</r:attachment:link>`
25
+ * Display name of the user who added the attachment `<r:attachment:author name="file.jpg"/>`
26
+ * Iterate through all the attachments on a page `<r:attachment:each><r:link/></r:attachment:each>`
27
+ * Display the extension of an attachement inside iterations with `<r:attachment:extension/>`
data/README.md CHANGED
@@ -4,12 +4,18 @@ Page Attachments
4
4
  About
5
5
  ---
6
6
 
7
- A [Radiant][rd] Extension by [Sean Cribbs][sc] that adds page-attachment-style asset management. Page Attachments adds support for file uploads realized as attachments to individual pages. Attachments can have an order via acts_as_list, a title, a description and various metadata fields as provided by AttachmentFu.
7
+ A [Radiant][rd] Extension by [Sean Cribbs][sc] that adds page-attachment-style
8
+ asset management. Page Attachments adds support for file uploads realized as
9
+ attachments to individual pages. Attachments can have an order via acts_as_list,
10
+ a title, a description and various metadata fields as provided by AttachmentFu.
8
11
 
9
12
  Installation
10
13
  ---
11
14
 
12
- If you want `page_attachments` to generate and display thumbnails of your uploaded images you'll first need to install one of, [`image_science`][is], [`mini-magick`][mm] or [`rmagick`][rm] on your server. This is completely optional, `page_attachments` will still function in every other way without any of these packages installed.
15
+ If you want `page_attachments` to generate and display thumbnails of your uploaded
16
+ images you'll first need to install one of, [`image_science`][is], [`mini-magick`][mm]
17
+ or [`rmagick`][rm] on your server. This is completely optional, `page_attachments`
18
+ will still function in every other way without any of these packages installed.
13
19
 
14
20
  Now you're ready to install `page_attachments`.
15
21
 
@@ -58,7 +64,8 @@ If you have a problem running the migrate task, and it fails with an error somet
58
64
  ld: library not found for -lfreeimage
59
65
  collect2: ld returned 1 exit status
60
66
 
61
- It means you have installed the ImageScience gem but you don't have FreeImage installed. So, either install FreeImage or uninstall the gem with
67
+ It means you have installed the ImageScience gem but you don't have FreeImage installed. So,
68
+ either install FreeImage or uninstall the gem with
62
69
 
63
70
  gem uninstall image_science
64
71
 
@@ -68,13 +75,15 @@ If you're using ImageScience as a user without a home directory, you may see thi
68
75
 
69
76
  Define INLINEDIR or HOME in your environment and try again
70
77
 
71
- This is caused by RubyInline not having a place to store its generated files and can be easily fixed by specifying a path in your environment file like so:
78
+ This is caused by RubyInline not having a place to store its generated files and can be
79
+ easily fixed by specifying a path in your environment file like so:
72
80
 
73
81
  ENV['INLINEDIR'] = File.join(RAILS_ROOT,'tmp','ruby_inline')
74
82
 
75
83
  ---
76
84
 
77
- If you have trouble attaching files to Page Types other than the normal type, try editing the following line in your `config/environment.rb` file
85
+ If you have trouble attaching files to Page Types other than the normal type, try editing
86
+ the following line in your `config/environment.rb` file
78
87
 
79
88
  config.extensions = [ :all ]
80
89
 
@@ -85,13 +94,21 @@ to look like
85
94
  Amazon S3 for Attachment storage
86
95
  ---
87
96
 
88
- Since `page_attachments` uses `attachment_fu` for the handling of attachments it's just as easy to use [S3][s3] as it is to use your hard drive. Before you get started with this there are a few things to keep in mind:
97
+ Since `page_attachments` uses `attachment_fu` for the handling of attachments it's just as
98
+ easy to use [S3][s3] as it is to use your hard drive. Before you get started with this there
99
+ are a few things to keep in mind:
89
100
 
90
- * If you've already started storing attachments on your hard drive **this will break** any `<r:attachment...>` tags pointing to those files. You'll need to remove all existing attachments and re-add them to Amazon S3.
91
- * You have to install the `AWS::S3` gem. In some shared hosting environments this might not be possible.
92
- * The `AWS::S3` gem does not (currently) support EU buckets, so if that's all you have you'll need to create a bucket in the US.
101
+ * If you've already started storing attachments on your hard drive **this will break**
102
+ any `<r:attachment...>` tags pointing to those files. You'll need to remove all existing
103
+ attachments and re-add them to Amazon S3.
104
+ * You have to install the `AWS::S3` gem. In some shared hosting environments this might
105
+ not be possible.
106
+ * The `AWS::S3` gem does not (currently) support EU buckets, so if that's all you have
107
+ you'll need to create a bucket in the US.
93
108
 
94
- Before you start make sure you have `page_attachments` working using your hard drive. Once you've tested an upload or two to the hard drive and feel confident the basic setup is working, dive right in.
109
+ Before you start make sure you have `page_attachments` working using your hard drive. Once
110
+ you've tested an upload or two to the hard drive and feel confident the basic setup is
111
+ working, dive right in.
95
112
 
96
113
  1. `gem install aws-s3`
97
114
  2. `cd /path/to/radiant`
@@ -101,7 +118,11 @@ Before you start make sure you have `page_attachments` working using your hard d
101
118
  6. edit line 2 of `vendor/extensions/page_attachments/app/models/page_attachment.rb` changing `:file_system` to `:s3`
102
119
  7. restart your server
103
120
 
104
- Add an attachment and make sure the link it gives back is on S3. You should see all your attachments start showing up at `http://s3.amazonaws.com/bucket-name/page_attachments/`. While it is possible to customize the URL to Amazon (i.e. http://attachments.your-domain.com/) but it's beyond the scope of this document and a task best left for those that really need custom URLs.
121
+ Add an attachment and make sure the link it gives back is on S3. You should see all your
122
+ attachments start showing up at `http://s3.amazonaws.com/bucket-name/page_attachments/`.
123
+ While it is possible to customize the URL to Amazon (i.e. http://attachments.your-domain.com/)
124
+ but it's beyond the scope of this document and a task best left for those that really
125
+ need custom URLs.
105
126
 
106
127
  Contributors
107
128
  ---
@@ -112,6 +133,7 @@ These people have contributed patches that have been added to the extension:
112
133
  * [Daniel Collis-Puro][djcp]
113
134
  * [James Burka][jb]
114
135
  * [Istvan Hoka][ihoka]
136
+ * [Jim Gay][sf]
115
137
  * [Oleg Ivanov][oleg]
116
138
 
117
139
  [rd]: http://radiantcms.org/
@@ -126,4 +148,5 @@ These people have contributed patches that have been added to the extension:
126
148
  [djcp]: http://www.kookdujour.com/
127
149
  [jb]: http://github.com/jjburka
128
150
  [ihoka]: http://github.com/ihoka
151
+ [sf]: http://www.saturnflyer.com
129
152
  [oleg]: http://github.com/morhekil
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.0.2
@@ -0,0 +1,46 @@
1
+ class Admin::PageAttachmentsController < ApplicationController
2
+
3
+ def index
4
+ @attachments = PageAttachment.paginate :per_page => 25, :page => params[:page], :conditions => {:parent_id => nil}, :order => 'title, filename'
5
+ end
6
+ def grid
7
+ @attachments = PageAttachment.paginate :per_page => 25, :page => params[:page], :conditions => {:parent_id => nil}, :order => 'title, filename'
8
+ end
9
+
10
+ def edit
11
+ @page_attachment = PageAttachment.find(params[:id])
12
+ end
13
+ def update
14
+ @page_attachment = PageAttachment.find(params[:id])
15
+ if @page_attachment.update_attributes(params[:page_attachment])
16
+ redirect_to admin_page_attachments_url
17
+ else
18
+ render :edit
19
+ end
20
+ end
21
+
22
+ def move_higher
23
+ if request.post?
24
+ @attachment = PageAttachment.find(params[:id])
25
+ @attachment.move_higher
26
+ render :partial => 'admin/page/attachment', :layout => false, :collection => @attachment.page.attachments
27
+ end
28
+ end
29
+
30
+ def move_lower
31
+ if request.post?
32
+ @attachment = PageAttachment.find(params[:id])
33
+ @attachment.move_lower
34
+ render :partial => 'admin/page/attachment', :layout => false, :collection => @attachment.page.attachments
35
+ end
36
+ end
37
+
38
+ def destroy
39
+ if request.post?
40
+ @attachment = PageAttachment.find(params[:id])
41
+ page = @attachment.page
42
+ @attachment.destroy
43
+ render :partial => 'admin/page/attachment', :layout => false, :collection => page.attachments
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,11 @@
1
+ module Admin::PageAttachmentsHelper
2
+ def preview_path(attachment)
3
+ case attachment.filename
4
+ when /pdf$/
5
+ attachment_path = '/images/admin/page_attachments/pdf-icon.png'
6
+ else
7
+ attachment_path = attachment.public_filename
8
+ end
9
+ attachment_path
10
+ end
11
+ end
@@ -7,15 +7,22 @@ module PageAttachmentTags
7
7
  The namespace for referencing page attachments/files. You may specify the 'name'
8
8
  attribute (for the filename) on this tag for all contained tags to refer to that attachment.
9
9
  Attachments can be inherited from parent pages.
10
-
10
+
11
+ You may also define a url where you would like to find an attachment. By default the tag
12
+ will look to the current page for it's attachments.
13
+
11
14
  *Usage*:
12
-
13
- <pre><code><r:attachment name="file.txt">...</r:attachment></code></pre>
15
+
16
+ <pre><code><r:attachment name="file.txt" [url="/other/location"]>...</r:attachment></code></pre>
14
17
  }
15
18
  tag "attachment" do |tag|
16
- page = tag.locals.page
17
- tag.locals.attachment = page.attachment(tag.attr['name']) rescue nil if tag.attr['name']
18
- tag.expand
19
+ scope_url = tag.attr['url'] || tag.locals.page.url
20
+ if page = Page.find_by_url(scope_url)
21
+ tag.locals.attachment = page.attachment(tag.attr['name']) rescue nil if tag.attr['name']
22
+ tag.expand
23
+ else
24
+ raise TagError, "'url' attribute must set be for an existing page"
25
+ end
19
26
  end
20
27
 
21
28
  desc %{
@@ -11,6 +11,7 @@ module PageAttachmentsInterface
11
11
  def add_page_attachment_partials
12
12
  @buttons_partials ||= []
13
13
  @buttons_partials << "attachments_box"
14
+ include_javascript 'admin/lowpro'
14
15
  include_javascript 'admin/dragdrop'
15
16
  include_javascript 'admin/page_attachments'
16
17
  include_stylesheet 'admin/page_attachments'
@@ -0,0 +1,21 @@
1
+ %h1 Edit Page Attachment
2
+ - form_for :page_attachment, @page_attachment, :url => admin_page_attachment_path(@page_attachment), :html => {:method => :put, :multipart => true} do |form|
3
+ .form-area
4
+ %p.title
5
+ = form.label :title
6
+ = form.text_field :title, :class => 'textbox', :maxlength => 255
7
+ %p.title
8
+ = form.label :description
9
+ = form.text_field :description, :class => 'textbox', :maxlength => 255
10
+ %p.buttons
11
+ = form.submit "Save"
12
+ or
13
+ = link_to 'Cancel', admin_page_attachments_url
14
+ %p
15
+ = image_tag "admin/minus.png", :alt => "Delete", :class => 'delete'
16
+ - unless @page_attachment.thumbnails.empty?
17
+ = link_to image_tag(@page_attachment.public_filename("icon")), @page_attachment.public_filename, :class => 'thumbnail'
18
+ - unless @page_attachment.title.blank?
19
+ %p
20
+ = h @page_attachment.short_title
21
+ = link_to @page_attachment.short_filename, @page_attachment.public_filename
@@ -0,0 +1,20 @@
1
+ %h1 Page Attachments
2
+ %p Below you'll find a list of all page attachments on your site. You may click to view the attachment file, click to edit the attached page, or use the sample code as a starting point for displaying a particular file on a page.
3
+ %p
4
+ See the
5
+ = link_to('List view', admin_page_attachments_path)
6
+ - unless @attachments.blank?
7
+ %ul.gridded
8
+ - @attachments.each do |attachment|
9
+ %li
10
+ .sample= link_to(image_tag(preview_path(attachment)), attachment.public_filename)
11
+ .pageTitle= (attachment.title.blank? ? attachment.filename : attachment.title)
12
+ .pageTitle= link_to attachment.page.title, page_edit_url(attachment.page)
13
+ %span
14
+ - if attachment.filename.match(/\.(jpg|gif|png|jpeg|tiff?)$/)
15
+ - code = %{<r:attachment name="#{attachment.filename}" url="#{attachment.page.url}"><r:image /></r:attachment>}
16
+ - else
17
+ - code = %{<r:attachment name="#{attachment.filename}" url="#{attachment.page.url}"><r:link /></r:attachment>}
18
+ %textarea{:cols => 22, :rows => 4}= code
19
+ = will_paginate @attachments
20
+ - include_stylesheet 'admin/page_attachments'
@@ -0,0 +1,24 @@
1
+ %h1 Page Attachments
2
+ %p Below you'll find a list of all page attachments on your site. You may click to view the attachment file, click to edit the attached page, or use the sample code as a starting point for displaying a particular file on a page.
3
+ / %p
4
+ / Try the
5
+ / = link_to('Grid view', admin_page_attachments_grid_path)
6
+ %table.index
7
+ %tr
8
+ %th Attachment
9
+ %th Page
10
+ %th Code
11
+ - unless @attachments.blank?
12
+ - @attachments.each do |attachment|
13
+ %tr
14
+ %td
15
+ = link_to((attachment.title.blank? ? attachment.filename : attachment.title), edit_admin_page_attachment_path(attachment))
16
+ = link_to('(view)', attachment.filename)
17
+ %td= link_to attachment.page.title, page_edit_url(attachment.page)
18
+ %td
19
+ - if attachment.filename.match(/\.(jpg|gif|png|jpeg|tiff?)$/)
20
+ - code = %{<r:attachment name="#{attachment.filename}" url="#{attachment.page.url}"><r:image /></r:attachment>}
21
+ - else
22
+ - code = %{<r:attachment name="#{attachment.filename}" url="#{attachment.page.url}"><r:link /></r:attachment>}
23
+ %input{:type => 'text', :size => 40, :value => code}
24
+ = will_paginate @attachments
@@ -0,0 +1,19 @@
1
+ <li id="attachment_<%= attachment.id %>" class="attachment clearfix">
2
+ <div>
3
+ <%= image_tag "admin/minus.png", :alt => "Delete", :class => 'delete' %>
4
+ <% unless attachment.first? %>
5
+ <%= image_tag "admin/page_attachments/move_higher.png", :alt => "Move Higher", :class => 'higher' %>
6
+ <% end %>
7
+ <% unless attachment.last? %>
8
+ <%= image_tag "admin/page_attachments/move_lower.png", :alt => "Move Lower", :class => 'lower' %>
9
+ <% end %>
10
+ </div>
11
+ <% unless attachment.thumbnails.empty? %>
12
+ <%= link_to image_tag(attachment.public_filename("icon")),
13
+ attachment.public_filename, :class => 'thumbnail' %>
14
+ <% end %>
15
+ <% unless attachment.title.blank? %>
16
+ <p><%= h attachment.short_title %></p>
17
+ <% end %>
18
+ <%= link_to attachment.short_filename, attachment.public_filename %>
19
+ </li>
@@ -1,6 +1,6 @@
1
1
  %li{:id => "attachment_#{attachment.id}", :class =>"attachment clearfix"}
2
2
  = hidden_field_tag "page[attachments_attributes][#{attachment_counter}][id]", attachment.id
3
- = hidden_field_tag "page[attachments_attributes][#{attachment_counter}][_delete]", "0"
3
+ = hidden_field_tag "page[attachments_attributes][#{attachment_counter}][_destroy]", "0"
4
4
  = hidden_field_tag "page[attachments_attributes][#{attachment_counter}][position]", attachment.position
5
5
  - unless attachment.first? && attachment.last?
6
6
  = image_tag 'admin/drag_order.png', :alt => "Drag handle", :title => "Drag to change order", :class => 'drag_order'
@@ -1,6 +1,9 @@
1
1
  namespace :radiant do
2
2
  namespace :extensions do
3
3
  namespace :page_attachments do
4
+
5
+ desc "Runs the migrate and update tasks"
6
+ task :install => [:environment, :migrate, :update]
4
7
 
5
8
  desc "Runs the migration of the Page Attachments extension"
6
9
  task :migrate => :environment do
@@ -3,21 +3,35 @@ require_dependency 'application_controller'
3
3
  # require 'tempfile'
4
4
 
5
5
  class PageAttachmentsExtension < Radiant::Extension
6
- version "1.0"
6
+ version "1.0.2"
7
7
  description "Adds page-attachment-style asset management."
8
8
  url "http://radiantcms.org"
9
+
10
+ extension_config do |config|
11
+ config.gem 'will_paginate'
12
+ end
9
13
 
10
14
  define_routes do |map|
11
- map.connect 'page_attachments/:action/:id', :controller => 'page_attachments'
15
+ map.namespace :admin do |admin|
16
+ admin.resources :page_attachments
17
+ admin.page_attachments_grid '/page_attachments_grid', :controller => 'page_attachments', :action => 'grid'
18
+ end
12
19
  end
13
20
 
14
21
  def activate
22
+ if self.respond_to?(:tab)
23
+ tab "Attachments" do
24
+ add_item 'List', "/admin/page_attachments"
25
+ end
26
+ else
27
+ admin.tabs.add 'Attachments', '/admin/page_attachments', :after => "Layouts", :visibility => [:admin]
28
+ end
15
29
  # Regular page attachments stuff
16
30
  Page.class_eval {
17
31
  include PageAttachmentAssociations
18
32
  include PageAttachmentTags
19
33
  }
20
- UserActionObserver.send :include, ObservePageAttachments
34
+ UserActionObserver.instance.send :add_observer!, PageAttachment
21
35
  Admin::PagesController.send :include, PageAttachmentsInterface
22
36
  end
23
37
 
@@ -0,0 +1,338 @@
1
+ LowPro = {};
2
+ LowPro.Version = '0.5';
3
+ LowPro.CompatibleWithPrototype = '1.6';
4
+
5
+ if (Prototype.Version.indexOf(LowPro.CompatibleWithPrototype) != 0 && window.console && window.console.warn)
6
+ console.warn("This version of Low Pro is tested with Prototype " + LowPro.CompatibleWithPrototype +
7
+ " it may not work as expected with this version (" + Prototype.Version + ")");
8
+
9
+ if (!Element.addMethods)
10
+ Element.addMethods = function(o) { Object.extend(Element.Methods, o) };
11
+
12
+ // Simple utility methods for working with the DOM
13
+ DOM = {};
14
+
15
+ // DOMBuilder for prototype
16
+ DOM.Builder = {
17
+ tagFunc : function(tag) {
18
+ return function() {
19
+ var attrs, children;
20
+ if (arguments.length>0) {
21
+ if (arguments[0].constructor == Object) {
22
+ attrs = arguments[0];
23
+ children = Array.prototype.slice.call(arguments, 1);
24
+ } else {
25
+ children = arguments;
26
+ };
27
+ children = $A(children).flatten()
28
+ }
29
+ return DOM.Builder.create(tag, attrs, children);
30
+ };
31
+ },
32
+ create : function(tag, attrs, children) {
33
+ attrs = attrs || {}; children = children || []; tag = tag.toLowerCase();
34
+ var el = new Element(tag, attrs);
35
+
36
+ for (var i=0; i<children.length; i++) {
37
+ if (typeof children[i] == 'string')
38
+ children[i] = document.createTextNode(children[i]);
39
+ el.appendChild(children[i]);
40
+ }
41
+ return $(el);
42
+ }
43
+ };
44
+
45
+ // Automatically create node builders as $tagName.
46
+ (function() {
47
+ var els = ("p|div|span|strong|em|img|table|tr|td|th|thead|tbody|tfoot|pre|code|" +
48
+ "h1|h2|h3|h4|h5|h6|ul|ol|li|form|input|textarea|legend|fieldset|" +
49
+ "select|option|blockquote|cite|br|hr|dd|dl|dt|address|a|button|abbr|acronym|" +
50
+ "script|link|style|bdo|ins|del|object|param|col|colgroup|optgroup|caption|" +
51
+ "label|dfn|kbd|samp|var").split("|");
52
+ var el, i=0;
53
+ while (el = els[i++])
54
+ window['$' + el] = DOM.Builder.tagFunc(el);
55
+ })();
56
+
57
+ DOM.Builder.fromHTML = function(html) {
58
+ var root;
59
+ if (!(root = arguments.callee._root))
60
+ root = arguments.callee._root = document.createElement('div');
61
+ root.innerHTML = html;
62
+ return root.childNodes[0];
63
+ };
64
+
65
+
66
+
67
+ // Wraps the 1.6 contentloaded event for backwards compatibility
68
+ //
69
+ // Usage:
70
+ //
71
+ // Event.onReady(callbackFunction);
72
+ Object.extend(Event, {
73
+ onReady : function(f) {
74
+ if (document.body) f();
75
+ else document.observe('dom:loaded', f);
76
+ }
77
+ });
78
+
79
+ // Based on event:Selectors by Justin Palmer
80
+ // http://encytemedia.com/event-selectors/
81
+ //
82
+ // Usage:
83
+ //
84
+ // Event.addBehavior({
85
+ // "selector:event" : function(event) { /* event handler. this refers to the element. */ },
86
+ // "selector" : function() { /* runs function on dom ready. this refers to the element. */ }
87
+ // ...
88
+ // });
89
+ //
90
+ // Multiple calls will add to exisiting rules. Event.addBehavior.reassignAfterAjax and
91
+ // Event.addBehavior.autoTrigger can be adjusted to needs.
92
+ Event.addBehavior = function(rules) {
93
+ var ab = this.addBehavior;
94
+ Object.extend(ab.rules, rules);
95
+
96
+ if (!ab.responderApplied) {
97
+ Ajax.Responders.register({
98
+ onComplete : function() {
99
+ if (Event.addBehavior.reassignAfterAjax)
100
+ setTimeout(function() { ab.reload() }, 10);
101
+ }
102
+ });
103
+ ab.responderApplied = true;
104
+ }
105
+
106
+ if (ab.autoTrigger) {
107
+ this.onReady(ab.load.bind(ab, rules));
108
+ }
109
+
110
+ };
111
+
112
+ Event.delegate = function(rules) {
113
+ return function(e) {
114
+ var element = $(e.element());
115
+ for (var selector in rules)
116
+ if (element.match(selector)) return rules[selector].apply(this, $A(arguments));
117
+ }
118
+ }
119
+
120
+ Object.extend(Event.addBehavior, {
121
+ rules : {}, cache : [],
122
+ reassignAfterAjax : false,
123
+ autoTrigger : true,
124
+
125
+ load : function(rules) {
126
+ for (var selector in rules) {
127
+ var observer = rules[selector];
128
+ var sels = selector.split(',');
129
+ sels.each(function(sel) {
130
+ var parts = sel.split(/:(?=[a-z]+$)/), css = parts[0], event = parts[1];
131
+ $$(css).each(function(element) {
132
+ if (event) {
133
+ var wrappedObserver = Event.addBehavior._wrapObserver(observer);
134
+ $(element).observe(event, wrappedObserver);
135
+ Event.addBehavior.cache.push([element, event, wrappedObserver]);
136
+ } else {
137
+ if (!element.$$assigned || !element.$$assigned.include(observer)) {
138
+ if (observer.attach) observer.attach(element);
139
+
140
+ else observer.call($(element));
141
+ element.$$assigned = element.$$assigned || [];
142
+ element.$$assigned.push(observer);
143
+ }
144
+ }
145
+ });
146
+ });
147
+ }
148
+ },
149
+
150
+ unload : function() {
151
+ this.cache.each(function(c) {
152
+ Event.stopObserving.apply(Event, c);
153
+ });
154
+ this.cache = [];
155
+ },
156
+
157
+ reload: function() {
158
+ var ab = Event.addBehavior;
159
+ ab.unload();
160
+ ab.load(ab.rules);
161
+ },
162
+
163
+ _wrapObserver: function(observer) {
164
+ return function(event) {
165
+ if (observer.call(this, event) === false) event.stop();
166
+ }
167
+ }
168
+
169
+ });
170
+
171
+ Event.observe(window, 'unload', Event.addBehavior.unload.bind(Event.addBehavior));
172
+
173
+ // A silly Prototype style shortcut for the reckless
174
+ $$$ = Event.addBehavior.bind(Event);
175
+
176
+ // Behaviors can be bound to elements to provide an object orientated way of controlling elements
177
+ // and their behavior. Use Behavior.create() to make a new behavior class then use attach() to
178
+ // glue it to an element. Each element then gets it's own instance of the behavior and any
179
+ // methods called onxxx are bound to the relevent event.
180
+ //
181
+ // Usage:
182
+ //
183
+ // var MyBehavior = Behavior.create({
184
+ // onmouseover : function() { this.element.addClassName('bong') }
185
+ // });
186
+ //
187
+ // Event.addBehavior({ 'a.rollover' : MyBehavior });
188
+ //
189
+ // If you need to pass additional values to initialize use:
190
+ //
191
+ // Event.addBehavior({ 'a.rollover' : MyBehavior(10, { thing : 15 }) })
192
+ //
193
+ // You can also use the attach() method. If you specify extra arguments to attach they get passed to initialize.
194
+ //
195
+ // MyBehavior.attach(el, values, to, init);
196
+ //
197
+ // Finally, the rawest method is using the new constructor normally:
198
+ // var draggable = new Draggable(element, init, vals);
199
+ //
200
+ // Each behaviour has a collection of all its instances in Behavior.instances
201
+ //
202
+ var Behavior = {
203
+ create: function() {
204
+ var parent = null, properties = $A(arguments);
205
+ if (Object.isFunction(properties[0]))
206
+ parent = properties.shift();
207
+
208
+ var behavior = function() {
209
+ if (!this.initialize) {
210
+ var args = $A(arguments);
211
+
212
+ return function() {
213
+ var initArgs = [this].concat(args);
214
+ behavior.attach.apply(behavior, initArgs);
215
+ };
216
+ } else {
217
+ var args = (arguments.length == 2 && arguments[1] instanceof Array) ?
218
+ arguments[1] : Array.prototype.slice.call(arguments, 1);
219
+
220
+ this.element = $(arguments[0]);
221
+ this.initialize.apply(this, args);
222
+ behavior._bindEvents(this);
223
+ behavior.instances.push(this);
224
+ }
225
+ };
226
+
227
+ Object.extend(behavior, Class.Methods);
228
+ Object.extend(behavior, Behavior.Methods);
229
+ behavior.superclass = parent;
230
+ behavior.subclasses = [];
231
+ behavior.instances = [];
232
+
233
+ if (parent) {
234
+ var subclass = function() { };
235
+ subclass.prototype = parent.prototype;
236
+ behavior.prototype = new subclass;
237
+ parent.subclasses.push(behavior);
238
+ }
239
+
240
+ for (var i = 0; i < properties.length; i++)
241
+ behavior.addMethods(properties[i]);
242
+
243
+ if (!behavior.prototype.initialize)
244
+ behavior.prototype.initialize = Prototype.emptyFunction;
245
+
246
+ behavior.prototype.constructor = behavior;
247
+
248
+ return behavior;
249
+ },
250
+ Methods : {
251
+ attach : function(element) {
252
+ return new this(element, Array.prototype.slice.call(arguments, 1));
253
+ },
254
+ _bindEvents : function(bound) {
255
+ for (var member in bound) {
256
+ var matches = member.match(/^on(.+)/);
257
+ if (matches && typeof bound[member] == 'function')
258
+ bound.element.observe(matches[1], Event.addBehavior._wrapObserver(bound[member].bindAsEventListener(bound)));
259
+ }
260
+ }
261
+ }
262
+ };
263
+
264
+
265
+
266
+ Remote = Behavior.create({
267
+ initialize: function(options) {
268
+ if (this.element.nodeName == 'FORM') new Remote.Form(this.element, options);
269
+ else new Remote.Link(this.element, options);
270
+ }
271
+ });
272
+
273
+ Remote.Base = {
274
+ initialize : function(options) {
275
+ this.options = Object.extend({
276
+ evaluateScripts : true
277
+ }, options || {});
278
+
279
+ this._bindCallbacks();
280
+ },
281
+ _makeRequest : function(options) {
282
+ if (options.update) new Ajax.Updater(options.update, options.url, options);
283
+ else new Ajax.Request(options.url, options);
284
+ return false;
285
+ },
286
+ _bindCallbacks: function() {
287
+ $w('onCreate onComplete onException onFailure onInteractive onLoading onLoaded onSuccess').each(function(cb) {
288
+ if (Object.isFunction(this.options[cb]))
289
+ this.options[cb] = this.options[cb].bind(this);
290
+ }.bind(this));
291
+ }
292
+ }
293
+
294
+ Remote.Link = Behavior.create(Remote.Base, {
295
+ onclick : function() {
296
+ var options = Object.extend({ url : this.element.href, method : 'get' }, this.options);
297
+ return this._makeRequest(options);
298
+ }
299
+ });
300
+
301
+
302
+ Remote.Form = Behavior.create(Remote.Base, {
303
+ onclick : function(e) {
304
+ var sourceElement = e.element();
305
+
306
+ if (['input', 'button'].include(sourceElement.nodeName.toLowerCase()) &&
307
+ sourceElement.type == 'submit')
308
+ this._submitButton = sourceElement;
309
+ },
310
+ onsubmit : function() {
311
+ var options = Object.extend({
312
+ url : this.element.action,
313
+ method : this.element.method || 'get',
314
+ parameters : this.element.serialize({ submit: this._submitButton.name })
315
+ }, this.options);
316
+ this._submitButton = null;
317
+ return this._makeRequest(options);
318
+ }
319
+ });
320
+
321
+ Observed = Behavior.create({
322
+ initialize : function(callback, options) {
323
+ this.callback = callback.bind(this);
324
+ this.options = options || {};
325
+ this.observer = (this.element.nodeName == 'FORM') ? this._observeForm() : this._observeField();
326
+ },
327
+ stop: function() {
328
+ this.observer.stop();
329
+ },
330
+ _observeForm: function() {
331
+ return (this.options.frequency) ? new Form.Observer(this.element, this.options.frequency, this.callback) :
332
+ new Form.EventObserver(this.element, this.callback);
333
+ },
334
+ _observeField: function() {
335
+ return (this.options.frequency) ? new Form.Element.Observer(this.element, this.options.frequency, this.callback) :
336
+ new Form.Element.EventObserver(this.element, this.callback);
337
+ }
338
+ });