Lines 27-34
Link Here
|
27 |
|
27 |
|
28 |
/* decorate element as autofiller */ |
28 |
/* decorate element as autofiller */ |
29 |
var $inputgroup = $('<div class="input-prepend">'); |
29 |
var $inputgroup = $('<div class="input-prepend">'); |
30 |
self.$undo = $('<span class="add-on" style="display:none;cursor:pointer;">undo</span>'); |
30 |
self.$undo = $('<span class="add-on" style="display:none;cursor:pointer;">Undo</span>'); |
31 |
self.$fillbtn = $('<span class="add-on"><i class="icon-edit" style="cursor:pointer;"></i></span>'); |
31 |
self.$fillbtn = $('<span class="add-on" style="cursor:pointer;"> Search Google Books </span>'); |
32 |
self.$error = $('<span class="add-on" style="display:none;"></span>'); |
32 |
self.$error = $('<span class="add-on" style="display:none;"></span>'); |
33 |
self.$elm.wrap($inputgroup); |
33 |
self.$elm.wrap($inputgroup); |
34 |
self.$elm.after(self.$error); |
34 |
self.$elm.after(self.$error); |
Lines 63-149
Link Here
|
63 |
self.$error.hide(); |
63 |
self.$error.hide(); |
64 |
/* give user some feedback that the request is in progress */ |
64 |
/* give user some feedback that the request is in progress */ |
65 |
self.$fillbtn.fadeOut(1000).fadeIn(1000); |
65 |
self.$fillbtn.fadeOut(1000).fadeIn(1000); |
66 |
|
66 |
if ( self.$elm.val()) { |
67 |
var gAPI = 'https://www.googleapis.com/books/v1/volumes?q='; |
67 |
var gAPI = 'https://www.googleapis.com/books/v1/volumes?q='; |
68 |
if(self.type) |
68 |
if(self.type) |
69 |
gAPI += self.type + ':'; |
69 |
gAPI += self.type + ':'; |
70 |
gAPI += self.$elm.val(); |
70 |
gAPI += self.$elm.val(); |
71 |
gAPI += '&maxResults=1'; |
71 |
gAPI += '&maxResults=1'; |
72 |
|
72 |
$.getJSON(gAPI, function (response) { |
73 |
$.getJSON(gAPI, function (response) { |
73 |
if(response.totalItems == 0) { |
74 |
if(response.totalItems == 0) { |
74 |
self.$error.html('Sorry, nothing found.'); |
75 |
self.$error.html('Sorry, nothing found.'); |
75 |
self.$error.show(); |
76 |
self.$error.show(); |
76 |
return; |
77 |
return; |
|
|
78 |
} |
79 |
|
80 |
var undos = 0; |
81 |
var item = response.items[0]; |
82 |
for(var key in self.fields) { |
83 |
var filled = false; |
84 |
var value = eval('item.'+key); |
85 |
var field = self.fields[key]; |
86 |
|
87 |
/* field handled by caller */ |
88 |
if('handle' in field) { |
89 |
if(typeof field.handle === 'function') |
90 |
field.handle(field.$target, value); |
91 |
|
92 |
continue; /* next please */ |
93 |
} |
77 |
} |
94 |
|
78 |
|
95 |
/* wouldn't know what to do with result unless we have a |
79 |
var undos = 0; |
96 |
* target */ |
80 |
var item = response.items[0]; |
97 |
if( ! field.$target ) |
81 |
for(var key in self.fields) { |
98 |
continue; |
82 |
var filled = false; |
99 |
|
83 |
var value = eval('item.'+key); |
100 |
/* handle differently depending on datatype */ |
84 |
var field = self.fields[key]; |
101 |
switch(typeString(value)) { |
85 |
|
102 |
case 'array': |
86 |
/* field handled by caller */ |
103 |
switch(field.$target.prop('nodeName').toUpperCase()) { |
87 |
if('handle' in field) { |
104 |
case 'TEXTAREA': |
88 |
if(typeof field.handle === 'function') |
105 |
undos++; |
89 |
field.handle(field.$target, value); |
106 |
field.$target.bind('autofill-undo', field.$target.text(), function(e){$(this).text(e.data);}); |
90 |
|
107 |
field.$target.text(value.join(', ')); |
91 |
continue; /* next please */ |
108 |
break; |
92 |
} |
109 |
case 'INPUT': |
93 |
|
110 |
default: |
94 |
/* wouldn't know what to do with result unless we have a |
111 |
undos++; |
95 |
* target */ |
112 |
field.$target.bind('autofill-undo', field.$target.val(), function(e){$(this).val(e.data);}); |
96 |
if( ! field.$target ) |
113 |
field.$target.val(value.join(', ')); |
97 |
continue; |
114 |
break; |
98 |
|
115 |
} |
99 |
/* handle differently depending on datatype */ |
116 |
break; |
100 |
switch(typeString(value)) { |
117 |
default: |
101 |
case 'array': |
118 |
switch(field.$target.prop('nodeName').toUpperCase()) { |
102 |
switch(field.$target.prop('nodeName').toUpperCase()) { |
119 |
case 'TEXTAREA': |
103 |
case 'TEXTAREA': |
120 |
undos++; |
104 |
undos++; |
121 |
field.$target.bind('autofill-undo', field.$target.text(), function(e){$(this).text(e.data);}); |
105 |
field.$target.bind('autofill-undo', field.$target.text(), function(e){$(this).text(e.data);}); |
122 |
field.$target.text(value); |
106 |
field.$target.text(value.join(', ')); |
123 |
break; |
107 |
break; |
124 |
case 'SELECT': |
108 |
case 'INPUT': |
125 |
case 'INPUT': |
109 |
default: |
126 |
default: |
110 |
undos++; |
127 |
undos++; |
111 |
field.$target.bind('autofill-undo', field.$target.val(), function(e){$(this).val(e.data);}); |
128 |
field.$target.bind('autofill-undo', field.$target.val(), function(e){$(this).val(e.data);}); |
112 |
field.$target.val(value.join(', ')); |
129 |
field.$target.val(value); |
113 |
break; |
130 |
break; |
114 |
} |
131 |
} |
115 |
break; |
|
|
116 |
default: |
117 |
switch(field.$target.prop('nodeName').toUpperCase()) { |
118 |
case 'TEXTAREA': |
119 |
undos++; |
120 |
field.$target.bind('autofill-undo', field.$target.text(), function(e){$(this).text(e.data);}); |
121 |
field.$target.text(value); |
122 |
break; |
123 |
case 'SELECT': |
124 |
case 'INPUT': |
125 |
default: |
126 |
undos++; |
127 |
field.$target.bind('autofill-undo', field.$target.val(), function(e){$(this).val(e.data);}); |
128 |
field.$target.val(value); |
129 |
break; |
130 |
} |
131 |
} |
132 |
|
133 |
switch(field.effect) { |
134 |
case 'flash': |
135 |
field.$target.fadeOut(500).fadeIn(500); |
136 |
break; |
137 |
} |
132 |
} |
138 |
} |
133 |
|
139 |
|
134 |
switch(field.effect) { |
140 |
if(undos > 0) |
135 |
case 'flash': |
141 |
self.$undo.show(); |
136 |
field.$target.fadeOut(500).fadeIn(500); |
142 |
|
137 |
break; |
143 |
}); |
138 |
} |
|
|
139 |
} |
144 |
} |
140 |
|
145 |
}); |
141 |
if(undos > 0) |
146 |
}; |
142 |
self.$undo.show(); |
|
|
143 |
|
144 |
}); |
145 |
}); |
146 |
}; |
147 |
|
147 |
|
148 |
/* |
148 |
/* |
149 |
* @fields object: Google Books API item propreties map for |
149 |
* @fields object: Google Books API item propreties map for |
150 |
- |
|
|