edifice 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,3 @@
1
1
  /*
2
2
  *= require edifice/framework
3
- *= require edifice/edifice_form
4
- *= require edifice/form
5
3
  */
@@ -1,3 +1,3 @@
1
1
  module Edifice
2
- VERSION = "0.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
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: 63
4
+ hash: 59
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 8
8
+ - 9
9
9
  - 0
10
- version: 0.8.0
10
+ version: 0.9.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-13 00:00:00 Z
20
+ date: 2011-10-14 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.
@@ -40,8 +40,6 @@ files:
40
40
  - README
41
41
  - Rakefile
42
42
  - app/assets/javascripts/edifice.js
43
- - app/assets/javascripts/edifice/edifice_form.js
44
- - app/assets/javascripts/edifice/form.js
45
43
  - app/assets/javascripts/edifice/framework.js
46
44
  - edifice.gemspec
47
45
  - init.rb
@@ -1,112 +0,0 @@
1
- // add methods to a form element so it can be easily manipulated
2
- //
3
- // add client side validation
4
- // understands:
5
- // - rails form structures
6
- // - rails form errors in JSON
7
- // - etc
8
- (function($) {
9
- $.fn.edifice_form = function() {
10
- $.extend(this, methods);
11
- return this;
12
- };
13
-
14
- // VALIDATORS:
15
- // validators expect a form element and return true or an error message
16
- $.fn.edifice_form.validators = {
17
- not_empty: function($input) {
18
- return $input.val() ? true : 'must not be empty';
19
- }
20
- };
21
-
22
-
23
- // in these 'this' is the $form
24
- var methods = {
25
- fields: function() {
26
- return this.find('input, textarea, select');
27
- },
28
-
29
- error_fields: function() {
30
- var $form = this;
31
- return this.fields().filter(function() {
32
- return $form.has_error($(this));
33
- });
34
- },
35
-
36
- submits: function() {
37
- return this.find('input[type=submit], button[type=submit]');
38
- },
39
-
40
- valid: function() {
41
- var $form = this, valid = true;
42
-
43
- this.fields().each(function() {
44
- if (!$form.validate($(this))) { valid = false; }
45
- });
46
-
47
- return valid;
48
- },
49
-
50
- // 'prepare' a validator --> validator returns true or an error string
51
- set_validator: function($field, validator) {
52
- if (typeof validator === 'string') {
53
- validator = $.fn.edifice_form.validators[validator] || validator;
54
- if (typeof validator !== 'function') { throw "Validator not defined: '" + validator + "'"; }
55
- };
56
- $field.data('validator', validator);
57
- },
58
-
59
- // validate a single field
60
- validate: function($field, validator) {
61
- if (typeof validator === 'undefined') { validator = $field.data('validator'); }
62
- if (validator) {
63
- var error = validator($field), valid = error === true;
64
- this.clear_error($field);
65
- if (!valid) {
66
- this.add_error($field, error);
67
- }
68
-
69
- return valid;
70
- } else {
71
- return true;
72
- }
73
- },
74
-
75
- label_for: function($field) {
76
- return this.find('label[for=' + $field.attr('id') + ']');
77
- },
78
-
79
- error_on: function($field) {
80
- return this.label_for($field).parents('.field_with_errors').next('.formError');
81
- },
82
-
83
- has_error: function($field) {
84
- return $field.parent('.field_with_errors').length > 0;
85
- },
86
-
87
- clear_error: function($field) {
88
- var id = $field.attr('id');
89
- if (this.has_error($field)) { $field.unwrap() }
90
-
91
- this.find('.field_with_errors label[for=' + id + ']')
92
- .unwrap()
93
- .next('.formError').remove();
94
- },
95
-
96
- add_error: function($field, error) {
97
- var id = $field.attr('id');
98
- $field.wrap('<div class="field_with_errors">');
99
- this.find('label[for=' + id + ']')
100
- .after('<div class="formError">' + error + '</div>')
101
- .wrap('<div class="field_with_errors">');
102
-
103
- },
104
-
105
- set_errors: function(errors) {
106
- for (var name in errors) {
107
- this.add_error(this.fields().filter('[name*=' + name + ']'), errors[name][0]);
108
- }
109
- }
110
- };
111
-
112
- }(jQuery));
@@ -1,137 +0,0 @@
1
- (function($) {
2
- var defaults = {
3
- status_element: this, // element to set to .submitting/.success/.error as we submit
4
- // a hash of input selector -> validation functions/names
5
- validators: {},
6
- // stop the form from submitting and use ajax
7
- ajax: true,
8
- // type of data to pass around (and thus what's seen by e.g. success)
9
- dataType: 'html'
10
- };
11
-
12
- // EVENTS that fire during the course of the life of the submission:
13
- // submit, invalid, success | error (+ user_error | server_error), complete
14
- //
15
- // -> success,errors + complete are all passed the data you would expect from
16
- // jQuery.ajax
17
- //
18
- // obviously on submit + invalid fire for non-ajax forms
19
-
20
- $.edifice_widgets.form = function() { return this.each(form); }
21
-
22
- function form() {
23
- var $form = $(this).edifice_form();
24
- $form.settings = $form.read_options(defaults);
25
- $.extend($form, methods);
26
-
27
- $form.initialize();
28
- }
29
- var methods = {
30
- initialize: function() {
31
- // I don't know why, but FF seems to helpful remember that these are disabled
32
- this.submits().removeAttr('disabled');
33
- this.prepare_validators();
34
- this.prepare_submit();
35
- },
36
-
37
- prepare_validators: function() {
38
- var $form = this;
39
-
40
- // setup validators from settings
41
- for (var selector in $form.settings.validators) {
42
- $form.set_validator($(selector), $form.settings.validators[selector]);
43
- }
44
-
45
- // setup validators from html
46
- $form.fields().filter('[data-widget-validator]').each(function() {
47
- $form.set_validator($(this), $(this).attr('data-widget-validator'));
48
- });
49
-
50
- // listen to validator
51
- this.fields().live('change.ajax_form focusout.ajax_form', function() {
52
- if ($form.validate($(this)) === false) { $form.trigger('invalid', [$form, $(this)]); }
53
- });
54
- },
55
-
56
- prepare_submit: function() {
57
- var $form = this;
58
- this.submit(function(event) {
59
- // do pre-submit validations
60
- if (!$form.valid()) {
61
- $form.invalid();
62
- return false; // we are done.
63
- }
64
-
65
- $form.submits().attr('disabled', true); // disable submit buttons
66
-
67
- // TODO - set status class
68
- if ($form.settings.ajax && $form.settings.ajax !== 'false') {
69
- // send up the form and process the results
70
- $.ajax({
71
- url: $form.attr('action'), type: $form.attr('method'),
72
- dataType: $form.settings.dataType,
73
- data: $.param($form.serializeArray()),
74
- cache: false,
75
- error: function (x, t, e) { $form.error(x, t, e); },
76
- success: function (data, status) {
77
- $form.trigger('success', [data, status, $form]);
78
- },
79
- complete: function (request, text_status) {
80
- $form.trigger('complete', [request, text_status, $form]);
81
-
82
- $form.submits().removeAttr('disabled');
83
- }
84
- });
85
- event.preventDefault();
86
- }
87
-
88
- });
89
- },
90
-
91
- invalid: function() {
92
- // focus the first error
93
- this.error_fields().eq(0).focus();
94
- this.trigger('invalid', [this]);
95
- },
96
-
97
- error: function(request, text_status, error) {
98
- this.trigger('error', [request, status, error, this]);
99
-
100
- // handle the different possible errors that we can see
101
- if (request.status >= 400 && request.status < 500) {
102
- // CLIENT ERROR -- server-side validation failed.
103
- this.trigger('client_error', [request, status, error, this]);
104
-
105
- // if data is html, we replace this content of the form with the content
106
- // of the form that we've just received back
107
- if (this.settings.dataType === 'html') {
108
- // wrap in a div incase there are a bunch of floating elements, pull the form out
109
- var $new_form = $('<div>').append(request.responseText).find('#' + this.attr('id'));
110
-
111
- this.html($new_form.html());
112
- this.prepare_validators();
113
-
114
- } else if (this.settings.dataType === 'json') {
115
- // we will be receiving an error object back, we can pass it straight into form.js
116
- this.set_errors($.parseJSON(request.responseText));
117
- } else {
118
- throw "Don't know how to handle dataType " + this.settings.dataType;
119
- }
120
-
121
- this.invalid();
122
- } else if (request.status >= 500 && request.status < 600) {
123
- // a SERVER ERROR -- something unrecoverable happened on the server
124
- this.trigger('server_error', [request, status, error, this]);
125
-
126
- // we aren't going to do anything here.
127
- // FIXME: we should probably have a way to set this behaviour at the application level.
128
- // for instance, you probably just want to redirect to somewhere, or show a dialog,
129
- // or popup something or.....?
130
- } else {
131
- // some other kind of error- something in the browser (e.g. navigating away)
132
- this.trigger('browser_error', [request, status, error, this]);
133
- }
134
- },
135
- }
136
-
137
- }(jQuery));