hyperactiveform 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/docs/index.html ADDED
@@ -0,0 +1,271 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>
7
+ File: README
8
+
9
+ &mdash; Documentation by YARD 0.9.37
10
+
11
+ </title>
12
+
13
+ <link rel="stylesheet" href="css/style.css" type="text/css" />
14
+
15
+ <link rel="stylesheet" href="css/common.css" type="text/css" />
16
+
17
+ <script type="text/javascript">
18
+ pathId = "README";
19
+ relpath = '';
20
+ </script>
21
+
22
+
23
+ <script type="text/javascript" charset="utf-8" src="js/jquery.js"></script>
24
+
25
+ <script type="text/javascript" charset="utf-8" src="js/app.js"></script>
26
+
27
+
28
+ </head>
29
+ <body>
30
+ <div class="nav_wrap">
31
+ <iframe id="nav" src="class_list.html?1"></iframe>
32
+ <div id="resizer"></div>
33
+ </div>
34
+
35
+ <div id="main" tabindex="-1">
36
+ <div id="header">
37
+ <div id="menu">
38
+
39
+ <a href="_index.html">Index</a> &raquo;
40
+ <span class="title">File: README</span>
41
+
42
+ </div>
43
+
44
+ <div id="search">
45
+
46
+ <a class="full_list_link" id="class_list_link"
47
+ href="class_list.html">
48
+
49
+ <svg width="24" height="24">
50
+ <rect x="0" y="4" width="24" height="4" rx="1" ry="1"></rect>
51
+ <rect x="0" y="12" width="24" height="4" rx="1" ry="1"></rect>
52
+ <rect x="0" y="20" width="24" height="4" rx="1" ry="1"></rect>
53
+ </svg>
54
+ </a>
55
+
56
+ </div>
57
+ <div class="clear"></div>
58
+ </div>
59
+
60
+ <div id="content"><div id='filecontents'>
61
+ <h1 id="label-E2-9C-A8+HyperActiveForm+-E2-9C-A8">✨ HyperActiveForm ✨</h1>
62
+
63
+ <p>HyperActiveForm is a simple form object implementation for Rails.</p>
64
+
65
+ <p>Form objects are objects that encapsulate form logic and validations, they allow to extract the business logic out of the controller and models into specialized objects.</p>
66
+
67
+ <p>HyperActiveForm’s form objects mimic the ActiveModel API, so they work out of the box with Rails’ form helpers, and allow you to use the ActiveModel validations you already know.</p>
68
+
69
+ <p>This allows you to only keep strictly necessary validations in the model, and have business logic validations in the form object. This is especially useful when you want different validations to be applied depending on the context.</p>
70
+
71
+ <h2 id="label-Installation">Installation</h2>
72
+
73
+ <p>Add this line to your application’s Gemfile:</p>
74
+
75
+ <pre class="code ruby"><code class="ruby"><span class='id identifier rubyid_gem'>gem</span> <span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>hyperactiveform</span><span class='tstring_end'>&#39;</span></span>
76
+ </code></pre>
77
+
78
+ <p>And then execute:</p>
79
+
80
+ <pre class="code ruby"><code class="ruby">$ bundle install
81
+ </code></pre>
82
+
83
+ <p>Run the install generator:</p>
84
+
85
+ <pre class="code ruby"><code class="ruby">$ rails generate hyper_active_form:install
86
+ </code></pre>
87
+
88
+ <p>this will create an <code>ApplicationForm</code> class in your app/forms directory. You can use it as a base class for your form objects.</p>
89
+
90
+ <h2 id="label-Usage">Usage</h2>
91
+
92
+ <p>Here is an example of an <code>HyperActiveForm</code> form object:</p>
93
+
94
+ <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
95
+ <span class='comment'># proxy_for is used to delegate the model name to the class, and some methods to the object
96
+ </span> <span class='comment'># this helps use `form_with` in views without having to specify the url
97
+ </span> <span class='id identifier rubyid_proxy_for'>proxy_for</span> <span class='const'>User</span><span class='comma'>,</span> <span class='symbol'>:@user</span>
98
+
99
+ <span class='comment'># Define the form fields, using ActiveModel::Attributes
100
+ </span> <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:first_name</span>
101
+ <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:last_name</span>
102
+ <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:birth_date</span><span class='comma'>,</span> <span class='symbol'>:date</span>
103
+
104
+ <span class='comment'># Define the validations, using ActiveModel::Validations
105
+ </span> <span class='id identifier rubyid_validates'>validates</span> <span class='symbol'>:first_name</span><span class='comma'>,</span> <span class='label'>presence:</span> <span class='kw'>true</span>
106
+ <span class='id identifier rubyid_validates'>validates</span> <span class='symbol'>:last_name</span><span class='comma'>,</span> <span class='label'>presence:</span> <span class='kw'>true</span>
107
+ <span class='id identifier rubyid_validates'>validates</span> <span class='symbol'>:birth_date</span><span class='comma'>,</span> <span class='label'>presence:</span> <span class='kw'>true</span>
108
+
109
+ <span class='comment'># Pre-fill the form if needed
110
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_setup'>setup</span><span class='lparen'>(</span><span class='id identifier rubyid_user'>user</span><span class='rparen'>)</span>
111
+ <span class='ivar'>@user</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span>
112
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_first_name'>first_name</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span><span class='period'>.</span><span class='id identifier rubyid_first_name'>first_name</span>
113
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_last_name'>last_name</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span><span class='period'>.</span><span class='id identifier rubyid_last_name'>last_name</span>
114
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_birth_date'>birth_date</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span><span class='period'>.</span><span class='id identifier rubyid_birth_date'>birth_date</span>
115
+ <span class='kw'>end</span>
116
+
117
+ <span class='comment'># Perform the form logic
118
+ </span> <span class='kw'>def</span> <span class='id identifier rubyid_perform'>perform</span>
119
+ <span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_update!'>update!</span><span class='lparen'>(</span>
120
+ <span class='label'>first_name:</span> <span class='id identifier rubyid_first_name'>first_name</span><span class='comma'>,</span>
121
+ <span class='label'>last_name:</span> <span class='id identifier rubyid_last_name'>last_name</span><span class='comma'>,</span>
122
+ <span class='label'>birth_date:</span> <span class='id identifier rubyid_birth_date'>birth_date</span>
123
+ <span class='rparen'>)</span>
124
+ <span class='kw'>end</span>
125
+ <span class='kw'>end</span>
126
+ </code></pre>
127
+
128
+ <p>The controller would look like this:</p>
129
+
130
+ <pre class="code ruby"><code class="ruby">class UsersController &lt; ApplicationController
131
+
132
+ def edit
133
+ @form = ProfileForm.new(user: current_user)
134
+ end
135
+
136
+ def update
137
+ @form = ProfileForm.new(user: current_user)
138
+ if @form.submit(params[:user])
139
+ redirect_to root_path, notice: &quot;Profile updated&quot;
140
+ else
141
+ render :edit, status: :unprocessable_entity
142
+ end
143
+ end
144
+ </code></pre>
145
+
146
+ <p>And the view would look like this:</p>
147
+
148
+ <pre class="code ruby"><code class="ruby">&lt;%= form_with(model: @form) do |f| %&gt;
149
+ &lt;%= f.text_field :first_name %&gt;
150
+ &lt;%= f.text_field :last_name %&gt;
151
+ &lt;%= f.date_field :birth_date %&gt;
152
+
153
+ &lt;%= f.submit %&gt;
154
+ &lt;% end %&gt;
155
+ </code></pre>
156
+
157
+ <h3 id="label-Understanding+proxy_for">Understanding <code>proxy_for</code></h3>
158
+
159
+ <p><code>HyperActiveForm</code> mimics a model object, you can use <code>proxy_for</code> to tell it which class and object to delegate to.</p>
160
+
161
+ <p>When using <code>form_for</code> or <code>form_with</code>, Rails will choose the URL and method based on the object, according to the persisted state of the object and its model name.</p>
162
+
163
+ <p>The first argument of <code>proxy_for</code> is the class of the object, and the second argument is the name of the instance variable that holds the object.</p>
164
+
165
+ <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
166
+ <span class='id identifier rubyid_proxy_for'>proxy_for</span> <span class='const'>User</span><span class='comma'>,</span> <span class='symbol'>:@user</span> <span class='comment'># Will delegate to @user
167
+ </span><span class='kw'>end</span>
168
+ </code></pre>
169
+
170
+ <p>If you pass an url and method yourself, you don’t need to use <code>proxy_for</code>.</p>
171
+
172
+ <h3 id="label-Understanding+setup">Understanding <code>setup</code></h3>
173
+
174
+ <p><code>setup</code> is called just after the form is initialized, and is used to pre-fill the form with data from the object.</p>
175
+
176
+ <p><code>setup</code> will receive the same arguments as the initializer, so you can use it to pass any data you need to the form.</p>
177
+
178
+ <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
179
+ <span class='kw'>def</span> <span class='id identifier rubyid_setup'>setup</span><span class='lparen'>(</span><span class='id identifier rubyid_user'>user</span><span class='rparen'>)</span>
180
+ <span class='ivar'>@user</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span>
181
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_first_name'>first_name</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span><span class='period'>.</span><span class='id identifier rubyid_first_name'>first_name</span>
182
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_last_name'>last_name</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span><span class='period'>.</span><span class='id identifier rubyid_last_name'>last_name</span>
183
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_birth_date'>birth_date</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span><span class='period'>.</span><span class='id identifier rubyid_birth_date'>birth_date</span>
184
+ <span class='kw'>end</span>
185
+ <span class='kw'>end</span>
186
+ </code></pre>
187
+
188
+ <h3 id="label-Understanding+perform">Understanding <code>perform</code></h3>
189
+
190
+ <p>When using <code>submit</code> or <code>submit!</code>, <code>HyperActiveForm</code> will first assign the form attributes to the object, then perform the validations, then call <code>perform</code> on the object if the form is valid.</p>
191
+
192
+ <p>The <code>perform</code> method is where you should do the actual form logic, like updating the object or creating a new one.</p>
193
+
194
+ <p>If the return value of <code>perform</code> is not truthy, <code>HyperActiveForm</code> will consider the form encountered an error and <code>submit</code> will return <code>false</code>, or <code>submit!</code> will raise a <code>HyperActiveForm::FormDidNotSubmitError</code>.</p>
195
+
196
+ <p>At any point during the form processing, you can raise <code>HyperActiveForm::CancelForm</code> to cancel the form submission, this is the same as returning <code>false</code>.</p>
197
+
198
+ <h2 id="label-Understanding+add_errors_from">Understanding add_errors_from</h2>
199
+
200
+ <p><code>HyperActiveForm</code> provides a method to add errors from a model and apply them fo the form.</p>
201
+
202
+ <p>This is useful when the underlying model has validations that are not set up in the form object, and you want them to be applied to the form.</p>
203
+
204
+ <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>User</span> <span class='op'>&lt;</span> <span class='const'>ApplicationRecord</span>
205
+ <span class='id identifier rubyid_validates'>validates</span> <span class='symbol'>:first_name</span><span class='comma'>,</span> <span class='label'>presence:</span> <span class='kw'>true</span>
206
+ <span class='kw'>end</span>
207
+
208
+ <span class='kw'>class</span> <span class='const'>ProfileForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
209
+ <span class='id identifier rubyid_proxy_for'>proxy_for</span> <span class='const'>User</span><span class='comma'>,</span> <span class='symbol'>:@user</span>
210
+ <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:first_name</span>
211
+
212
+ <span class='kw'>def</span> <span class='id identifier rubyid_setup'>setup</span><span class='lparen'>(</span><span class='id identifier rubyid_user'>user</span><span class='rparen'>)</span>
213
+ <span class='ivar'>@user</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span>
214
+ <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_first_name'>first_name</span> <span class='op'>=</span> <span class='id identifier rubyid_user'>user</span><span class='period'>.</span><span class='id identifier rubyid_first_name'>first_name</span>
215
+ <span class='kw'>end</span>
216
+
217
+ <span class='kw'>def</span> <span class='id identifier rubyid_perform'>perform</span>
218
+ <span class='ivar'>@user</span><span class='period'>.</span><span class='id identifier rubyid_update!'>update!</span><span class='lparen'>(</span><span class='label'>first_name:</span> <span class='id identifier rubyid_first_name'>first_name</span><span class='rparen'>)</span> <span class='op'>||</span> <span class='id identifier rubyid_add_errors_from'>add_errors_from</span><span class='lparen'>(</span><span class='ivar'>@user</span><span class='rparen'>)</span>
219
+ <span class='kw'>end</span>
220
+ <span class='kw'>end</span>
221
+ </code></pre>
222
+
223
+ <h3 id="label-Not+all+forms+map+to+a+single+model">Not all forms map to a single model</h3>
224
+
225
+ <p>The power of <code>HyperActiveForm</code> is that you can use it to create forms that don’t map to a single model.</p>
226
+
227
+ <p>Some forms can be used to create several models at once. Doing so without form objects can be tedious especially with nested attributes.</p>
228
+
229
+ <p>Some forms dont map to any model at all, like a simple contact form that only sends an email and saves nothing in the database, or a sign in form that would only validate the credentials and return the instance of the connected user.</p>
230
+
231
+ <p>One great example of such forms are search forms. You can use a form object to encapsulate the search logic :</p>
232
+
233
+ <pre class="code ruby"><code class="ruby"><span class='kw'>class</span> <span class='const'>UserSearchForm</span> <span class='op'>&lt;</span> <span class='const'>ApplicationForm</span>
234
+ <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:name</span>
235
+ <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:email</span>
236
+ <span class='id identifier rubyid_attribute'>attribute</span> <span class='symbol'>:min_age</span><span class='comma'>,</span> <span class='symbol'>:integer</span>
237
+
238
+ <span class='id identifier rubyid_attr_reader'>attr_reader</span> <span class='symbol'>:results</span> <span class='comment'># So the controller can access the results
239
+ </span>
240
+ <span class='kw'>def</span> <span class='id identifier rubyid_perform'>perform</span>
241
+ <span class='ivar'>@results</span> <span class='op'>=</span> <span class='const'>User</span><span class='period'>.</span><span class='id identifier rubyid_all'>all</span>
242
+
243
+ <span class='kw'>if</span> <span class='id identifier rubyid_name'>name</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span>
244
+ <span class='ivar'>@results</span> <span class='op'>=</span> <span class='ivar'>@results</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='label'>name:</span> <span class='id identifier rubyid_name'>name</span><span class='rparen'>)</span>
245
+ <span class='kw'>end</span>
246
+ <span class='kw'>if</span> <span class='id identifier rubyid_email'>email</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span>
247
+ <span class='ivar'>@results</span> <span class='op'>=</span> <span class='ivar'>@results</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='label'>email:</span> <span class='id identifier rubyid_email'>email</span><span class='rparen'>)</span>
248
+ <span class='kw'>end</span>
249
+ <span class='kw'>if</span> <span class='id identifier rubyid_age'>age</span><span class='period'>.</span><span class='id identifier rubyid_present?'>present?</span>
250
+ <span class='ivar'>@results</span> <span class='op'>=</span> <span class='ivar'>@results</span><span class='period'>.</span><span class='id identifier rubyid_where'>where</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&quot;</span><span class='tstring_content'>age &gt;= ?</span><span class='tstring_end'>&quot;</span></span><span class='comma'>,</span> <span class='id identifier rubyid_age'>age</span><span class='rparen'>)</span>
251
+ <span class='kw'>end</span>
252
+
253
+ <span class='kw'>true</span>
254
+ <span class='kw'>end</span>
255
+ <span class='kw'>end</span>
256
+ </code></pre>
257
+
258
+ <p>And in the controller:</p>
259
+
260
+ <p>“‘ruby class UsersController &lt; ApplicationController def index @form = UserSearchForm.new @<a href=":user">form.submit!(params</a>) @users = @form.results end end</p>
261
+ </div></div>
262
+
263
+ <div id="footer">
264
+ Generated on Sat Dec 14 13:22:03 2024 by
265
+ <a href="https://yardoc.org" title="Yay! A Ruby Documentation Tool" target="_parent">yard</a>
266
+ 0.9.37 (ruby-3.3.1).
267
+ </div>
268
+
269
+ </div>
270
+ </body>
271
+ </html>
data/docs/js/app.js ADDED
@@ -0,0 +1,344 @@
1
+ (function () {
2
+ var localStorage = {},
3
+ sessionStorage = {};
4
+ try {
5
+ localStorage = window.localStorage;
6
+ } catch (e) {}
7
+ try {
8
+ sessionStorage = window.sessionStorage;
9
+ } catch (e) {}
10
+
11
+ function createSourceLinks() {
12
+ $(".method_details_list .source_code").before(
13
+ "<span class='showSource'>[<a href='#' class='toggleSource'>View source</a>]</span>"
14
+ );
15
+ $(".toggleSource").toggle(
16
+ function () {
17
+ $(this).parent().nextAll(".source_code").slideDown(100);
18
+ $(this).text("Hide source");
19
+ },
20
+ function () {
21
+ $(this).parent().nextAll(".source_code").slideUp(100);
22
+ $(this).text("View source");
23
+ }
24
+ );
25
+ }
26
+
27
+ function createDefineLinks() {
28
+ var tHeight = 0;
29
+ $(".defines").after(" <a href='#' class='toggleDefines'>more...</a>");
30
+ $(".toggleDefines").toggle(
31
+ function () {
32
+ tHeight = $(this).parent().prev().height();
33
+ $(this).prev().css("display", "inline");
34
+ $(this).parent().prev().height($(this).parent().height());
35
+ $(this).text("(less)");
36
+ },
37
+ function () {
38
+ $(this).prev().hide();
39
+ $(this).parent().prev().height(tHeight);
40
+ $(this).text("more...");
41
+ }
42
+ );
43
+ }
44
+
45
+ function createFullTreeLinks() {
46
+ var tHeight = 0;
47
+ $(".inheritanceTree").toggle(
48
+ function () {
49
+ tHeight = $(this).parent().prev().height();
50
+ $(this).parent().toggleClass("showAll");
51
+ $(this).text("(hide)");
52
+ $(this).parent().prev().height($(this).parent().height());
53
+ },
54
+ function () {
55
+ $(this).parent().toggleClass("showAll");
56
+ $(this).parent().prev().height(tHeight);
57
+ $(this).text("show all");
58
+ }
59
+ );
60
+ }
61
+
62
+ function searchFrameButtons() {
63
+ $(".full_list_link").click(function () {
64
+ toggleSearchFrame(this, $(this).attr("href"));
65
+ return false;
66
+ });
67
+ window.addEventListener("message", function (e) {
68
+ if (e.data === "navEscape") {
69
+ $("#nav").slideUp(100);
70
+ $("#search a").removeClass("active inactive");
71
+ $(window).focus();
72
+ }
73
+ });
74
+
75
+ $(window).resize(function () {
76
+ if ($("#search:visible").length === 0) {
77
+ $("#nav").removeAttr("style");
78
+ $("#search a").removeClass("active inactive");
79
+ $(window).focus();
80
+ }
81
+ });
82
+ }
83
+
84
+ function toggleSearchFrame(id, link) {
85
+ var frame = $("#nav");
86
+ $("#search a").removeClass("active").addClass("inactive");
87
+ if (frame.attr("src") === link && frame.css("display") !== "none") {
88
+ frame.slideUp(100);
89
+ $("#search a").removeClass("active inactive");
90
+ } else {
91
+ $(id).addClass("active").removeClass("inactive");
92
+ if (frame.attr("src") !== link) frame.attr("src", link);
93
+ frame.slideDown(100);
94
+ }
95
+ }
96
+
97
+ function linkSummaries() {
98
+ $(".summary_signature").click(function () {
99
+ document.location = $(this).find("a").attr("href");
100
+ });
101
+ }
102
+
103
+ function summaryToggle() {
104
+ $(".summary_toggle").click(function (e) {
105
+ e.preventDefault();
106
+ localStorage.summaryCollapsed = $(this).text();
107
+ $(".summary_toggle").each(function () {
108
+ $(this).text($(this).text() == "collapse" ? "expand" : "collapse");
109
+ var next = $(this).parent().parent().nextAll("ul.summary").first();
110
+ if (next.hasClass("compact")) {
111
+ next.toggle();
112
+ next.nextAll("ul.summary").first().toggle();
113
+ } else if (next.hasClass("summary")) {
114
+ var list = $('<ul class="summary compact" />');
115
+ list.html(next.html());
116
+ list.find(".summary_desc, .note").remove();
117
+ list.find("a").each(function () {
118
+ $(this).html($(this).find("strong").html());
119
+ $(this).parent().html($(this)[0].outerHTML);
120
+ });
121
+ next.before(list);
122
+ next.toggle();
123
+ }
124
+ });
125
+ return false;
126
+ });
127
+ if (localStorage.summaryCollapsed == "collapse") {
128
+ $(".summary_toggle").first().click();
129
+ } else {
130
+ localStorage.summaryCollapsed = "expand";
131
+ }
132
+ }
133
+
134
+ function constantSummaryToggle() {
135
+ $(".constants_summary_toggle").click(function (e) {
136
+ e.preventDefault();
137
+ localStorage.summaryCollapsed = $(this).text();
138
+ $(".constants_summary_toggle").each(function () {
139
+ $(this).text($(this).text() == "collapse" ? "expand" : "collapse");
140
+ var next = $(this).parent().parent().nextAll("dl.constants").first();
141
+ if (next.hasClass("compact")) {
142
+ next.toggle();
143
+ next.nextAll("dl.constants").first().toggle();
144
+ } else if (next.hasClass("constants")) {
145
+ var list = $('<dl class="constants compact" />');
146
+ list.html(next.html());
147
+ list.find("dt").each(function () {
148
+ $(this).addClass("summary_signature");
149
+ $(this).text($(this).text().split("=")[0]);
150
+ if ($(this).has(".deprecated").length) {
151
+ $(this).addClass("deprecated");
152
+ }
153
+ });
154
+ // Add the value of the constant as "Tooltip" to the summary object
155
+ list.find("pre.code").each(function () {
156
+ console.log($(this).parent());
157
+ var dt_element = $(this).parent().prev();
158
+ var tooltip = $(this).text();
159
+ if (dt_element.hasClass("deprecated")) {
160
+ tooltip = "Deprecated. " + tooltip;
161
+ }
162
+ dt_element.attr("title", tooltip);
163
+ });
164
+ list.find(".docstring, .tags, dd").remove();
165
+ next.before(list);
166
+ next.toggle();
167
+ }
168
+ });
169
+ return false;
170
+ });
171
+ if (localStorage.summaryCollapsed == "collapse") {
172
+ $(".constants_summary_toggle").first().click();
173
+ } else {
174
+ localStorage.summaryCollapsed = "expand";
175
+ }
176
+ }
177
+
178
+ function generateTOC() {
179
+ if ($("#filecontents").length === 0) return;
180
+ var _toc = $('<ol class="top"></ol>');
181
+ var show = false;
182
+ var toc = _toc;
183
+ var counter = 0;
184
+ var tags = ["h2", "h3", "h4", "h5", "h6"];
185
+ var i;
186
+ var curli;
187
+ if ($("#filecontents h1").length > 1) tags.unshift("h1");
188
+ for (i = 0; i < tags.length; i++) {
189
+ tags[i] = "#filecontents " + tags[i];
190
+ }
191
+ var lastTag = parseInt(tags[0][1], 10);
192
+ $(tags.join(", ")).each(function () {
193
+ if ($(this).parents(".method_details .docstring").length != 0) return;
194
+ if (this.id == "filecontents") return;
195
+ show = true;
196
+ var thisTag = parseInt(this.tagName[1], 10);
197
+ if (this.id.length === 0) {
198
+ var proposedId = $(this).attr("toc-id");
199
+ if (typeof proposedId != "undefined") this.id = proposedId;
200
+ else {
201
+ var proposedId = $(this)
202
+ .text()
203
+ .replace(/[^a-z0-9-]/gi, "_");
204
+ if ($("#" + proposedId).length > 0) {
205
+ proposedId += counter;
206
+ counter++;
207
+ }
208
+ this.id = proposedId;
209
+ }
210
+ }
211
+ if (thisTag > lastTag) {
212
+ for (i = 0; i < thisTag - lastTag; i++) {
213
+ if (typeof curli == "undefined") {
214
+ curli = $("<li/>");
215
+ toc.append(curli);
216
+ }
217
+ toc = $("<ol/>");
218
+ curli.append(toc);
219
+ curli = undefined;
220
+ }
221
+ }
222
+ if (thisTag < lastTag) {
223
+ for (i = 0; i < lastTag - thisTag; i++) {
224
+ toc = toc.parent();
225
+ toc = toc.parent();
226
+ }
227
+ }
228
+ var title = $(this).attr("toc-title");
229
+ if (typeof title == "undefined") title = $(this).text();
230
+ curli = $('<li><a href="#' + this.id + '">' + title + "</a></li>");
231
+ toc.append(curli);
232
+ lastTag = thisTag;
233
+ });
234
+ if (!show) return;
235
+ html =
236
+ '<div id="toc"><p class="title hide_toc"><a href="#"><strong>Table of Contents</strong></a></p></div>';
237
+ $("#content").prepend(html);
238
+ $("#toc").append(_toc);
239
+ $("#toc .hide_toc").toggle(
240
+ function () {
241
+ $("#toc .top").slideUp("fast");
242
+ $("#toc").toggleClass("hidden");
243
+ $("#toc .title small").toggle();
244
+ },
245
+ function () {
246
+ $("#toc .top").slideDown("fast");
247
+ $("#toc").toggleClass("hidden");
248
+ $("#toc .title small").toggle();
249
+ }
250
+ );
251
+ }
252
+
253
+ function navResizeFn(e) {
254
+ if (e.which !== 1) {
255
+ navResizeFnStop();
256
+ return;
257
+ }
258
+
259
+ sessionStorage.navWidth = e.pageX.toString();
260
+ $(".nav_wrap").css("width", e.pageX);
261
+ $(".nav_wrap").css("-ms-flex", "inherit");
262
+ }
263
+
264
+ function navResizeFnStop() {
265
+ $(window).unbind("mousemove", navResizeFn);
266
+ window.removeEventListener("message", navMessageFn, false);
267
+ }
268
+
269
+ function navMessageFn(e) {
270
+ if (e.data.action === "mousemove") navResizeFn(e.data.event);
271
+ if (e.data.action === "mouseup") navResizeFnStop();
272
+ }
273
+
274
+ function navResizer() {
275
+ $("#resizer").mousedown(function (e) {
276
+ e.preventDefault();
277
+ $(window).mousemove(navResizeFn);
278
+ window.addEventListener("message", navMessageFn, false);
279
+ });
280
+ $(window).mouseup(navResizeFnStop);
281
+
282
+ if (sessionStorage.navWidth) {
283
+ navResizeFn({ which: 1, pageX: parseInt(sessionStorage.navWidth, 10) });
284
+ }
285
+ }
286
+
287
+ function navExpander() {
288
+ if (typeof pathId === "undefined") return;
289
+ var done = false,
290
+ timer = setTimeout(postMessage, 500);
291
+ function postMessage() {
292
+ if (done) return;
293
+ clearTimeout(timer);
294
+ var opts = { action: "expand", path: pathId };
295
+ document.getElementById("nav").contentWindow.postMessage(opts, "*");
296
+ done = true;
297
+ }
298
+
299
+ window.addEventListener(
300
+ "message",
301
+ function (event) {
302
+ if (event.data === "navReady") postMessage();
303
+ return false;
304
+ },
305
+ false
306
+ );
307
+ }
308
+
309
+ function mainFocus() {
310
+ var hash = window.location.hash;
311
+ if (hash !== "" && $(hash)[0]) {
312
+ $(hash)[0].scrollIntoView();
313
+ }
314
+
315
+ setTimeout(function () {
316
+ $("#main").focus();
317
+ }, 10);
318
+ }
319
+
320
+ function navigationChange() {
321
+ // This works around the broken anchor navigation with the YARD template.
322
+ window.onpopstate = function () {
323
+ var hash = window.location.hash;
324
+ if (hash !== "" && $(hash)[0]) {
325
+ $(hash)[0].scrollIntoView();
326
+ }
327
+ };
328
+ }
329
+
330
+ $(document).ready(function () {
331
+ navResizer();
332
+ navExpander();
333
+ createSourceLinks();
334
+ createDefineLinks();
335
+ createFullTreeLinks();
336
+ searchFrameButtons();
337
+ linkSummaries();
338
+ summaryToggle();
339
+ constantSummaryToggle();
340
+ generateTOC();
341
+ mainFocus();
342
+ navigationChange();
343
+ });
344
+ })();