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/static/old/old.js ADDED
@@ -0,0 +1,76 @@
1
+ function Hash(hash) {
2
+ hash = hash || {};
3
+ var _this = this;
4
+ $.each(hash, function(key){_this[key] = this});
5
+ };
6
+
7
+ decode_uri = function(uri){
8
+ return uri.replace(/\&/g,'&');
9
+ };
10
+
11
+
12
+ $.extend({
13
+ infoMessage: function(message){
14
+ log(message);
15
+ },
16
+
17
+ message: function(msg){
18
+ $.infoMessage(msg)
19
+ },
20
+
21
+ errorMessage: function(message){
22
+ log(message);
23
+ },
24
+
25
+ checkForMessages: function(json){
26
+ if(json['info']) $.infoMessage(json['info']);
27
+ if(json['error']) $.errorMessage(json['error']);
28
+ }
29
+ });
30
+
31
+ $.fn.extend({
32
+ serializeObject: function(){
33
+ var o = {};
34
+ var a = this.serializeArray();
35
+ $.each(a, function() {
36
+ if (o[this.name]) {
37
+ if (!o[this.name].push) {
38
+ o[this.name] = [o[this.name]];
39
+ }
40
+ o[this.name].push(this.value || '');
41
+ } else {
42
+ o[this.name] = this.value || '';
43
+ }
44
+ });
45
+ return o;
46
+ }
47
+ });
48
+
49
+ //
50
+ // jQuery Extension
51
+ //
52
+
53
+ $.fn.extend({
54
+
55
+
56
+ first: function(){
57
+ return $((this.size() > 0) ? this[0] : this)
58
+ },
59
+
60
+ last: function(){
61
+ return $((this.size() > 0) ? this[this.size() - 1] : this)
62
+ },
63
+
64
+ blank: function(){
65
+ return this.size() < 1
66
+ }
67
+ });
68
+
69
+ //
70
+ // jQuery Template
71
+ //
72
+ // try{
73
+ // $.template.regx.standard = /\#\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g;
74
+ // }catch(error){
75
+ // console.info('jquery.template not installed!');
76
+ // }
@@ -0,0 +1,273 @@
1
+ //
2
+ // Requirements
3
+ //
4
+
5
+ railsExt = 'defined';
6
+
7
+ //
8
+ // JavaScript Extension
9
+ //
10
+ String.prototype.blank = function() {
11
+ return this.replace(/[\s\n\r]+/, '').length == 0;
12
+ };
13
+
14
+ String.prototype.size = function() {
15
+ return this.length;
16
+ };
17
+
18
+ Array.prototype.size = function() {
19
+ return this.length;
20
+ };
21
+
22
+ function Hash(hash) {
23
+ hash = hash || {};
24
+ var _this = this;
25
+ $.each(hash, function(key){_this[key] = this});
26
+ };
27
+
28
+ Hash.prototype.size = function () {
29
+ var l = -1;
30
+ for (var k in this) {
31
+ l++;
32
+ }
33
+ return l;
34
+ };
35
+
36
+ inspect = function(data){
37
+ console.dir(data);
38
+ };
39
+
40
+ log = function(data){
41
+ console.log(data);
42
+ };
43
+
44
+ p = log;
45
+
46
+ warn = function(data){
47
+ console.warn(data);
48
+ }
49
+
50
+ decode_uri = function(uri){
51
+ return uri.replace(/\&amp;/g,'&');
52
+ };
53
+
54
+ //
55
+ // jQuery Extension
56
+ //
57
+ $.extend({
58
+ infoMessage: function(message){
59
+ log(message);
60
+ },
61
+
62
+ message: function(msg){
63
+ $.infoMessage(msg)
64
+ },
65
+
66
+ errorMessage: function(message){
67
+ log(message);
68
+ },
69
+
70
+ checkForMessages: function(json){
71
+ if(json['info']) $.infoMessage(json['info']);
72
+ if(json['error']) $.errorMessage(json['error']);
73
+ }
74
+ });
75
+
76
+ $.fn.extend({
77
+ serializeObject: function(){
78
+ var o = {};
79
+ var a = this.serializeArray();
80
+ $.each(a, function() {
81
+ if (o[this.name]) {
82
+ if (!o[this.name].push) {
83
+ o[this.name] = [o[this.name]];
84
+ }
85
+ o[this.name].push(this.value || '');
86
+ } else {
87
+ o[this.name] = this.value || '';
88
+ }
89
+ });
90
+ return o;
91
+ },
92
+
93
+ identify: function() {
94
+ var id = $(this).attr('id');
95
+ if(!id){
96
+ var i = 0;
97
+ do {
98
+ i++;
99
+ id = 'auto_id_' + i;
100
+ } while($('#' + id).length > 0);
101
+ $(this).attr('id', id);
102
+ }
103
+ return id;
104
+ },
105
+
106
+ first: function(){
107
+ return $((this.size() > 0) ? this[0] : this)
108
+ },
109
+
110
+ last: function(){
111
+ return $((this.size() > 0) ? this[this.size() - 1] : this)
112
+ },
113
+
114
+ blank: function(){
115
+ return this.size() < 1
116
+ }
117
+ });
118
+
119
+ //
120
+ // jQuery Template
121
+ //
122
+ // try{
123
+ // $.template.regx.standard = /\#\{([\w-]+)(?:\:([\w\.]*)(?:\((.*?)?\))?)?\}/g;
124
+ // }catch(error){
125
+ // console.info('jquery.template not installed!');
126
+ // }
127
+
128
+ //
129
+ // Initializers
130
+ //
131
+ $.extend({
132
+ callOnceForEvery: function(name, query, initializer, dependencyLevel){
133
+ if(!(name && query && initializer)) throw "Invalid arguments!";
134
+ this._addInitializer({name: name, query: query, initializer: initializer}, this._processOnceForEveryInitializer, false, dependencyLevel)
135
+ },
136
+
137
+ callOnceAfterUpdate: function(name, initializer, dependencyLevel){
138
+ this._addInitializer({name: name, initializer: initializer}, this._processOnceAfterUpdateInitializer, true, dependencyLevel)
139
+ },
140
+
141
+ callAfterUpdate: function(name, initializer, dependencyLevel){
142
+ this._addInitializer({name: name, initializer: initializer}, this._processAfterUpdateInitializer, false, dependencyLevel)
143
+ },
144
+
145
+ _orderedInitializers: [[], [], [], [], [], []],
146
+ _addInitializer: function(definition, processor, callOnce, dependencyLevel){
147
+ definition.name = '_' + definition.name + '_initialized';
148
+ dependencyLevel = dependencyLevel || 0;
149
+ if(!(dependencyLevel >= 0 && dependencyLevel <= 5)) throw "Not supported Dependency Level Value: '" + dependencyLevel + "'!";
150
+ this._orderedInitializers[dependencyLevel].push({definition: definition, processor: processor, callOnce: callOnce});
151
+ },
152
+
153
+ processInitializers: function(){
154
+ for(var i = this._orderedInitializers.size() - 1; i >= 0; i--){
155
+ var initializers = $(this._orderedInitializers[i]);
156
+ var toDelete = [];
157
+ initializers.each(function(i, v){
158
+ // log(v.definition.name);
159
+ v.processor(v.definition);
160
+ if(v.callOnce) toDelete.push(i);
161
+ });
162
+ $(toDelete.reverse()).each(function(){
163
+ initializers.splice(this, this + 1);
164
+ });
165
+ }
166
+ },
167
+
168
+ _processOnceForEveryInitializer: function(definition) {
169
+ var count = 0
170
+ $(definition.query).each(function(){
171
+ var e = $(this);
172
+ if(!e.hasClass(definition.name)){ // if(!e.data(definition.name)){
173
+ count = count + 1;
174
+ e.addClass(definition.name); // e.data(definition.name, true);
175
+ definition.initializer.apply($(this));
176
+ }
177
+ });
178
+ // log(definition.name + ', ' + definition.query + ': ' + $(definition.query).size() + '/' + count)
179
+ },
180
+
181
+ _processOnceAfterUpdateInitializer: function(definition){
182
+ definition.initializer();
183
+ },
184
+
185
+ _processAfterUpdateInitializer: function(definition){
186
+ definition.initializer();
187
+ }
188
+ });
189
+
190
+ $(function(){
191
+ $.processInitializers();
192
+ });
193
+ $(document).ajaxSuccess(function(){
194
+ $.processInitializers();
195
+ });
196
+
197
+
198
+ //
199
+ // AJAX
200
+ //
201
+ $.ajaxSetup({
202
+ authenticity_token: $.authenticity_token,
203
+ type: "script",
204
+ dataType: "script",
205
+ format: 'js'
206
+ });
207
+
208
+
209
+ //
210
+ // dataAttr
211
+ //
212
+ $.fn.extend({
213
+ dataAttr: function(name, value) {
214
+ var attr_name = 'data-' + name;
215
+ if(value){
216
+ this.attr(attr_name, value);
217
+ }else{
218
+ return this.attr(attr_name);
219
+ }
220
+ }
221
+ });
222
+
223
+
224
+ //
225
+ // link_to
226
+ //
227
+ $.fn.extend({
228
+ link_to: function(options) {
229
+ options = $.extend({url: $(this).attr('href'), method: 'get'}, options);
230
+ options.method = options.method.toLowerCase();
231
+ // if(options.method == 'put' || options.method == 'delete') options.method = 'post';
232
+ try{
233
+ if(options.ajax){
234
+ var data = {
235
+ _method: options.method,
236
+ target: $(this).identify()
237
+ };
238
+ if(options.method != 'get') data.authenticity_token = $.authenticity_token;
239
+ $.ajax({
240
+ data: $.param(data),
241
+ dataType: 'script',
242
+ type: options.method,
243
+ url: options.url
244
+ });
245
+ }else{
246
+ var url = $(this).attr('href');
247
+ var form = $("<form method='POST'>").attr("action", options.url);
248
+ var params = {'authenticity_token': $.authenticity_token, '_method': options.method}
249
+ $.each(params, function(name, value) {
250
+ $("<input type='hidden'>").attr("name", name).attr("value", value).appendTo(form);
251
+ });
252
+ form.appendTo("body");
253
+ form.submit();
254
+ }
255
+ }catch(e){console.log(e)};
256
+ }
257
+ });
258
+ // it doesn't works
259
+ // $(document).ajaxError(function (e, r, o, e) {
260
+ // console.log(e);
261
+ // });
262
+
263
+
264
+ //
265
+ // Helpers
266
+ //
267
+ // AJAX for autosubmitting changes
268
+ $.callOnceForEvery('autosubmit', '._autosubmit_on_change', function() {
269
+ var e = $(this);
270
+ e.bind('change', function(){
271
+ $.post(e.dataAttr('action'), {value: e.val(), target: e.identify()})
272
+ });
273
+ });