garlicjs-rails 1.0.3 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,9 +18,20 @@ Add the following directive to your Javascript manifest file (application.js):
18
18
 
19
19
  //= require garlic
20
20
 
21
+ ## Compatibility
22
+
23
+ Garlic.js relies on localStorage, which is only compatible with "modern" browsers: IE8+, Chrome 4+, FF 4+, Safari 4+, Opera 11+.
24
+ To add support for IE6 and IE7, add the `localstorageshim-rails` gem to your Gemfile:
25
+
26
+ gem 'localstorageshim-rails'
27
+
28
+ And add the following line to your application layout (must be before garlicjs is loaded):
29
+
30
+ <%= localstorage_shim %>
31
+
21
32
  ## Versioning
22
33
 
23
- garlicjs-rails 1.0.3 == Garlic.js 1.0.3
34
+ garlicjs-rails 1.1.1 == Garlic.js 1.1.1
24
35
 
25
36
  ## Contributing
26
37
 
@@ -1,5 +1,5 @@
1
1
  module Garlicjs
2
2
  module Rails
3
- VERSION = "1.0.3"
3
+ VERSION = "1.1.1"
4
4
  end
5
5
  end
@@ -84,16 +84,18 @@
84
84
  this.storage = storage;
85
85
  this.path = this.getPath();
86
86
  this.parentForm = this.$element.closest( 'form' );
87
+ this.$element.addClass('garlic-auto-save');
88
+ this.expiresFlag = !this.options.expires ? false : ( this.$element.data( 'expires' ) ? this.path : this.getPath( this.parentForm ) ) + '_flag' ;
87
89
 
88
- this.retrieve();
89
-
90
+ // bind garlic events
90
91
  this.$element.on( this.options.events.join( '.' + this.type + ' ') , false, $.proxy( this.persist, this ) );
91
92
 
92
93
  if ( this.options.destroy ) {
93
- this.$element.closest( 'form' ).on( 'submit reset' , false, $.proxy( this.destroy, this ) );
94
+ $( this.parentForm ).on( 'submit reset' , false, $.proxy( this.destroy, this ) );
94
95
  }
95
96
 
96
- this.$element.addClass('garlic-auto-save');
97
+ // retrieve garlic persisted data
98
+ this.retrieve();
97
99
  }
98
100
 
99
101
  , getOptions: function ( options ) {
@@ -104,6 +106,19 @@
104
106
 
105
107
  /* temporary store data / state in localStorage */
106
108
  , persist: function () {
109
+
110
+ // some binded events are redundant (change & paste for example), persist only once by field val
111
+ if ( this.val === this.$element.val() ) {
112
+ return;
113
+ }
114
+
115
+ this.val = this.$element.val();
116
+
117
+ // if auto-expires is enabled, set the expiration date for future auto-deletion
118
+ if ( this.options.expires ) {
119
+ this.storage.set( this.expiresFlag , ( new Date().getTime() + this.options.expires * 1000 ).toString() );
120
+ }
121
+
107
122
  // for checkboxes, we need to implement an unchecked / checked behavior
108
123
  if ( this.$element.is( 'input[type=checkbox]' ) ) {
109
124
  return this.storage.set( this.path , this.$element.attr( 'checked' ) ? 'checked' : 'unchecked' );
@@ -115,6 +130,18 @@
115
130
  /* retrieve localStorage data / state and update elem accordingly */
116
131
  , retrieve: function () {
117
132
  if ( this.storage.has( this.path ) ) {
133
+
134
+ // if data expired, destroy it!
135
+ if ( this.options.expires ) {
136
+ var date = new Date().getTime();
137
+ if ( this.storage.get( this.expiresFlag ) < date.toString() ) {
138
+ this.storage.destroy( this.path );
139
+ return;
140
+ } else {
141
+ this.$element.attr( 'expires-in', Math.floor( ( parseInt( this.storage.get( this.expiresFlag ) ) - date ) / 1000 ) );
142
+ }
143
+ }
144
+
118
145
  var storedValue = this.storage.get( this.path );
119
146
 
120
147
  // if conflictManager enabled, manage fields with already provided data, different from the one stored
@@ -245,16 +272,20 @@
245
272
  * other inputs: domain > pathname > form.<attr.name>[:eq(x)] > input.<attr.name>[:eq(y)]
246
273
  we just need the element name / eq() inside a given form
247
274
  */
248
- , getPath: function () {
275
+ , getPath: function ( elem ) {
276
+
277
+ if ( 'undefined' === typeof elem ) {
278
+ elem = this.$element;
279
+ }
249
280
 
250
281
  // Requires one element.
251
- if ( this.$element.length != 1 ) {
282
+ if ( elem.length != 1 ) {
252
283
  return false;
253
284
  }
254
285
 
255
286
  var path = ''
256
- , fullPath = this.$element.is( 'input[type=checkbox]' )
257
- , node = this.$element;
287
+ , fullPath = elem.is( 'input[type=checkbox]' )
288
+ , node = elem;
258
289
 
259
290
  while ( node.length ) {
260
291
  var realNode = node[0]
@@ -364,6 +395,7 @@
364
395
  , inputs: 'input, textarea, select' // Default supported inputs.
365
396
  , events: [ 'DOMAttrModified', 'textInput', 'input', 'change', 'keypress', 'paste', 'focus' ] // Events list that trigger a localStorage
366
397
  , domain: false // Store et retrieve forms data accross all domain, not just on
398
+ , expires: false // false for no expiration, otherwise (int) in seconds for auto-expiration
367
399
  , conflictManager: {
368
400
  enabled: true // Manage default data and persisted data. If false, persisted data will always replace default ones
369
401
  , garlicPriority: true // If form have default data, garlic persisted data will be shown first
@@ -383,4 +415,4 @@
383
415
  });
384
416
 
385
417
  // This plugin works with jQuery or Zepto (with data extension builded for Zepto. See changelog 0.0.6)
386
- }(window.jQuery || window.Zepto);
418
+ }(window.jQuery || window.Zepto);
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garlicjs-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-13 00:00:00.000000000 Z
12
+ date: 2012-12-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -58,7 +58,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
58
58
  version: '0'
59
59
  segments:
60
60
  - 0
61
- hash: -2086385096376470105
61
+ hash: 4582252746168385461
62
62
  required_rubygems_version: !ruby/object:Gem::Requirement
63
63
  none: false
64
64
  requirements:
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
67
  version: '0'
68
68
  segments:
69
69
  - 0
70
- hash: -2086385096376470105
70
+ hash: 4582252746168385461
71
71
  requirements: []
72
72
  rubyforge_project:
73
73
  rubygems_version: 1.8.24