mera 0.1.5 → 0.1.6

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.
@@ -6,4 +6,133 @@
6
6
  *
7
7
  */
8
8
 
9
- $(function(){$.mera=function(options){$('a[data-skip-mera]').mera(options)};jQuery.fn.mera=function(options){$location=location.pathname;$absoluteUrl=location.href;$this=$(this);$default={before:function(){$('body').hide()},after:function(){$('body').fadeIn()},error:function(){window.location.replace('/404.html')},confirme:function(question){return confirm(question)},compress:true,autoload:true};$options=$.extend({},$default,options);$this.click(function(){window.location.href=$(this).attr('href')});$('a:not([data-remote=true])').click(function(){var hrefMera=$(this).attr('href');if($(this).attr('data-method')!==undefined){$.rails.handleMethodRemote($(this))}else{$.rails.handleRemote($(this))}history.pushState({url:$location},document.title,hrefMera);return false});$('form:not([data-remote=true])').on('submit',function(){$.rails.handleRemote($(this));return false});$(window).bind('popstate',function(event){if(location.href!==$absoluteUrl){window.location.replace(location.href)}});if($.inArray('state',$.event.props)<0){$.event.props.push('state')}$(window).on('ajax:beforeSend',function(event,xhr,settings){$options.before();xhr.setRequestHeader('X-Mera','enabled')});$(window).on('ajax:success',function(event,data,status){$.meraUpdater(data);$options.after()});$.meraUpdater=function(data){var resBody=data.split(/.*<body.*>.*/)[1].split(/.*<\/body>.*/)[0];var resHead=data.split(/.*<head.*>.*/)[1].split(/.*<\/head>.*/)[0];if($options.compress){resBody=$.whiteSpaceRemover(resBody);resHead=$.whiteSpaceRemover(resHead)}if($options.autoload){$('head').html(resHead)}else{if(!$.isHeadSame(resHead)){$('head').html(resHead)}}$('body').html(resBody)};$.whiteSpaceRemover=function(data){data=data.replace(/ /g,'');data=data.replace(/\n/g,'');data=$.trim(data);return data};$.isHeadSame=function(head){var ident="head link, head meta, head script";return $(ident).count==$(ident,$(head)).count};$.rails.handleMethodRemote=function(link){var href=$.rails.href(link),method=link.data('method'),target=link.attr('target'),confirmLink=link.attr('data-confirm'),csrf_token=$('meta[name=csrf-token]').attr('content'),csrf_param=$('meta[name=csrf-param]').attr('content'),form=$('<form method="post" action="'+href+'"></form>'),metadata_input='<input name="_method" value="'+method+'" type="hidden" />';if(csrf_param!==undefined&&csrf_token!==undefined){metadata_input+='<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />'}if(target){form.attr('target',target)}form.hide().append(metadata_input).appendTo('body');if(confirmLink!==undefined&&$options.confirme(confirmLink)){$.rails.handleRemote(form)}}}});
9
+
10
+ $(function(){
11
+
12
+ $.mera = function(options){
13
+ $('a[data-skip-mera]').mera(options);
14
+ };
15
+
16
+ jQuery.fn.mera = function(options) {
17
+ //get actual location
18
+ $location = location.pathname;
19
+ //get current url
20
+ $absoluteUrl = location.href;
21
+ //get this identifier
22
+ $this = $(this);
23
+ //get default options
24
+ $default = {
25
+ before : function(){$('body').hide();},
26
+ after : function(){$('body').fadeIn();},
27
+ error : function(){window.location.replace('/404.html')},
28
+ confirme : function(question){return confirm(question);},
29
+ compress: true,
30
+ autoload: true
31
+ };
32
+ //get user options
33
+ $options = $.extend({}, $default, options);
34
+
35
+ $this.click(function(){
36
+ window.location.href = $(this).attr('href');
37
+ });
38
+
39
+ //handle a click
40
+ $('a:not([data-remote=true])').click(function(){
41
+ var hrefMera = $(this).attr('href');
42
+
43
+ if($(this).attr('data-method') !== undefined){
44
+ $.rails.handleMethodRemote($(this));
45
+ } else {
46
+ $.rails.handleRemote($(this));
47
+ }
48
+
49
+ history.pushState({url: $location}, document.title, hrefMera);
50
+
51
+ return false
52
+ });
53
+
54
+ //handle all form
55
+ $('form:not([data-remote=true])').on('submit', function(){
56
+ $.rails.handleRemote($(this));
57
+ return false
58
+ });
59
+
60
+ $(window).bind('popstate', function(event){
61
+ if(location.href !== $absoluteUrl){
62
+ window.location.replace(location.href);
63
+ }
64
+ });
65
+
66
+ if ($.inArray('state', $.event.props) < 0){
67
+ $.event.props.push('state');
68
+ }
69
+
70
+ $(window).on('ajax:beforeSend', function(event, xhr, settings) {
71
+ $options.before();
72
+ xhr.setRequestHeader('X-Mera', 'enabled');
73
+ });
74
+
75
+ $(window).on('ajax:success', function(event, data, status) {
76
+ $.meraUpdater(data);
77
+ $options.after();
78
+ });
79
+
80
+ $.meraUpdater = function(data){
81
+ var resBody = data.split(/.*<body.*>.*/)[1].split(/.*<\/body>.*/)[0];
82
+ var resHead = data.split(/.*<head.*>.*/)[1].split(/.*<\/head>.*/)[0];
83
+
84
+ if($options.compress){
85
+ resBody = $.whiteSpaceRemover(resBody);
86
+ resHead = $.whiteSpaceRemover(resHead);
87
+ }
88
+
89
+ if($options.autoload){
90
+ $('head').html(resHead);
91
+ } else {
92
+ if(!$.isHeadSame(resHead)){
93
+ $('head').html(resHead);
94
+ }
95
+ }
96
+
97
+ $('body').html(resBody);
98
+ };
99
+
100
+ $.whiteSpaceRemover = function(data){
101
+ data = data.replace(/ /g,'');
102
+ data = data.replace(/\n/g,'');
103
+ data = $.trim(data);
104
+
105
+ return data;
106
+ };
107
+
108
+ $.isHeadSame = function(head) {
109
+ var ident = "head link, head meta, head script";
110
+ return $(ident).count == $(ident, $(head)).count;
111
+ };
112
+
113
+ $.rails.handleMethodRemote = function(link) {
114
+ var href = $.rails.href(link),
115
+ method = link.data('method'),
116
+ target = link.attr('target'),
117
+ confirmLink = link.attr('data-confirm'),
118
+ csrf_token = $('meta[name=csrf-token]').attr('content'),
119
+ csrf_param = $('meta[name=csrf-param]').attr('content'),
120
+ form = $('<form method="post" action="' + href + '"></form>'),
121
+ metadata_input = '<input name="_method" value="' + method + '" type="hidden" />';
122
+
123
+ if (csrf_param !== undefined && csrf_token !== undefined) {
124
+ metadata_input += '<input name="' + csrf_param + '" value="' + csrf_token + '" type="hidden" />';
125
+ }
126
+
127
+ if (target) { form.attr('target', target); }
128
+
129
+ form.hide().append(metadata_input).appendTo('body');
130
+
131
+ if(confirmLink !== undefined && $options.confirme(confirmLink)){
132
+ $.rails.handleRemote(form);
133
+ }
134
+ }
135
+
136
+ };
137
+ });
138
+
@@ -0,0 +1,9 @@
1
+ /**
2
+ *
3
+ * My Easy Rails Ajax
4
+ * Open under GPL (General Public License)
5
+ * Author Richard Hutta 2012
6
+ *
7
+ */
8
+
9
+ $(function(){$.mera=function(options){$('a[data-skip-mera]').mera(options)};jQuery.fn.mera=function(options){$location=location.pathname;$absoluteUrl=location.href;$this=$(this);$default={before:function(){$('body').hide()},after:function(){$('body').fadeIn()},error:function(){window.location.replace('/404.html')},confirme:function(question){return confirm(question)},compress:true,autoload:true};$options=$.extend({},$default,options);$this.click(function(){window.location.href=$(this).attr('href')});$('a:not([data-remote=true])').click(function(){var hrefMera=$(this).attr('href');if($(this).attr('data-method')!==undefined){$.rails.handleMethodRemote($(this))}else{$.rails.handleRemote($(this))}history.pushState({url:$location},document.title,hrefMera);return false});$('form:not([data-remote=true])').on('submit',function(){$.rails.handleRemote($(this));return false});$(window).bind('popstate',function(event){if(location.href!==$absoluteUrl){window.location.replace(location.href)}});if($.inArray('state',$.event.props)<0){$.event.props.push('state')}$(window).on('ajax:beforeSend',function(event,xhr,settings){$options.before();xhr.setRequestHeader('X-Mera','enabled')});$(window).on('ajax:success',function(event,data,status){$.meraUpdater(data);$options.after()});$.meraUpdater=function(data){var resBody=data.split(/.*<body.*>.*/)[1].split(/.*<\/body>.*/)[0];var resHead=data.split(/.*<head.*>.*/)[1].split(/.*<\/head>.*/)[0];if($options.compress){resBody=$.whiteSpaceRemover(resBody);resHead=$.whiteSpaceRemover(resHead)}if($options.autoload){$('head').html(resHead)}else{if(!$.isHeadSame(resHead)){$('head').html(resHead)}}$('body').html(resBody)};$.whiteSpaceRemover=function(data){data=data.replace(/ /g,'');data=data.replace(/\n/g,'');data=$.trim(data);return data};$.isHeadSame=function(head){var ident="head link, head meta, head script";return $(ident).count==$(ident,$(head)).count};$.rails.handleMethodRemote=function(link){var href=$.rails.href(link),method=link.data('method'),target=link.attr('target'),confirmLink=link.attr('data-confirm'),csrf_token=$('meta[name=csrf-token]').attr('content'),csrf_param=$('meta[name=csrf-param]').attr('content'),form=$('<form method="post" action="'+href+'"></form>'),metadata_input='<input name="_method" value="'+method+'" type="hidden" />';if(csrf_param!==undefined&&csrf_token!==undefined){metadata_input+='<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />'}if(target){form.attr('target',target)}form.hide().append(metadata_input).appendTo('body');if(confirmLink!==undefined&&$options.confirme(confirmLink)){$.rails.handleRemote(form)}}}});
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mera
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -34,6 +34,7 @@ extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
36
  - lib/assets/javascripts/mera.js
37
+ - lib/assets/javascripts/mera.min.js
37
38
  - lib/mera.rb
38
39
  homepage:
39
40
  licenses: []