edifice 0.7.3 → 0.8.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.
@@ -13,6 +13,17 @@ jQuery.noConflict();
13
13
  @description jQuery Plugins
14
14
  */
15
15
  (function($){
16
+ /**
17
+ * Extract the view_path/name and layout from the meta tags (as put down by Edifice::Helper::edifice_meta_tags)
18
+ *
19
+ */
20
+ function extract_page_details_from_meta_tags() {
21
+ return {
22
+ view_path: $('meta[name=edifice-view_path]').attr('content'),
23
+ view_name: $('meta[name=edifice-view_name]').attr('content'),
24
+ layout: $('meta[name=edifice-layout]').attr('content')
25
+ };
26
+ }
16
27
  /**
17
28
  * Runs an 'event method' on a view object and a layout object defined by meta tags
18
29
  * we specially set in every layout.
@@ -20,12 +31,21 @@ jQuery.noConflict();
20
31
  * @param {String} methodName The method to call on the view object corresponding to event that's happening.
21
32
  */
22
33
  function page_level_hookup(methodName) {
23
- var view_path = $('meta[name=edifice-view_path]').attr('content');
24
- var view_name = $('meta[name=edifice-view_name]').attr('content');
25
- var layout = $('meta[name=edifice-layout]').attr('content');
26
-
27
- hookup(methodName, view_path, view_name, layout);
34
+ hookup(methodName, extract_page_details_from_meta_tags());
28
35
  };
36
+
37
+ /**
38
+ * Extract the view_path/name and layout from the headers of an ajax request
39
+ *
40
+ * @param {XMLHttpRequest} request The ajax request
41
+ */
42
+ function extract_page_details_from_ajax(request) {
43
+ return {
44
+ view_path: request.getResponseHeader('x-edifice-view_path'),
45
+ view_name: request.getResponseHeader('x-edifice-view_name'),
46
+ layout: request.getResponseHeader('x-edifice-layout')
47
+ };
48
+ }
29
49
 
30
50
  /**
31
51
  * Runs an 'event method' on a view object and a layout object defined by the headers
@@ -35,11 +55,7 @@ jQuery.noConflict();
35
55
  * @param {XMLHttpRequest} request The returning ajax request.
36
56
  */
37
57
  function ajax_hookup(methodName, request) {
38
- var view_path = request.getResponseHeader('x-edifice-view_path');
39
- var view_name = request.getResponseHeader('x-edifice-view_name');
40
- var layout = request.getResponseHeader('x-edifice-layout');
41
-
42
- hookup(methodName, view_path, view_name, layout);
58
+ hookup(methodName, extract_page_details_from_ajax(request));
43
59
  }
44
60
 
45
61
  /**
@@ -48,11 +64,12 @@ jQuery.noConflict();
48
64
  * Will call annosList.onLoad() and layoutsApplication.onLoad() if they exist.
49
65
  *
50
66
  * @param {String} methodName The method to call on the view object corresponding to event that's happening.
51
- * @param {String} view_path The camelcased path to the view, often the same as the controller name.
52
- * @param {String} view_name The name of the view being rendered.
53
- * @param {String} layout The layout being used. If there is no layout this is 'no_layout'
67
+ * @param {Object} page_details The page details as returned by extract_page_details_from methods. Contains:
68
+ * view_path The camelcased path to the view, often the same as the controller name.
69
+ * view_name The name of the view being rendered.
70
+ * layout The layout being used. If there is no layout this is 'no_layout'
54
71
  */
55
- function hookup(methodName, view_path, view_name, layout) {
72
+ function hookup(methodName, page_details) {
56
73
  //capitalize the first character in a string
57
74
  function capitalize_first_chr(str) {
58
75
  if ((typeof(str) == 'undefined') || (str.length == 0)) {
@@ -63,9 +80,9 @@ jQuery.noConflict();
63
80
  }
64
81
 
65
82
  // sometimes, on errors, etc these may not be set
66
- view_path = view_path || 'no_controller';
67
- view_name = view_name || 'no_view'
68
- layout = layout || 'no_layout'
83
+ var view_path = page_details.view_path || 'no_controller',
84
+ view_name = page_details.view_name || 'no_view',
85
+ layout = page_details.layout || 'no_layout';
69
86
 
70
87
  hookup_once(view_path + capitalize_first_chr(view_name), methodName); // hookup the view
71
88
  hookup_once('layouts' + capitalize_first_chr(layout), methodName);
@@ -84,11 +101,34 @@ jQuery.noConflict();
84
101
  }
85
102
  }
86
103
  };
87
-
104
+
105
+ /**
106
+ * Updates classes set on the body tag (as set by Edifice::Helper::edifice_body_classes),
107
+ * and meta tags (as set by Edifice::Helper::edifice_meta_tags)
108
+ *
109
+ * @param {Object} page_details The page details as used in hookup above.
110
+ */
111
+ function update_page_details(page_details) {
112
+ function ucc(string) {
113
+ return string.replace(/([A-Z])/g, '_$1').toLowerCase();
114
+ }
115
+
116
+ $('meta[name=edifice-view_path]').attr('content', page_details.view_path);
117
+ $('meta[name=edifice-view_name]').attr('content', page_details.view_name);
118
+ $('meta[name=edifice-layout]').attr('content', page_details.layout);
119
+
120
+ $('body').attr('class', function(i, classes) {
121
+ // remove any other classes beginning with c_, v_ or l_
122
+ classes = classes.replace(/[cvl]_.*/g, '')
123
+ return classes + ' c_' + ucc(page_details.view_path) +
124
+ ' v_' + ucc(page_details.view_name) + ' l_' + ucc(page_details.layout);
125
+ });
126
+ }
127
+
88
128
  //when the dom is ready
89
129
  $(document).ready(function() {
90
130
  page_level_hookup('onReady');
91
- $.attach_widgets();
131
+ $('body').attach_widgets().attach_traits();
92
132
  page_level_hookup('onWidgetsReady');
93
133
  $(document).trigger('widgetsReady');
94
134
  });
@@ -103,30 +143,74 @@ jQuery.noConflict();
103
143
  $('body').ajaxComplete(function(event, request) {
104
144
  ajax_hookup('onAjaxComplete', request);
105
145
  page_level_hookup('onAjaxComplete');
106
- $.attach_widgets();
146
+ $('body').attach_widgets().attach_traits();
107
147
  ajax_hookup('onWidgetsReady', request);
108
148
  page_level_hookup('onWidgetsReady');
109
149
  $(document).trigger('widgetsReady');
110
150
  });
151
+
152
+ // special code for dealing with pjax requests
153
+ $('body').bind('pjax:end', function(event, request) {
154
+ // we need to update any body classes based on the request headers
155
+ update_page_details(extract_page_details_from_ajax(request));
156
+ });
111
157
  });
