ultimate-base 0.3.5.0 → 0.4.0.0

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.
@@ -1,81 +0,0 @@
1
- #= require ./base
2
- #= require ./tag
3
-
4
- @Ultimate.Helpers.RecordTag =
5
-
6
- div_for: (record, args...) ->
7
- @content_tag_for "div", record, args...
8
-
9
- content_tag_for: (tag_name, single_or_multiple_records, prefix = null, options = null, block = null) ->
10
- block = _.outcasts.blockGiven(arguments)
11
- if _.isFunction(single_or_multiple_records?.map)
12
- single_or_multiple_records.map( (single_record) =>
13
- @content_tag_for_single_record(tag_name, single_record, prefix, options, block)
14
- ).join("\n")
15
- else
16
- @content_tag_for_single_record(tag_name, single_or_multiple_records, prefix, options, block)
17
-
18
- content_tag_for_single_record: (tag_name, record, prefix, options, block = null) ->
19
- return '' unless record?
20
- [options, prefix] = [prefix, null] if _.isObject(prefix)
21
- block = _.outcasts.blockGiven(arguments)
22
- options = if _.isObject(options) then _.clone(options) else {}
23
- _.extend options, {class: Ultimate.Helpers.Tag.concat_class(@dom_class(record, prefix), options['class']), id: @dom_id(record, prefix)}
24
- if block
25
- Ultimate.Helpers.Tag.content_tag(tag_name, block(record), options, false)
26
- else
27
- Ultimate.Helpers.Tag.content_tag(tag_name, block, options, false)
28
-
29
-
30
-
31
- # from ActionView::ModelNaming
32
- model_name_from_record_or_class: (record_or_class) ->
33
- modelClass = record_or_class?.constructor ? record_or_class
34
- if modelClass?
35
- modelClass.modelName or modelClass.className or modelClass.name or 'Model'
36
- else
37
- 'Model'
38
-
39
- # ============= from ActionView::RecordIdentifier ===============
40
-
41
- # The DOM class convention is to use the singular form of an object or class. Examples:
42
- #
43
- # dom_class(post) # => "post"
44
- # dom_class(Person) # => "person"
45
- #
46
- # If you need to address multiple instances of the same class in the same view, you can prefix the dom_class:
47
- #
48
- # dom_class(post, 'edit') # => "edit_post"
49
- # dom_class(Person, 'edit') # => "edit_person"
50
- dom_class: (record_or_class, prefix = "") ->
51
- singular =
52
- _.result(record_or_class, 'singular') ?
53
- _.singularize _.string.underscored(
54
- if _.isString(record_or_class)
55
- record_or_class
56
- else
57
- @model_name_from_record_or_class(record_or_class) )
58
- if prefix then "#{prefix}_#{singular}" else singular
59
-
60
- # The DOM id convention is to use the singular form of an object or class with the id following an underscore.
61
- # If no id is found, prefix with "new_" instead. Examples:
62
- #
63
- # dom_id(Post.find(45)) # => "post_45"
64
- # dom_id(new Post) # => "new_post"
65
- #
66
- # If you need to address multiple instances of the same class in the same view, you can prefix the dom_id:
67
- #
68
- # dom_id(Post.find(45), "edit") # => "edit_post_45"
69
- # TODO sync with rorId and ror_id
70
- dom_id: (record, prefix = "") ->
71
- if record_id = @_record_key_for_dom_id(record)
72
- "#{@dom_class(record, prefix)}_#{record_id}"
73
- else
74
- @dom_class(record, prefix or "new")
75
-
76
-
77
-
78
- # protected
79
-
80
- _record_key_for_dom_id: (record) ->
81
- record?.id
@@ -1,91 +0,0 @@
1
- #= require ./base
2
-
3
- @Ultimate.Helpers.Tag =
4
-
5
- BOOLEAN_ATTRIBUTES: "disabled readonly multiple checked autobuffer
6
- autoplay controls loop selected hidden scoped async
7
- defer reversed ismap seemless muted required
8
- autofocus novalidate formnovalidate open pubdate itemscope".split(/\s+/)
9
-
10
- PRE_CONTENT_STRINGS:
11
- textarea: "\n"
12
-
13
- tag: (tag_name, options = {}, open = false, escape = true) ->
14
- "<#{tag_name}#{@tag_options(options, escape)}#{if open then '>' else ' />'}"
15
-
16
- content_tag: (name, content_or_options_with_block = null, options = null, escape = true, block = null) ->
17
- if block = _.outcasts.blockGiven(arguments)
18
- options = content_or_options_with_block
19
- @content_tag_string(name, block(), options, escape)
20
- else
21
- @content_tag_string(name, content_or_options_with_block, options, escape)
22
-
23
- cdata_section: (content) ->
24
- splitted = content.replace(/\]\]>/g, ']]]]><![CDATA[>')
25
- "<![CDATA[#{splitted}]]>"
26
-
27
- content_tag_string: (name, content = '', options = {}, escape = true, _escapeContent = escape) ->
28
- content = _.string.escapeHTML(content) if _escapeContent
29
- "<#{name}#{@tag_options(options, escape)}>#{@PRE_CONTENT_STRINGS[name] ? ''}#{content}</#{name}>"
30
-
31
- html_options_to_s: (html_options, escape = false, prefix = "") ->
32
- deprecate 'html_options_to_s()', "tag_options()"
33
- @tag_options arguments...
34
-
35
- tag_options: (options, escape = true) ->
36
- return "" if _.isEmpty(options)
37
- attrs = []
38
- for key, value of options
39
- if key is "data" and _.isObject(value)
40
- for k, v of value
41
- attrs.push @data_tag_option(k, v, escape)
42
- else if _.include(@BOOLEAN_ATTRIBUTES, key)
43
- attrs.push @boolean_tag_option(key) if value
44
- else if value?
45
- attrs.push @tag_option(key, value, escape)
46
- if _.isEmpty(attrs) then "" else " #{attrs.sort().join(' ')}"
47
-
48
- data_tag_option: (key, value, escape) ->
49
- key = "data-#{_.string.dasherize(key)}"
50
- value = JSON.stringify(value) if JSON? and _.isObject(value)
51
- @tag_option(key, value, escape)
52
-
53
- boolean_tag_option: (key) ->
54
- "#{key}=\"#{key}\""
55
-
56
- tag_option: (key, value, escape) ->
57
- value = value.join(" ") if _.isArray(value)
58
- value = _.string.escapeHTML(value) if escape
59
- "#{key}=\"#{value}\""
60
-
61
- concat_class: ->
62
- flatten_classes = _.filter( _.flatten(arguments), (e) -> _.isString(e) ).join(' ')
63
- _.uniq( _.string.words(flatten_classes) ).join(' ')
64
-
65
-
66
-
67
- selfClosedTags: _.string.words('area base br col command embed hr img input keygen link meta param source track wbr')
68
-
69
- # Generate html from zen-selector. Ninja tool.
70
- # TODO more zen features: +, *x, {content}
71
- # TODO cache
72
- selectorToHtml: (selector) ->
73
- if matches = selector.match(/^[\s>]*([\w\-\.#]+)(.*)$/)
74
- selector = matches[1]
75
- continuation = matches[2] # in v1 {(if continuation then ">" + content else " />")}
76
- tag_name = selector.match(/^\w+/)?[0] or 'div'
77
- id = selector.match(/#([\w\-]+)/)?[1]
78
- classes = _.map( selector.match(/\.[\w\-]+/g), (c) -> _.string.ltrim(c, '.') )
79
- html_options = {}
80
- html_options['id'] = id if id
81
- html_options['class'] = classes.join(' ') if classes.length
82
- if _.contains(@selfClosedTags, tag_name)
83
- @tag(tag_name, html_options)
84
- else
85
- continuation = @selectorToHtml(continuation) if continuation
86
- @content_tag(tag_name, continuation, html_options)
87
- # else if matches = selector.match(/^\s*\+\s*(.*)$/) # /^\s*\+\s*([\w\.#]+)(.*)$/)
88
- # continuation = matches[1]
89
- # if continuation then @selectorToHtml(continuation) else continuation
90
- else
91
- ''
@@ -1,152 +0,0 @@
1
- #= require ./base
2
- #= require ./tag
3
- #= require ./javascript
4
-
5
- __char_encode = (char) -> "%#{char.charCodeAt(0).toString(16)}"
6
- __escape_path = (str) -> str.replace(/[^*\-.0-9A-Z_a-z]/g, __char_encode).replace(/\+/g, '%20')
7
- __string_encode = (str) -> _.map(str, (char) -> "&##{char.charCodeAt(0)};" ).join('')
8
-
9
- @Ultimate.Helpers.Url =
10
-
11
- url_for: (options = null) ->
12
- if _.isString(options)
13
- if options is 'back'
14
- 'javascript:history.back();'
15
- else
16
- options
17
- else unless _.isEmpty(options)
18
- url = _.result(options, 'url') ? ''
19
- if $.isPlainObject(options)
20
- options = _.clone(options)
21
- delete options['url']
22
- anchor = _.outcasts.delete(options, 'anchor')
23
- url += "?#{_.map(options, (value, key) -> "#{key}=#{value}").sort().join('&')}" unless _.isEmpty(options)
24
- url += "##{anchor}" if anchor
25
- url
26
- else
27
- 'javascript:;'
28
-
29
- link_to: (name = null, options = null, html_options = null, block = null) ->
30
- [html_options, options] = [options, name] if block = _.outcasts.blockGiven(arguments)
31
- options ||= {}
32
- url = @url_for(options)
33
- html_options = @_convert_options_to_data_attributes(options, html_options)
34
- html_options['href'] ||= url
35
- if block
36
- Ultimate.Helpers.Tag.content_tag('a', html_options, null, false, block)
37
- else
38
- Ultimate.Helpers.Tag.content_tag('a', name ? url, html_options, false)
39
-
40
- link_to_js: (name = null, html_options = null, block = null) ->
41
- [options, name] = [name, null] if block = _.outcasts.blockGiven(arguments)
42
- @link_to [name, options, html_options, block]...
43
-
44
- # TODO tests
45
- link_to_unless_current_span: (name, options = null, html_options = null, block = null) ->
46
- [html_options, options] = [options, name] if block = _.outcasts.blockGiven(arguments)
47
- url = @url_for(options)
48
- if @current_page_(url)
49
- if block
50
- Ultimate.Helpers.Tag.content_tag('span', html_options, null, false, block)
51
- else
52
- Ultimate.Helpers.Tag.content_tag('span', name ? url, html_options, false)
53
- else
54
- if block
55
- @link_to options, html_options, block
56
- else
57
- @link_to name, options, html_options
58
-
59
- # TODO tests
60
- link_to_unless_current: (name, options = {}, html_options = {}, block = null) ->
61
- @link_to_unless @current_page_(options), name, options, html_options, block
62
-
63
- # TODO tests
64
- link_to_unless: (condition, name, options = {}, html_options = {}, block = null) ->
65
- if condition
66
- if block = _.outcasts.blockGiven(arguments)
67
- block(name, options, html_options)
68
- else
69
- name
70
- else
71
- @link_to name, options, html_options, block
72
-
73
- # TODO tests
74
- link_to_if: (condition, name, options = {}, html_options = {}, block = null) ->
75
- @link_to_unless not condition, name, options, html_options, block
76
-
77
- mail_to: (email_address, name = null, html_options = {}) ->
78
- email_address = _.string.escapeHTML(email_address)
79
- encode = _.outcasts.delete(html_options, 'encode')
80
- extras = _.compact _.map _.string.words('cc bcc body subject'), (item) ->
81
- option = _.outcasts.delete(html_options, item)
82
- if option?
83
- "#{item}=#{__escape_path(option)}"
84
- extras = if _.isEmpty(extras) then '' else '?' + _.string.escapeHTML(extras.join('&'))
85
- email_address_obfuscated = email_address
86
- email_address_obfuscated = email_address_obfuscated.replace('@', _.outcasts.delete(html_options, 'replace_at')) if 'replace_at' of html_options
87
- email_address_obfuscated = email_address_obfuscated.replace('.', _.outcasts.delete(html_options, 'replace_dot')) if 'replace_dot' of html_options
88
- switch encode
89
- when 'javascript'
90
- html = @link_to(name or email_address_obfuscated, "mailto:#{email_address}#{extras}", html_options)
91
- html = Ultimate.Helpers.Javascript.escape_javascript(html)
92
- string = _.map("document.write('#{html}');", __char_encode).join('')
93
- "<script>eval(decodeURIComponent('#{string}'))</script>"
94
- when 'hex'
95
- email_address_encoded = __string_encode(email_address_obfuscated)
96
- string = __string_encode('mailto:') + _.map(email_address, (char) -> if /\w/.test(char) then __char_encode(char) else char).join('')
97
- @link_to name or email_address_encoded, "#{string}#{extras}", html_options
98
- else
99
- @link_to name or email_address_obfuscated, "mailto:#{email_address}#{extras}", html_options
100
-
101
- # TODO tests
102
- current_page_: (options) ->
103
- url_string = @url_for(options)
104
- request_uri = location.pathname
105
- if /^\w+:\/\//.test(url_string)
106
- url_string == "#{location.protocol}#{location.host}#{request_uri}"
107
- else
108
- url_string == request_uri
109
-
110
-
111
-
112
- _convert_options_to_data_attributes: (options, html_options) ->
113
- if html_options
114
- html_options['data-remote'] = 'true' if @_link_to_remote_options(options) or @_link_to_remote_options(html_options)
115
- method = _.outcasts.delete(html_options, 'method')
116
- @_add_method_to_attributes(html_options, method) if method
117
- html_options
118
- else
119
- if @_link_to_remote_options(options) then {'data-remote': 'true'} else {}
120
-
121
- _link_to_remote_options: (options) ->
122
- _.isObject(options) and _.outcasts.delete(options, 'remote')
123
-
124
- _add_method_to_attributes: (html_options, method) ->
125
- if _.isString(method) and method.toLowerCase() isnt 'get' and not /nofollow/.test(html_options['rel'])
126
- html_options['rel'] = Ultimate.Helpers.Tag.concat_class(html_options['rel'], 'nofollow')
127
- html_options['data-method'] = method
128
-
129
- _convert_boolean_attributes: (html_options, bool_attrs) ->
130
- html_options[x] = x for x in bool_attrs when _.outcasts.delete(html_options, x)
131
- html_options
132
-
133
- __protect_against_forgery: false
134
- __request_forgery_protection_token: 'form_token'
135
- __form_authenticity_token: 'secret'
136
-
137
- __init_request_forgery_protection: ->
138
- param = $('head meta[name="csrf-param"]').attr('content')
139
- token = $('head meta[name="csrf-token"]').attr('content')
140
- if param and token
141
- @__protect_against_forgery = true
142
- @__request_forgery_protection_token = param
143
- @__form_authenticity_token = token
144
-
145
- _token_tag: (token = @__form_authenticity_token) ->
146
- if token isnt false and @__protect_against_forgery
147
- Ultimate.Helpers.Tag.tag 'input', type: 'hidden', name: @__request_forgery_protection_token, value: token
148
- else
149
- ''
150
-
151
- _method_tag: (method) ->
152
- Ultimate.Helpers.Tag.tag 'input', type: 'hidden', name: '_method', value: method
@@ -1,51 +0,0 @@
1
- #= require ultimate/underscore/underscore
2
- #= require ultimate/underscore/underscore.string
3
- #= require ultimate/helpers/asset_tag
4
-
5
- module "Ultimate.Helpers.AssetTag"
6
-
7
- _.extend @, Ultimate.Helpers.AssetTag
8
-
9
- test "favicon_link_tag", ->
10
- equal favicon_link_tag(), '<link href="/images/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />'
11
- equal favicon_link_tag('favicon.ico'), '<link href="/images/favicon.ico" rel="shortcut icon" type="image/vnd.microsoft.icon" />'
12
- equal favicon_link_tag('favicon.ico', rel: 'foo'), '<link href="/images/favicon.ico" rel="foo" type="image/vnd.microsoft.icon" />'
13
- equal favicon_link_tag('favicon.ico', rel: 'foo', type: 'bar'), '<link href="/images/favicon.ico" rel="foo" type="bar" />'
14
- equal favicon_link_tag('mb-icon.png', rel: 'apple-touch-icon', type: 'image/png'), '<link href="/images/mb-icon.png" rel="apple-touch-icon" type="image/png" />'
15
-
16
- test "image_path", ->
17
- equal image_path(""), ''
18
- equal image_path("xml"), '/images/xml'
19
- equal image_path("xml.png"), '/images/xml.png'
20
- equal image_path("dir/xml.png"), '/images/dir/xml.png'
21
- equal image_path("/dir/xml.png"), '/dir/xml.png'
22
-
23
- test "path_to_image", ->
24
- equal path_to_image(""), ''
25
- equal path_to_image("xml"), '/images/xml'
26
- equal path_to_image("xml.png"), '/images/xml.png'
27
- equal path_to_image("dir/xml.png"), '/images/dir/xml.png'
28
- equal path_to_image("/dir/xml.png"), '/dir/xml.png'
29
-
30
- test "image_alt", ->
31
- for prefix in ['', '/', '/foo/bar/', 'foo/bar/']
32
- equal image_alt("#{prefix}rails.png"), 'Rails'
33
- equal image_alt("#{prefix}rails-9c0a079bdd7701d7e729bd956823d153.png"), 'Rails'
34
- equal image_alt("#{prefix}avatar-0000.png"), 'Avatar-0000'
35
-
36
- test "image_tag", ->
37
- equal image_tag("xml.png"), '<img alt="Xml" src="/images/xml.png" />'
38
- equal image_tag("rss.gif", alt: "rss syndication"), '<img alt="rss syndication" src="/images/rss.gif" />'
39
- equal image_tag("gold.png", size: "20"), '<img alt="Gold" height="20" src="/images/gold.png" width="20" />'
40
- equal image_tag("gold.png", size: "45x70"), '<img alt="Gold" height="70" src="/images/gold.png" width="45" />'
41
- equal image_tag("gold.png", size: "45x70"), '<img alt="Gold" height="70" src="/images/gold.png" width="45" />'
42
- equal image_tag("error.png", size: "45 x 70"), '<img alt="Error" src="/images/error.png" />'
43
- equal image_tag("error.png", size: "x"), '<img alt="Error" src="/images/error.png" />'
44
- equal image_tag("google.com.png"), '<img alt="Google.com" src="/images/google.com.png" />'
45
- equal image_tag("slash..png"), '<img alt="Slash." src="/images/slash..png" />'
46
- equal image_tag(".pdf.png"), '<img alt=".pdf" src="/images/.pdf.png" />'
47
- equal image_tag("http://www.rubyonrails.com/images/rails.png"), '<img alt="Rails" src="http://www.rubyonrails.com/images/rails.png" />'
48
- equal image_tag("//www.rubyonrails.com/images/rails.png"), '<img alt="Rails" src="//www.rubyonrails.com/images/rails.png" />'
49
- equal image_tag("mouse.png", alt: null), '<img src="/images/mouse.png" />'
50
- equal image_tag("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==", alt: null), '<img src="data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==" />'
51
- equal image_tag(""), '<img src="" />'
@@ -1,170 +0,0 @@
1
- #= require ultimate/underscore/underscore
2
- #= require ultimate/underscore/underscore.string
3
- #= require ultimate/underscore/underscore.outcasts
4
- #= require ultimate/backbone/lib/backbone
5
- #= require ultimate/helpers/form_options
6
-
7
- module "Ultimate.Helpers.FormOptions"
8
-
9
- _.extend @, Ultimate.Helpers.FormOptions
10
-
11
- test "options_for_select", ->
12
- options = '<option value="Denmark">Denmark</option><option value="USA">USA</option><option value="Sweden">Sweden</option>'
13
- equal options_for_select(options), options
14
- equal options_for_select([ "<Denmark>", "USA", "Sweden" ]),
15
- "<option value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>\n<option value=\"USA\">USA</option>\n<option value=\"Sweden\">Sweden</option>"
16
- equal options_for_select([ "Denmark", "<USA>", "Sweden" ], "<USA>"),
17
- "<option value=\"Denmark\">Denmark</option>\n<option selected=\"selected\" value=\"&lt;USA&gt;\">&lt;USA&gt;</option>\n<option value=\"Sweden\">Sweden</option>"
18
- equal options_for_select([ "Denmark", "<USA>", "Sweden" ], [ "<USA>", "Sweden" ]),
19
- "<option value=\"Denmark\">Denmark</option>\n<option selected=\"selected\" value=\"&lt;USA&gt;\">&lt;USA&gt;</option>\n<option selected=\"selected\" value=\"Sweden\">Sweden</option>"
20
- equal options_for_select([ "Denmark", "<USA>", "Sweden" ], disabled: "<USA>"),
21
- "<option value=\"Denmark\">Denmark</option>\n<option disabled=\"disabled\" value=\"&lt;USA&gt;\">&lt;USA&gt;</option>\n<option value=\"Sweden\">Sweden</option>"
22
- equal options_for_select([ "Denmark", "<USA>", "Sweden" ], disabled: ["<USA>", "Sweden"]),
23
- "<option value=\"Denmark\">Denmark</option>\n<option disabled=\"disabled\" value=\"&lt;USA&gt;\">&lt;USA&gt;</option>\n<option disabled=\"disabled\" value=\"Sweden\">Sweden</option>"
24
- equal options_for_select([ "Denmark", "<USA>", "Sweden" ], selected: "Denmark", disabled: "<USA>"),
25
- "<option selected=\"selected\" value=\"Denmark\">Denmark</option>\n<option disabled=\"disabled\" value=\"&lt;USA&gt;\">&lt;USA&gt;</option>\n<option value=\"Sweden\">Sweden</option>"
26
- equal options_for_select([ true, false ], selected: false, disabled: null),
27
- "<option value=\"true\">true</option>\n<option selected=\"selected\" value=\"false\">false</option>"
28
- equal options_for_select([1..3]),
29
- "<option value=\"1\">1</option>\n<option value=\"2\">2</option>\n<option value=\"3\">3</option>"
30
- equal options_for_select([ "ruby", "rubyonrails" ], "rubyonrails"),
31
- "<option value=\"ruby\">ruby</option>\n<option selected=\"selected\" value=\"rubyonrails\">rubyonrails</option>"
32
- equal options_for_select([ "ruby", "rubyonrails" ], "ruby"),
33
- "<option selected=\"selected\" value=\"ruby\">ruby</option>\n<option value=\"rubyonrails\">rubyonrails</option>"
34
- equal options_for_select([ "ruby", "rubyonrails", null ], "ruby"),
35
- "<option selected=\"selected\" value=\"ruby\">ruby</option>\n<option value=\"rubyonrails\">rubyonrails</option>\n<option value=\"\"></option>"
36
- equal options_for_select("$": "Dollar", "<DKR>": "<Kroner>"),
37
- "<option value=\"Dollar\">$</option>\n<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>"
38
- equal options_for_select({ "$": "Dollar", "<DKR>": "<Kroner>" }, "Dollar"),
39
- "<option selected=\"selected\" value=\"Dollar\">$</option>\n<option value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>"
40
- equal options_for_select({ "$": "Dollar", "<DKR>": "<Kroner>" }, [ "Dollar", "<Kroner>" ]),
41
- "<option selected=\"selected\" value=\"Dollar\">$</option>\n<option selected=\"selected\" value=\"&lt;Kroner&gt;\">&lt;DKR&gt;</option>"
42
- equal options_for_select([ [ "<Denmark>", { class: 'bold' } ], [ "USA", { onclick: "alert('Hello World');" } ], [ "Sweden" ], "Germany" ]),
43
- "<option class=\"bold\" value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>\n<option onclick=\"alert(&apos;Hello World&apos;);\" value=\"USA\">USA</option>\n<option value=\"Sweden\">Sweden</option>\n<option value=\"Germany\">Germany</option>"
44
- equal options_for_select([ [ "<Denmark>", { data: { test: 'bold' } } ] ]),
45
- "<option data-test=\"bold\" value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>"
46
- equal options_for_select([ [ "<Denmark>", { data: { test: '<bold>' } } ] ]),
47
- "<option data-test=\"&lt;bold&gt;\" value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>"
48
- equal options_for_select([ "<Denmark>", [ "USA", { class: 'bold' } ], "Sweden" ], "USA"),
49
- "<option value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>\n<option class=\"bold\" selected=\"selected\" value=\"USA\">USA</option>\n<option value=\"Sweden\">Sweden</option>"
50
- equal options_for_select([ "<Denmark>", [ "USA", { class: 'bold' } ], "Sweden" ], [ "USA", "Sweden" ]),
51
- "<option value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>\n<option class=\"bold\" selected=\"selected\" value=\"USA\">USA</option>\n<option selected=\"selected\" value=\"Sweden\">Sweden</option>"
52
- equal options_for_select([ [ "<Denmark>", { onclick: 'alert("<code>")' } ] ]),
53
- "<option onclick=\"alert(&quot;&lt;code&gt;&quot;)\" value=\"&lt;Denmark&gt;\">&lt;Denmark&gt;</option>"
54
-
55
-
56
-
57
- class Struct
58
- constructor: ->
59
- for key, index in @constructor.keys
60
- @[key] = arguments[index] ? null
61
-
62
- class Post extends Struct
63
- @keys = ['title', 'author_name', 'body', 'secret', 'written_on', 'category', 'origin', 'allow_comments']
64
-
65
- dummy_posts =
66
- [ new Post("<Abe> went home", "<Abe>", "To a little house", "shh!"),
67
- new Post("Babe went home", "Babe", "To a little house", "shh!"),
68
- new Post("Cabe went home", "Cabe", (-> "To a little house"), (-> "shh!")) ]
69
-
70
- test "options_from_collection_for_select", ->
71
- equal options_from_collection_for_select(dummy_posts, "author_name", "title"),
72
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>"
73
- equal options_from_collection_for_select(dummy_posts, "author_name", "title", "Babe"),
74
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option selected=\"selected\" value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>"
75
- equal options_from_collection_for_select(dummy_posts, "author_name", "title", [ "Babe", "Cabe" ]),
76
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option selected=\"selected\" value=\"Babe\">Babe went home</option>\n<option selected=\"selected\" value=\"Cabe\">Cabe went home</option>"
77
- equal options_from_collection_for_select(dummy_posts, "author_name", "title", (p) -> p.author_name is 'Babe'),
78
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option selected=\"selected\" value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>"
79
- equal options_from_collection_for_select(dummy_posts, "author_name", "title", disabled: "Babe"),
80
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option disabled=\"disabled\" value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>"
81
- equal options_from_collection_for_select(dummy_posts, "author_name", "title", disabled: [ "Babe", "Cabe" ]),
82
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option disabled=\"disabled\" value=\"Babe\">Babe went home</option>\n<option disabled=\"disabled\" value=\"Cabe\">Cabe went home</option>"
83
- equal options_from_collection_for_select(dummy_posts, "author_name", "title", selected: "Cabe", disabled: "Babe"),
84
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option disabled=\"disabled\" value=\"Babe\">Babe went home</option>\n<option selected=\"selected\" value=\"Cabe\">Cabe went home</option>"
85
- equal options_from_collection_for_select(dummy_posts, "author_name", "title", disabled: (p) -> p.author_name in ['Babe', 'Cabe']),
86
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option disabled=\"disabled\" value=\"Babe\">Babe went home</option>\n<option disabled=\"disabled\" value=\"Cabe\">Cabe went home</option>"
87
- equal options_from_collection_for_select(dummy_posts, ((p) -> p.author_name), "title"),
88
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>"
89
- equal options_from_collection_for_select(dummy_posts, "author_name", (p) -> p.title),
90
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>"
91
-
92
-
93
-
94
- _objectsArrayToHashesArray = (objectsArray) ->
95
- _.map objectsArray, (element) ->
96
- hash = {}
97
- for key in element.constructor.keys
98
- hash[key] = element[key]
99
- hash
100
-
101
- test "options_from_collection_for_select with array of hashes", ->
102
- equal options_from_collection_for_select(_objectsArrayToHashesArray(dummy_posts), "author_name", "title", "Babe"),
103
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option selected=\"selected\" value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>"
104
-
105
-
106
-
107
- class BBPost extends Backbone.Model
108
-
109
- class BBPosts extends Backbone.Collection
110
- model: BBPost
111
-
112
- test "options_from_collection_for_select with Backbone.Collection", ->
113
- bbPosts = new BBPosts(_objectsArrayToHashesArray(dummy_posts))
114
- equal options_from_collection_for_select(bbPosts, "author_name", "title", "Babe"),
115
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option selected=\"selected\" value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>"
116
- equal options_from_collection_for_select(bbPosts, "author_name", "title", disabled: (p) -> p.get('author_name') in ['Babe', 'Cabe']),
117
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option disabled=\"disabled\" value=\"Babe\">Babe went home</option>\n<option disabled=\"disabled\" value=\"Cabe\">Cabe went home</option>"
118
- equal options_from_collection_for_select(bbPosts, ((p) -> p.get('author_name')), "title"),
119
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>"
120
- equal options_from_collection_for_select(bbPosts, "author_name", (p) -> p.get('title')),
121
- "<option value=\"&lt;Abe&gt;\">&lt;Abe&gt; went home</option>\n<option value=\"Babe\">Babe went home</option>\n<option value=\"Cabe\">Cabe went home</option>"
122
-
123
-
124
-
125
- class Continent extends Struct
126
- @keys = ['continent_name', 'countries']
127
-
128
- class Country extends Struct
129
- @keys = ['country_id', 'country_name']
130
-
131
- dummy_continents =
132
- [ new Continent("<Africa>", [new Country("<sa>", "<South Africa>"), new Country("so", "Somalia")]),
133
- new Continent("Europe", [new Country("dk", "Denmark"), new Country("ie", "Ireland")]) ]
134
-
135
- test "option_groups_from_collection_for_select", ->
136
- equal option_groups_from_collection_for_select(dummy_continents, "countries", "continent_name", "country_id", "country_name", "dk"),
137
- "<optgroup label=\"&lt;Africa&gt;\"><option value=\"&lt;sa&gt;\">&lt;South Africa&gt;</option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option selected=\"selected\" value=\"dk\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>"
138
-
139
-
140
-
141
- class BBContinent extends Backbone.Model
142
-
143
- class BBContinents extends Backbone.Collection
144
- model: BBContinent
145
-
146
- test "option_groups_from_collection_for_select with Backbone.Collection", ->
147
- BBContinents = new BBContinents(_objectsArrayToHashesArray(dummy_continents))
148
- equal option_groups_from_collection_for_select(dummy_continents, "countries", "continent_name", "country_id", "country_name", "dk"),
149
- "<optgroup label=\"&lt;Africa&gt;\"><option value=\"&lt;sa&gt;\">&lt;South Africa&gt;</option>\n<option value=\"so\">Somalia</option></optgroup><optgroup label=\"Europe\"><option selected=\"selected\" value=\"dk\">Denmark</option>\n<option value=\"ie\">Ireland</option></optgroup>"
150
-
151
-
152
-
153
- test "grouped_options_for_select", ->
154
- equal grouped_options_for_select([
155
- ["North America",
156
- [['United States','US'],"Canada"]],
157
- ["Europe",
158
- [["Great Britain","GB"], "Germany"]]
159
- ]),
160
- "<optgroup label=\"North America\"><option value=\"US\">United States</option>\n<option value=\"Canada\">Canada</option></optgroup><optgroup label=\"Europe\"><option value=\"GB\">Great Britain</option>\n<option value=\"Germany\">Germany</option></optgroup>"
161
- equal grouped_options_for_select([['US',"Canada"] , ["GB", "Germany"]], null, divider: "----------"),
162
- "<optgroup label=\"----------\"><option value=\"US\">US</option>\n<option value=\"Canada\">Canada</option></optgroup><optgroup label=\"----------\"><option value=\"GB\">GB</option>\n<option value=\"Germany\">Germany</option></optgroup>"
163
- equal grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], "Cowboy Hat", prompt: "Choose a product..."),
164
- "<option value=\"\">Choose a product...</option><optgroup label=\"Hats\"><option value=\"Baseball Cap\">Baseball Cap</option>\n<option selected=\"selected\" value=\"Cowboy Hat\">Cowboy Hat</option></optgroup>"
165
- equal grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], "Cowboy Hat", prompt: true),
166
- "<option value=\"\">Please select</option><optgroup label=\"Hats\"><option value=\"Baseball Cap\">Baseball Cap</option>\n<option selected=\"selected\" value=\"Cowboy Hat\">Cowboy Hat</option></optgroup>"
167
- equal grouped_options_for_select([["Hats", ["Baseball Cap","Cowboy Hat"]]], null, prompt: '<Choose One>'),
168
- "<option value=\"\">&lt;Choose One&gt;</option><optgroup label=\"Hats\"><option value=\"Baseball Cap\">Baseball Cap</option>\n<option value=\"Cowboy Hat\">Cowboy Hat</option></optgroup>"
169
- equal grouped_options_for_select({'North America': ['United States','Canada'], 'Europe': ['Denmark','Germany']}),
170
- "<optgroup label=\"Europe\"><option value=\"Denmark\">Denmark</option>\n<option value=\"Germany\">Germany</option></optgroup><optgroup label=\"North America\"><option value=\"United States\">United States</option>\n<option value=\"Canada\">Canada</option></optgroup>"