rad_js 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -5,6 +5,7 @@ project(
5
5
  official_name: "rad_js",
6
6
  gem: true,
7
7
  summary: "JavaScript support for Rad Framework (MooTools, jQuery)",
8
+ dirs: 'static',
8
9
 
9
10
  author: "Alexey Petrushin",
10
11
  homepage: "http://github.com/alexeypetrushin/rad_js"
@@ -0,0 +1,136 @@
1
+ //
2
+ // rad.assets.require '/vendor/mootools.js'
3
+ //
4
+
5
+ //
6
+ // Utility functions
7
+ //
8
+ var p=function(){
9
+ return console.log.apply(console, arguments);
10
+ };
11
+
12
+ //
13
+ // String extensions
14
+ //
15
+ String.implement({
16
+ blank: function(){return this.test(/^\s*$/);},
17
+ size: function(){return this.length;},
18
+ toElements: function(bang){
19
+ var list;
20
+ if (this.match(/^\s*</)){
21
+ list = Elements.from("" + this);
22
+ }else{
23
+ list = $$("" + this);
24
+ };
25
+ if(bang && (list.size() == 0)) throw "no elements '" + this + "'!";
26
+ return list;
27
+ },
28
+ toElement: function(bang){
29
+ var e = this.toElements().first();
30
+ if(bang && !e) throw "no element '" + this + "'!";
31
+ return e
32
+ }
33
+ });
34
+
35
+
36
+ //
37
+ // Array extensions
38
+ //
39
+ Array.implement({
40
+ any: function(){return this.some.apply(this, arguments);},
41
+ add: function(){return this.push.apply(this, arguments);},
42
+ blank: function(){return this.length == 0;},
43
+ size: function(){return this.length;},
44
+ first: function(){return this[0];},
45
+ last: function(){return this[this.length-1];},
46
+ reject: function(fn, bind){return this.filter(function(){return !fn.apply(this, arguments)});},
47
+ none: function(fn, bind){return !this.any.apply(this, arguments);},
48
+ clear: function(){return this.empty.apply(this, arguments);},
49
+ });
50
+
51
+
52
+ //
53
+ // Elements
54
+ //
55
+ Elements.implement({
56
+ applyOnce: function(identifier, fn){this.invoke('applyOnce', identifier, fn);},
57
+ // appendElement: function(obj){this.invoke('append', obj);},
58
+ toElements: function(bang){
59
+ if(bang && (this.size() == 0)) throw "no elements!";
60
+ return this;
61
+ },
62
+ toElement: function(bang){
63
+ if(bang && (this.size() == 0)) throw "no element!";
64
+ return this.first();
65
+ }
66
+ // last: function(){return this[this.length-1];}
67
+ // show: function(){this.invoke('show');},
68
+ // hide: function(){this.invoke('hide');},
69
+ });
70
+
71
+
72
+ //
73
+ // Element extensions
74
+ //
75
+ Element.implement({
76
+ getChild: function(){return this.getChildren.apply(this, arguments).first();},
77
+ getElement: function(){return this.getElements.apply(this, arguments).first();},
78
+ // appendElement: function(obj){obj.toElements().inject(this);},
79
+ applyOnce: function(identifier, fn){
80
+ if(!this.retrieve(identifier)){
81
+ fn.apply(this, [this]);
82
+ this.store(identifier, 'initialized');
83
+ }
84
+ },
85
+ classNames: function(){
86
+ return this.className.clean().split(/\s+/);
87
+ },
88
+ toElements: function(bang){return new Elements(this)},
89
+ toElement: function(bang){return this;},
90
+ replaceWith: function(obj){obj.toElements(true).replaces(this);},
91
+ // append: function(obj){obj.toElements().inject(this)},
92
+ identify: function(){
93
+ var id = this.get('id');
94
+ if(!id){
95
+ var i = 0;
96
+ do {
97
+ i++;
98
+ id = 'auto_id_' + i;
99
+ } while($(id));
100
+ this.set('id', id);
101
+ }
102
+ return id;
103
+ },
104
+ getData: function(name, value){
105
+ return this.getProperty('data-' + name);
106
+ },
107
+ setData: function(name, value){
108
+ this.setProperty('data-' + name, value);
109
+ return this;
110
+ },
111
+ getInnerSize: function(){
112
+ var dim = this.getComputedSize();
113
+ return {x: dim.width, y: dim.height};
114
+ },
115
+ getOuterSize: function(){
116
+ var dim = this.getComputedSize({styles: ['margin', 'border', 'padding']});
117
+ return {x: dim.totalWidth, y: dim.totalHeight};
118
+ },
119
+ getSpaceSize: function(){
120
+ var dim = this.getComputedSize({styles: ['margin', 'border', 'padding']});
121
+ return {x: dim.computedLeft + dim.computedRight, y: dim.computedTop + dim.computedBottom};
122
+ },
123
+ highlight: function(){
124
+ this.setStyle('border', "5px solid yellow");
125
+ },
126
+ hover: function(fn1,fn2) {
127
+ return this.addEvents({
128
+ mouseenter: function(e) {
129
+ fn1.apply(this, [e]);
130
+ },
131
+ mouseleave: function(e) {
132
+ fn2.apply(this, [e]);
133
+ }
134
+ });
135
+ }
136
+ });
@@ -0,0 +1,3 @@
1
+ .hidden{display: none !important;}
2
+ .todo{background: #ffa;}
3
+ .dev_border{border: 1px solid red;}
data/static/lib/rad.js ADDED
@@ -0,0 +1,162 @@
1
+ //
2
+ // rad.assets.require '/lib/mootools.extensions.js'
3
+ //
4
+
5
+
6
+ Rad = new Class({
7
+ initialize: function(){
8
+ this.deferred = [];
9
+ },
10
+ defer: function(fn){
11
+ this.deferred.push(fn);
12
+ },
13
+ callDeferred: function(){
14
+ this.deferred.each(function(fn){fn()});
15
+ this.deferred = [];
16
+ }
17
+ });
18
+
19
+ //
20
+ // Request
21
+ //
22
+ Rad.Request = new Class({
23
+ Extends: Request,
24
+
25
+ options: {
26
+ method: 'post',
27
+ data: {authenticity_token: window.AUTHENTICITY_TOKEN},
28
+ evalScripts: true,
29
+ evalResponse: true,
30
+ },
31
+
32
+ success: function(text, xml){
33
+ try{
34
+ this.fireEvent('load', arguments);
35
+ this.parent(text, xml);
36
+ }catch(e){console.log(e)};
37
+ window.fireEvent('updated');
38
+ window.fireEvent('updateStyle');
39
+ },
40
+
41
+ failure: function(){
42
+ this.parent();
43
+ console.log('Rad.Request failed!');
44
+ window.fireEvent('updated');
45
+ window.fireEvent('updateStyle');
46
+ }
47
+ });
48
+
49
+
50
+ //
51
+ // Rad
52
+ //
53
+ Rad.implement({
54
+ call: function(url, data, options){
55
+ options = options || {};
56
+ options.url = url;
57
+ options.data = Object.merge({}, options.data, data);
58
+ var request = new Rad.Request(options);
59
+ request.send();
60
+ },
61
+
62
+ submitFormViaXhr: function(form, data, callback){
63
+ // callback = callback || function(){};
64
+ // var url = form.get('action');
65
+ // var formData = form.toQueryString().parseQueryString();
66
+ //
67
+ // options = {
68
+ // url: url,
69
+ // data: Object.merge({}, formData, data), // , {format: 'js'}),
70
+ // onLoad: callback
71
+ // };
72
+ // var request = new Rad.Request(options);
73
+ // request.send();
74
+
75
+ callback = callback || function(){};
76
+ var url = form.get('action');
77
+
78
+ var stringData = form.toQueryString();
79
+ if(data) stringData = stringData + '&' + Object.toQueryString(data);
80
+
81
+ options = {
82
+ url: url,
83
+ data: stringData,
84
+ onLoad: callback
85
+ };
86
+ var request = new Rad.Request(options);
87
+ request.send();
88
+ },
89
+
90
+ // submitForm: function(form, data, options){
91
+ // var url = form.get('action');
92
+ // var formData = form.toQueryString().parseQueryString();
93
+ //
94
+ // options = options || {};
95
+ // options.url = url;
96
+ // options.data = Object.merge({}, options.data, formData, data, {format: 'js'});
97
+ // var request = new Rad.Request(options);
98
+ // request.send();
99
+ // },
100
+
101
+ process_js_link: function(event){
102
+ var target = event.target;
103
+
104
+ // some element shouldn't be autosubmitted as soon as You click on it, selets for example
105
+ if(Rad.not_js_links.contains(target.get('tag'))) return;
106
+
107
+ var confirmMessage = target.getProperty('data-confirm');
108
+ if(confirmMessage && !confirm(confirmMessage)){
109
+ event.stop();
110
+ return;
111
+ };
112
+
113
+ var action = target.getProperty('data-action');
114
+ if(action){
115
+ event.stop();
116
+
117
+ var method = target.getProperty('data-method') || 'post';
118
+ var remote = target.getProperty('data-remote');
119
+ var target_id = target.identify && target.identify();
120
+
121
+ if(remote){
122
+ rad.call(action, {_method: method, _target: target_id});
123
+ }else{
124
+ var form = new Element('form', {method: 'post', action: action});
125
+ var params = {'authenticity_token': window.AUTHENTICITY_TOKEN, '_method': options.method};
126
+ params.each(function(name, value){
127
+ var input = new Element('input', {type: 'hidden', name: name, value: value});
128
+ input.inject(form);
129
+ });
130
+ document.body.inject(form);
131
+ form.submit();
132
+ }
133
+ };
134
+ }
135
+ });
136
+ Rad.not_js_links = ['select']
137
+ Rad.implement({
138
+ submitForm: Rad.prototype.submitFormViaXhr
139
+ });
140
+
141
+ rad = new Rad();
142
+
143
+
144
+ //
145
+ // JavaScript links
146
+ //
147
+ window.addEvent('domready', function(){
148
+ document.body.addEvent('click', function(event){
149
+ rad.process_js_link(event);
150
+ });
151
+ });
152
+
153
+ //
154
+ // Events
155
+ //
156
+ window.addEvent('domready', function(){
157
+ window.fireEvent('updated');
158
+ window.fireEvent('updateStyle');
159
+ });
160
+ window.addEvent('resize', function(){
161
+ window.fireEvent('updateStyle');
162
+ });