112
158
 
159
+ // *********** EDIFICE TRAIT CODE ************ //
113
160
 
114
- // *********** EDIFICE WIDGET CODE *********** //
161
+ /**
162
+ @name $.edifice_traits
163
+ @namespace
164
+ @description Traits live here.
165
+ */
166
+ $.edifice_traits = {};
167
+
168
+ /**
169
+ * Runs $.edifice.traits.X on all contained elements with data-trait containing X
170
+ *
171
+ * @param {Boolean} Reset the checks to see if things have already been attached
172
+ * [use this if you have clone an element without copying events]
173
+ *
174
+ * Records which elements have already been 'traited' via data-trait-attached
175
+ */
176
+ $.fn.attach_traits = function(reset) {
177
+ if (reset) { this.find('[data-trait]').removeAttr('data-trait-attached'); }
115
178
 
179
+ for (trait in $.edifice_traits) {
180
+ var $els = this.find('[data-trait~=' + trait + ']:not([data-trait-attached~=' + trait + '])');
181
+ $els.attr('data-trait-attached', function(i, val) {
182
+ return (val ? val + ' ' : '') + trait;
183
+ });
184
+ $.edifice_traits[trait].call($els);
185
+ }
186
+ return this;
187
+ }
188
+
189
+
190
+ // *********** EDIFICE WIDGET CODE *********** //
191
+
116
192
  /**
117
193
  @name $.edifice_widgets
118
194
  @namespace
119
195
  @description Our widgets live here.
120
196
  */
121
197
  $.edifice_widgets = {};
