-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rails dojo helpers, first useful code
- Loading branch information
Travis Tilley
committed
Mar 2, 2009
1 parent
a82b87c
commit c40ca17
Showing
6 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
Rails Dojo Helpers | ||
================== | ||
|
||
This plugin is not even close to complete and functionality is being fleshed | ||
out as I use it, or as members of my team need it. If you would like to fork | ||
this project and contribute, I would be -ecstatic-. ^\_^ | ||
|
||
Default Options and Behavior | ||
---------------------------- | ||
|
||
All helpers attempt to be sane in regards to default options, and determining | ||
when certain defaults are appropriate. This should hopefully make things | ||
easier for people unfamiliar with, or still learning, the dojo framework. | ||
These "defaults" include using dojox.dijit.ContentPane instead of the version | ||
in dijit proper due to how common it is to need script and style parsing. | ||
|
||
### All ### | ||
|
||
* Any dijit with an id set will also have jsId set to the same value. This | ||
will create a global javascript variable with the same name as the ID that | ||
references the widget. This can be disabled by setting option 'global' to | ||
false. | ||
|
||
* By default, all helpers that don't make most sense with another tag will | ||
be contained by a div. This can be changed with option 'tag'. | ||
|
||
### Form Fields ### | ||
|
||
* The form helpers will attempt to set alt and title to something sane where | ||
possible, if they are not already set. | ||
|
||
* All fields will scroll on focus so that they are not outside the viewport | ||
when tabbed to. | ||
|
||
* intermediateChanges defaults to true. | ||
|
||
#### Text Fields #### | ||
|
||
* If a field is required, or the 'validation' option is set, a | ||
ValidationTextBox will be used in place of a TextBox widget and the prompt | ||
message will default to the element's title (which should also default to | ||
the capitalized name of the field when used in scoped helpers). | ||
|
||
* Text fields are automatically trimmed for whitespace. | ||
|
||
Form Helpers | ||
------------ | ||
|
||
Since my first real use of the helpers is for forms, I have first implemented | ||
rails-compatible form helpers. This includes bare versions, as well as scoped | ||
versions for use within a form\_for or fields\_for block. | ||
|
||
* dijit\_form\_for | ||
|
||
* dijit\_text\_field | ||
|
||
* dijit\_password\_field | ||
|
||
* dijit\_text\_area | ||
|
||
* dijit\_check\_box | ||
|
||
* dijit\_check\_box\_tag | ||
|
||
* dijit\_select | ||
|
||
* dijit\_button | ||
|
||
* dijit\_submit\_button | ||
|
||
* dijit\_reset\_button |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'dojo_helpers' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require 'dojo_helpers/dijit' | ||
require 'dojo_helpers/dijit/form' | ||
require 'dojo_helpers/dijit/layout' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
module DojoHelpers | ||
module Dijit | ||
def dijit_content(content_or_html_options_with_block = nil, html_options_or_options_with_block = {}, options = {}, &block) | ||
if block_given? | ||
html_options = content_or_html_options_with_block if content_or_html_options_with_block.is_a?(Hash) | ||
options = html_options_or_options_with_block | ||
content = capture(&block) | ||
else | ||
content = content_or_html_options_with_block | ||
html_options = html_options_or_options_with_block | ||
end | ||
|
||
::DojoHelpers::Dijit.core_options(html_options, options) | ||
|
||
unless html_options['dojoType'] | ||
html_options.reverse_merge!({ | ||
'dojoType' => 'dijit.layout.ContentPane', | ||
'parseOnLoad' => true, | ||
'extractContent' => false, | ||
'preventCache' => true, | ||
'preLoad' => false, | ||
'refreshOnShow' => false | ||
}) | ||
|
||
unless not options['dojox_content_pane'] | ||
html_options['dojoType'] = 'dojox.layout.ContentPane' | ||
html_options.reverse_merge!({ | ||
'adjustPaths' => false, | ||
'cleanContent' => false, | ||
'renderStyles' => true, | ||
'executeScripts' => true | ||
}) | ||
end | ||
end | ||
|
||
dijit_content = content_tag(options['tag'], content, html_options) | ||
|
||
if block_given? && block_is_within_action_view?(block) | ||
concat(dijit_content, block.binding) | ||
else | ||
dijit_content | ||
end | ||
end | ||
|
||
class << self | ||
def get_options(html_options = {}, options = nil) | ||
html_options.stringify_keys! | ||
options ||= {} | ||
|
||
options_from_html_options = html_options.delete('dijit') | ||
options.reverse_merge!(options_from_html_options) if options_from_html_options | ||
|
||
[html_options, options] | ||
end | ||
|
||
def core_options(html_options = {}, options = nil) | ||
html_options, options = get_options(html_options, options) | ||
|
||
options.reverse_merge!({ | ||
'global' => true, | ||
'tag' => 'div' | ||
}) | ||
|
||
html_options['jsId'] = html_options['id'] if options['global'] | ||
end | ||
|
||
def form_options(html_options = {}, options = {}) | ||
core_options(html_options, options) | ||
|
||
html_options.reverse_merge!({ | ||
'alt' => html_options['title'] || '', | ||
'disabled' => false, | ||
'readOnly' => false, | ||
'intermediateChanges' => true, | ||
'scrollOnFocus' => true | ||
}) | ||
end | ||
|
||
def textbox_options(html_options = {}, options_or_method = nil, method = nil) | ||
method ||= options_or_method if options_or_method.is_a?(String) | ||
options = options_or_method if options_or_method.is_a?(Hash) | ||
|
||
form_options(html_options, options) | ||
|
||
textbox_options = { | ||
'trim' => true, | ||
'tooltipPosition' => "before", | ||
'title' => html_options['title'] || method.to_s.capitalize.sub('_', '') | ||
} | ||
|
||
if html_options['required'] || html_options.delete('validation') | ||
textbox_options['dojoType'] = 'dijit.form.ValidationTextBox' | ||
textbox_options['promptMessage'] = textbox_options['title'] | ||
else | ||
textbox_options['dojoType'] = 'dijit.form.TextBox' | ||
end | ||
|
||
html_options.reverse_merge!(textbox_options) | ||
html_options['alt'] = textbox_options['title'] if html_options['alt'].blank? | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActionView::Base.class_eval {include DojoHelpers::Dijit} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
module DojoHelpers | ||
module Dijit | ||
module Form | ||
def dijit_form_for(record_or_name_or_array, *args, &proc) | ||
html_options = args.extract_options! | ||
::DojoHelpers::Dijit.form_options(html_options) | ||
form_for(record_or_name_or_array, *(args << html_options), &proc) | ||
end | ||
|
||
def dijit_text_field(object_name, method, html_options = {}) | ||
::DojoHelpers::Dijit.textbox_options(html_options, method) | ||
text_field(object_name, method, html_options) | ||
end | ||
|
||
def dijit_password_field(object_name, method, html_options = {}) | ||
::DojoHelpers::Dijit.textbox_options(html_options, method) | ||
password_field(object_name, method, html_options) | ||
end | ||
|
||
def dijit_text_area(object_name, method, html_options = {}) | ||
::DojoHelpers::Dijit.form_options(html_options) | ||
html_options['dojoType'] ||= 'dijit.form.TextArea' | ||
text_area(object_name, method, html_options) | ||
end | ||
|
||
def dijit_check_box(object_name, method, html_options = {}, checked_value = "1", unchecked_value = "0") | ||
::DojoHelpers::Dijit.form_options(html_options) | ||
html_options['dojoType'] ||= 'dijit.form.CheckBox' | ||
check_box(object_name, method, html_options, checked_value, unchecked_value) | ||
end | ||
|
||
def dijit_check_box_tag(name, value = "1", checked = false, html_options = {}) | ||
::DojoHelpers::Dijit.form_options(html_options) | ||
html_options['dojoType'] ||= 'dijit.form.CheckBox' | ||
check_box_tag(name, value, checked, html_options) | ||
end | ||
|
||
def dijit_select(object, method, choices, select_options = {}, html_options = {}) | ||
::DojoHelpers::Dijit.form_options(html_options) | ||
html_options.reverse_merge!({ | ||
'dojoType' => 'dijit.form.FilteringSelect', | ||
'autoComplete' => true, | ||
'highlightMatch' => 'all', | ||
'ignoreCase' => true, | ||
'hasDownArrow' => true | ||
}) | ||
select(object, method, choices, select_options, html_options) | ||
end | ||
|
||
def dijit_button(content = '', html_options = {}, options = {}) | ||
options['tag'] ||= 'button' | ||
::DojoHelpers::Dijit.form_options(html_options, options) | ||
|
||
if content.blank? and not html_options['iconClass'] | ||
raise("a dijit button must have content or an iconClass set") | ||
end | ||
|
||
html_options.reverse_merge!({ | ||
'dojoType' => 'dijit.form.Button', | ||
'type' => 'button', | ||
'showLabel' => content.blank? ? false : true | ||
}) | ||
|
||
dijit_content(content, html_options, options) | ||
end | ||
|
||
def dijit_submit_button(content = '', html_options = {}, options = {}) | ||
html_options['type'] ||= 'submit' | ||
dijit_button(content, html_options, options) | ||
end | ||
|
||
def dijit_reset_button(content = '', html_options = {}, options = {}) | ||
html_options['type'] ||= 'reset' | ||
dijit_button(content, html_options, options) | ||
end | ||
end | ||
end | ||
end | ||
|
||
ActionView::Base.class_eval {include DojoHelpers::Dijit::Form} | ||
|
||
module ActionView | ||
module Helpers | ||
class FormBuilder | ||
def dijit_text_field(method, options = {}) | ||
@template.dijit_text_field(@object_name, method, options.merge(:object => @object)) | ||
end | ||
|
||
def dijit_password_field(method, options = {}) | ||
@template.dijit_password_field(@object_name, method, options.merge(:object => @object)) | ||
end | ||
|
||
def dijit_text_area(method, options = {}) | ||
@template.dijit_text_area(@object_name, method, options.merge(:object => @object)) | ||
end | ||
|
||
def dijit_check_box(method, options = {}, checked_value = "1", unchecked_value = "0") | ||
@template.dijit_check_box(@object_name, method, options.merge(:object => @object), checked_value, unchecked_value) | ||
end | ||
|
||
def dijit_select(method, choices, options = {}, html_options = {}) | ||
@template.dijit_select(@object_name, method, choices, options.merge(:object => @object), html_options) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module DojoHelpers | ||
module Dijit | ||
module Layout | ||
end | ||
end | ||
end | ||
|
||
ActionView::Base.class_eval {include DojoHelpers::Dijit::Layout} |