window_rails 0.2.12 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,50 +0,0 @@
1
- class WindowRailsController < ApplicationController
2
-
3
- unloadable
4
-
5
- # Make sure we kick any thing out that is making a request other
6
- # than openning a new window
7
- before_filter :redirect_out, :except => :open_window
8
- def redirect_out
9
- respond_to do |format|
10
- format.html do
11
- redirect_to '/'
12
- end
13
- format.js do
14
- render :update do |page|
15
- page.redirect_to '/'
16
- end
17
- end
18
- end
19
- end
20
-
21
- # Opens a new window
22
- # NOTE: It is important to note that the contents of the window will be opened
23
- # within an IFrame. This means links within the window will not be able to
24
- # communicate with the page around it (like closing the window)
25
- def open_window
26
- respond_to do |format|
27
- format.html do
28
- redirect_to '/'
29
- end
30
- format.js do
31
- opts = {}
32
- content = nil
33
- if(params[:window_url].blank?)
34
- content = {:url => params[:iframe_url]}
35
- else
36
- content = {:content_url => params[:window_url]}
37
- end
38
- base_key = params[:window_options] ? :window_options : 'window_options'
39
- if(params[base_key])
40
- params[base_key].keys.each do |key|
41
- opts[key.to_sym] = params[base_key][key]
42
- end
43
- end
44
- render :update do |page|
45
- page.open_window(content, opts || {})
46
- end
47
- end
48
- end
49
- end
50
- end
data/config/routes.rb DELETED
@@ -1,9 +0,0 @@
1
- if(Rails.version.split('.').first.to_i < 3)
2
- ActionController::Routing::Routes.draw do |map|
3
- map.open_window 'window_rails/open_window', :controller => :window_rails, :action => :open_window
4
- end
5
- else
6
- Rails.application.routes.draw do
7
- match 'window_rails/open_window' => 'window_rails#open_window', :as => :open_window
8
- end
9
- end
@@ -1,451 +0,0 @@
1
- require 'rails_javascript_helpers'
2
-
3
- module WindowRailsGenerators
4
-
5
- include RailsJavaScriptHelpers
6
- include ActionView::Helpers::JavaScriptHelper
7
- include ActionView::Helpers::TagHelper
8
-
9
- # msg:: Alert message
10
- # options:: Hash of options values for the alert window
11
- # Opens an alert window
12
- def open_alert_window(msg, options={})
13
- options[:width] ||= 200
14
- options[:modal] = true
15
- options[:title] ||= 'Alert!'
16
- options[:buttons] ||= {'OK' => 'function(){ jQuery(this).dialog("close");}'}
17
- store_content(msg, 'alert_modal');
18
- self << "
19
- if(jQuery('#alert_modal').size() == 0){
20
- jQuery('<div id=\"alert_modal\"></div>')
21
- .html(window_rails_contents['alert_modal'])
22
- .dialog(#{format_type_to_js(options)});
23
- } else {
24
- jQuery('#alert_modal')
25
- .html(window_rails_contents['alert_modal'])
26
- .dialog(#{format_type_to_js(options)}).focus();
27
- }"
28
- nil
29
- end
30
-
31
- # Closes alert window
32
- def close_alert_window
33
- self << 'jQuery("#alert_modal").dialog("close");'
34
- nil
35
- end
36
-
37
- # msg:: Confirmation message
38
- # options:: Hash of options values for the confirmation window
39
- # Opens a confirmation window
40
- def open_confirm_window(msg, options={})
41
- options[:width] ||= 200
42
- options[:modal] = true
43
- options[:title] ||= 'Confirm'
44
- options[:buttons] ||= {'OK' => 'function(){ jQuery(this).dialog("close");}'}
45
- store_content(msg, 'confirmation_modal')
46
- self << "
47
- if(jQuery('#confirmation_modal').size() == 0){
48
- jQuery('<div id=\"confirmation_modal\"></div>')
49
- .html(window_rails_contents['confirmation_modal'])
50
- .dialog(#{format_type_to_js(options)});
51
- } else {
52
- jQuery('#confirmation_modal')
53
- .html(window_rails_contents['confirmation_modal'])
54
- .dialog(#{format_type_to_js(options)}).focus();
55
- }"
56
- nil
57
- end
58
-
59
- # msg:: Information message
60
- # options:: Hash of options values for info window
61
- # Open an information window
62
- def open_info_window(msg, options={})
63
- options[:width] ||= 200
64
- options[:modal] = true
65
- options[:title] ||= 'Information'
66
- options[:close_on_escape] = false
67
- store_content(msg, 'information_modal')
68
- self << "
69
- if(jQuery('#information_modal').size() == 0){
70
- jQuery('<div id=\"information_modal\"></div>')
71
- .html(window_rails_contents['information_modal'])
72
- .dialog(#{format_type_to_js(options)});
73
- } else {
74
- jQuery('#information_modal')
75
- .html(window_rails_contents['information_modal'])
76
- .dialog(#{format_type_to_js(options)}).focus();
77
- }"
78
- nil
79
- end
80
-
81
- # Close an information window
82
- def close_info_window
83
- self << 'jQuery("#information_modal").dialog("close");'
84
- nil
85
- end
86
-
87
- # Update the contents of an information window
88
- def update_info_window(msg)
89
- self << 'jQuery("#information_modal").html(#{format_type_to_js(msg)});'
90
- nil
91
- end
92
-
93
- def window_setup
94
- @_window_rails_setup ||= false
95
- unless(@set)
96
- self << '
97
- if(typeof(window.window_rails_windows) == "undefined") {
98
- window.window_rails_windows = {};
99
- } if(typeof(window_rails_windows_array) == "undefined") {
100
- window.window_rails_windows_array = new Array();
101
- }
102
- '
103
- end
104
- @_window_rails_setup = true
105
- nil
106
- end
107
-
108
- def window(dom_id, *args)
109
- window_setup
110
- unless(dom_id.blank?)
111
- dom_id = dom_id.to_s.dup
112
- dom_id.slice!(0) if dom_id.start_with?('#')
113
- output = "window.window_rails_windows['#{dom_id}']"
114
- else
115
- output = "window.window_rails_windows[window.window_rails_windows_array[0]]"
116
- end
117
- if(args.include?(:raw))
118
- output
119
- else
120
- self << output
121
- self
122
- end
123
- end
124
-
125
- # content:: content
126
- # options:: Hash of options
127
- # * window -> Name of the window to update (defaults to last window)
128
- # * error -> Show error if window is not found (defaults false)
129
- # Updates the window with the new content. If the content is a string, it will
130
- # be placed into the window. If it is a Hash, it will be fed to render and the
131
- # result will be placed in the window (basically an easy way to integrate partials:
132
- # page.update_window(:partial => 'my_parital'))
133
- def update_window(content, options={})
134
- window_setup
135
- win = options.delete(:window)
136
- error = options.delete(:error)
137
- key = store_content(content)
138
- self << check_for_window(win, error){ update_window_contents(key, win, options) }
139
- nil
140
- end
141
-
142
- # name:: Name of the window
143
- # error:: Will throw an alert if window is not found
144
- # Checks for a window of the given name. If a block is provided, it will be executed
145
- # if the window is found
146
- def check_for_window(name, error=true)
147
- name = name.to_s.dup
148
- name.slice!(0) if name.start_with?('#')
149
- window_setup
150
- if(name.blank?)
151
- self << "if(window.window_rails_windows.length > 0){"
152
- else
153
- self << "if(window.window_rails_windows['#{name}']){"
154
- end
155
- yield if block_given?
156
- self << "}"
157
- self << "else { alert('Unexpected error. Failed to locate window for output.'); }" if error
158
- nil
159
- end
160
-
161
- # key:: Content key location
162
- # win:: Name of window
163
- # Updates the contents of the window. If no window name is provided, the topmost window
164
- # will be updated
165
- def update_window_contents(key, win, options=nil)
166
- window(win) << ".html('<div id=\"#{win}_wr_content\">' + window.window_rails_contents[#{format_type_to_js(key.to_s)}] + '</div>');"
167
- end
168
-
169
- # win:: Name of window
170
- # padding:: Extra padding to add to calculated height and width
171
- def resize_to_fit(win, padding=20)
172
- if(padding.is_a?(Hash))
173
- w_pad = padding[:width_pad]
174
- h_pad = padding[:height_pad]
175
- else
176
- w_pad = h_pad = padding
177
- end
178
- auto_resize(win, h_pad, :height)
179
- auto_resize(win, w_pad, :width)
180
- center_window(win)
181
- end
182
-
183
- # win:: Name of window
184
- # Recenters window
185
- def center_window(win)
186
- window(win) << ".dialog('option', 'position', 'center');"
187
- end
188
-
189
- # win:: Name of window
190
- # extra_padding:: Extra padding to add to calculated size
191
- # args:: Symbols for what to resize. Valid: :height and :width
192
- def auto_resize(win, extra_padding, *args)
193
- win = win.start_with?('#') ? win : "##{win}"
194
- args.each do |item|
195
- self << "
196
- var _wr_max = Math.max.apply(Math, jQuery('#{win}_wr_content *').map(function(){ return jQuery(this).#{item}(); }).get());
197
- if(jQuery('#{win}').#{item}() < _wr_max || jQuery('#{win}').#{item}() > _wr_max){"
198
- window(win) << ".dialog('option', '#{item}', _wr_max + #{extra_padding.to_i});"
199
- self << "
200
- }"
201
- end
202
- end
203
-
204
- # win:: Name of window
205
- # Will focus the window. If no name provided, topmost window will be focused
206
- def focus_window(win=nil)
207
- window(win) << '.dialog().dialog("focus");'
208
- nil
209
- end
210
-
211
- # win:: Name of window
212
- # Will maximize the window. If no name provided, topmost window will be maximized
213
- def maximize_window(win=nil)
214
- # noop
215
- end
216
-
217
- # win:: Name of window
218
- # Will minimize the window. If no name provided, topmost window will be minimized
219
- def minimize_window(win)
220
- # noop
221
- end
222
-
223
- # key:: Content key location
224
- # win:: Name of window
225
- # options:: Options to be passed onto window
226
- # Creates a new window. Generally this should not be called,
227
- # rather #open_window should be used
228
- # TODO: The below line causes an error in IE. Fix up to be compatible
229
- # so we aren't storing worthless junk:
230
- # window.window_rails_windows_array.splice(window.window_rails_windows_array.indexOf('#{win}'), 1);
231
- def create_window(key, win, options)
232
- window_setup
233
- options[:auto_open] ||= false
234
- options[:close] = "function(event,ui){
235
- #{
236
- if(options[:close])
237
- "callback = #{options[:close]}; callback();"
238
- end
239
- }
240
- jQuery('##{win}').dialog('destroy');
241
- window.window_rails_windows['#{win}'] = null;
242
- jQuery('##{win}').remove();
243
- jQuery('##{win}_wr_content').remove();
244
- }"
245
- unless(win.is_a?(String))
246
- win = win.to_s
247
- end
248
- if(win.start_with?('#'))
249
- win.slice!(0)
250
- end
251
- window_setup
252
- self << "
253
- if(jQuery('#window_rails_windows').size() == 0){
254
- jQuery('body').append('<div id=\"window_rails_windows\" style=\"visibility:hidden;display:none;\"></div>');
255
- }
256
- if(jQuery('##{win}').size() == 0){
257
- jQuery('#window_rails_windows').append('<div id=\"#{win}\"><div id=\"#{win}_wr_content\"></div></div>');
258
- }
259
- window.window_rails_windows['#{win}'] = jQuery('##{win}');
260
- window.window_rails_windows_array.push('#{win}');
261
- "
262
- if(key == :url)
263
- options[:auto_open] = true
264
- self << "
265
- jQuery.get(
266
- '#{options.delete(:url)}',
267
- function(data){
268
- jQuery('##{win}_wr_content')
269
- .html(data)
270
- .dialog(#{format_type_to_js(options)})
271
- .dialog('open');
272
- },
273
- 'html'
274
- )
275
- "
276
- else
277
- window(win) << ".dialog(#{format_type_to_js(options)});"
278
- update_window_contents(key, win)
279
- end
280
- nil
281
- end
282
-
283
- # win:: Name of window
284
- # modal:: True if window should be modal
285
- # Shows the window centered on the screen. If no window name is provided
286
- # it will default to the last created window. Generally this should not
287
- # be called, rather #open_window should be used
288
- # NOTE: No modal mode currently
289
- def show_window(win, modal)
290
- modal ||= false
291
- check_for_window(win) do
292
- window(win) << ".dialog('option', 'modal', #{format_type_to_js(modal)});"
293
- window(win) << ".dialog('option', 'autoOpen', true);"
294
- window(win) << '.dialog("open");'
295
- end
296
- nil
297
- end
298
-
299
- # content:: Content for window
300
- # options:: Hash of options
301
- # * :modal -> Window should be modal
302
- # * :window -> Name to reference window (used for easy updating and closing)
303
- # * :constraints -> Hash containing top,left,right,bottom values for padding from edges. False value will turn off constraints (window can travel out of viewport)
304
- # * :iframe -> URL to load within an IFrame in the window
305
- # * :width -> Width of the window
306
- # * :height -> Height of the window
307
- # * :no_update -> Set to true to force creation of a new window even if window of same name already exists (defaults to false)
308
- # Creates a new window and displays it at the center of the viewport. Content can be provided as
309
- # a string, or as a Hash. If :url is defined, the window will be loaded with the contents of the request. If not, the hash
310
- # will be passed to #render and the string will be displayed.
311
- # Important note:
312
- # There are two ways to load remote content. The first is to use :url => 'http://host/page.html' for content. This will
313
- # load the contents directly into the window. The second is to set content to nil and pass :iframe => 'http://host/page.html'
314
- # in the options. This later form will load the contents within an IFrame in the window. This is useful in that it will
315
- # be isolated from the current page, but this isolation means it cannot communicate with other windows on the page (including
316
- # its own).
317
- def open_window(content, options={})
318
- window_setup
319
- key = nil
320
- if(options[:iframe])
321
- iframe = @context.url_for(options.delete(:iframe))
322
- end
323
- if(content.is_a?(Hash))
324
- if(content[:url])
325
- options[:iframe] = @context.url_for(content[:url])
326
- content = nil
327
- elsif(content[:content_url])
328
- options[:url] = @context.url_for(content[:content_url])
329
- content = nil
330
- else
331
- content = @context.render(content)
332
- end
333
- end
334
- options[:width] ||= 300
335
- options[:height] ||= 200
336
- modal = options[:modal]
337
- if(options[:on_close])
338
- options[:close] = options.delete(:on_close)
339
- end
340
- if(options[:iframe])
341
- key = :iframe
342
- elsif(options[:url])
343
- key = :url
344
- else
345
- key = store_content(content)
346
- end
347
- win = options.delete(:window) || "win_#{rand(99999)}"
348
- if(options.delete(:no_update))
349
- create_window(key, win, options)
350
- show_window(win, modal)
351
- else
352
- check_for_window(win, false) do
353
- update_window_contents(key, win, options)
354
- focus_window(win)
355
- end
356
- else_block do
357
- create_window(key, win, options)
358
- show_window(win, modal)
359
- end
360
- end
361
- nil
362
- end
363
-
364
- def store_content(content, key=nil)
365
- unless(key)
366
- key = rand.to_s
367
- key.slice!(0,2)
368
- end
369
- c = content.is_a?(Hash) ? @context.render(content) : content.to_s
370
- self << "if(typeof(window.window_rails_contents) == 'undefined'){ window.window_rails_contents = {}; }"
371
- self << "window.window_rails_contents[#{format_type_to_js(key.to_s)}] = #{format_type_to_js(c)};"
372
- key
373
- end
374
-
375
- # options:: Hash of options
376
- # * :window -> name of window to close
377
- # Close the window of the provided name or the last opened window
378
- def close_window(options = {})
379
- win_name = if(options.is_a?(Hash))
380
- [(options[:window] || options[:name]).to_s]
381
- elsif(options.is_a?(Array))
382
- options.map(&:to_s)
383
- else
384
- [options.to_s]
385
- end
386
- win_name.each do |win|
387
- self << check_for_window(win, false){ self << "#{window(win, :raw)}.dialog('close');"}
388
- end
389
- end
390
-
391
- # Close all open windows
392
- def close_all_windows
393
- setup_windows
394
- self << '
395
- jQuery.each(
396
- window_rails_windows_array,
397
- function(name, win){
398
- window.window_rails_windows[win].dialog("close");
399
- }
400
- );
401
- '
402
- end
403
-
404
- # args:: List of window names to refresh (All will be refreshed if :all is included)
405
- # Refresh window contents
406
- # NOTE: Currently does nothing
407
- # NOTE: Deprecated (remaining temporarily for compatibility reasons)
408
- def refresh_window(*args)
409
- return
410
- self << "var myWin = null;"
411
- if(args.include?(:all))
412
- self << "Windows.windows.values().each(function(win){ win.refresh(); });"
413
- else
414
- args.each do |win|
415
- self << "myWin = Windows.getWindowByName('#{escape_javascript(win.to_s)}');"
416
- self << "if(myWin){ myWin.refresh(); }"
417
- end
418
- end
419
- end
420
-
421
- # field_id:: DOM ID of form element to observe
422
- # options:: Options
423
- # Helper for observing fields that have been dynamically loaed into the DOM. Works like
424
- # #observe_field but not as full featured.
425
- # NOTE: Currently does nothing
426
- # NOTE: Deprecated (remaining temporarily for compatibility reasons)
427
- def observe_dynamically_loaded_field(field_id, options={}) # :nodoc
428
- f = options.delete(:function)
429
- unless(f)
430
- f = "function(event){ new Ajax.Request('#{escape_javascript(@context.url_for(options[:url]).to_s)}', {asynchronous:true, evalScripts:true,parameters:'#{escape_javascript(options[:with].to_s)}='+$('#{escape_javascript(options[:with].to_s)}').getValue()})}"
431
- else
432
- f = "function(event){ #{f}; }"
433
- end
434
- self << "if($('#{escape_javascript(field_id.to_s)}')){ $('#{escape_javascript(field_id.to_s)}').observe('change', #{f}); }"
435
- end
436
-
437
- private
438
-
439
- # Simply wraps a block within an else statement
440
- def else_block
441
- self << "else {"
442
- yield if block_given?
443
- self << "}"
444
- nil
445
- end
446
-
447
- end
448
-
449
- if(defined?(ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods))
450
- ActionView::Helpers::PrototypeHelper::JavaScriptGenerator::GeneratorMethods.send :include, WindowRailsGenerators
451
- end