122
-
198
+
123
199
  /**
124
200
  * Runs attach_widget() on any widget found in the html which isn't already attached.
201
+ *
202
+ * @param {Boolean} Reset the checks to see if things have already been attached
203
+ * [use this if you have clone an element without copying events]
204
+ *
125
205
  */
126
- $.attach_widgets = function() {
127
- $('[data-widget]:not([data-widget-attached])').attach_widget();
206
+ $.fn.attach_widgets = function(reset) {
207
+ if (reset) { this.find('[data-widget]').removeAttr('data-widget-attached'); }
208
+
209
+ this.find('[data-widget]:not([data-widget-attached])').attach_widget();
210
+
211
+ return this;
128
212
  };
129
-
213
+
130
214
  /**
131
215
  * Call $.WIDGET_NAME on the matched elements where WIDGET_NAME is set in the
132
216
  * data-widget attribute.
@@ -0,0 +1,5 @@
1
+ /*
2
+ *= require edifice/framework
3
+ *= require edifice/edifice_form
4
+ *= require edifice/form
5
+ */
data/init.rb CHANGED
@@ -1,9 +1,5 @@
1
1
  require 'edifice'
2
2
 
3
- config.to_prepare do
4
- Edifice.install_js_files
5
- end
6
-
7
3
  ActionController::Base.send :include, Edifice::Controller
8
4
  # ActionMailer::Base.send :include, Edifice::Controller
9
5
  ActionView::Base.send :include, EdificeHelper
@@ -2,7 +2,7 @@ require 'edifice'
2
2
  require 'rails'
3
3
 
4
4
  module Edifice
5
- class Railtie < Rails::Railtie
5
+ class Engine < Rails::Engine
6
6
  rake_tasks do
7
7
  load "tasks/edifice.rake"
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module Edifice
2
- VERSION = "0.7.3"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/edifice.rb CHANGED
@@ -5,15 +5,7 @@ require 'edifice/form_model'
5
5
  require 'edifice/responder'
6
6
 
7
7
  module Edifice
8
- require 'edifice/railtie' if defined?(Rails)
9
-
10
- def self.install_js_files
11
- install_dir = ::Rails.application.paths.public.javascripts.first
12
- edifice_js_dir = File.join(File.dirname(__FILE__), 'public', 'javascripts', 'edifice')
13
-
14
- FileUtils.rm_r File.join(install_dir, 'edifice')
15
- FileUtils.cp_r edifice_js_dir, install_dir
16
- end
8
+ require 'edifice/engine' if defined?(Rails)
17
9
  end
18
10
 
19
11
  ActionController::Base.send :include, Edifice::Controller
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: edifice
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 63
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 7
9
- - 3
10
- version: 0.7.3
8
+ - 8
9
+ - 0
10
+ version: 0.8.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tom Coleman
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-10-07 00:00:00 Z
20
+ date: 2011-10-13 00:00:00 Z
21
21
  dependencies: []
22
22
 
23
23
  description: The idea here is to communicate which view is rendering to the javascript so that we can call the correct javascript files in an automagical way.
@@ -39,19 +39,20 @@ files:
39
39
  - MIT-LICENSE
40
40
  - README
41
41
  - Rakefile
42
+ - app/assets/javascripts/edifice.js
43
+ - app/assets/javascripts/edifice/edifice_form.js
44
+ - app/assets/javascripts/edifice/form.js
45
+ - app/assets/javascripts/edifice/framework.js
42
46
  - edifice.gemspec
43
47
  - init.rb
44
48
  - lib/edifice.rb
45
49
  - lib/edifice/controller.rb
50
+ - lib/edifice/engine.rb
46
51
  - lib/edifice/form_model.rb
47
52
  - lib/edifice/helper.rb
48
- - lib/edifice/railtie.rb
49
53
  - lib/edifice/renderer.rb
50
54
  - lib/edifice/responder.rb
51
55
  - lib/edifice/version.rb
52
- - lib/public/javascripts/edifice/edifice_form.js
53
- - lib/public/javascripts/edifice/form.js
54
- - lib/public/javascripts/edifice/framework.js
55
56
  - lib/tasks/edifice.rake
56
57
  - test/rails3/.gitignore
57
58
  - test/rails3/Gemfile