| 1 |
|
| 2 |
/** |
| 3 |
* Constructor |
| 4 |
* @param selector string (required), jQuery selector of the form which needs to be intercepted |
| 5 |
* @param message string (required), message displayed on the output box |
| 6 |
* @param target string (required), jQuery selector corresponding to the HTML element that will be after the output box |
| 7 |
* @param event string (optional), form event to listen. Possible values: submit, live. Default: submit. |
| 8 |
*/ |
| 9 |
function IscBlockForm(selector, message, target, event) |
| 10 |
{ |
| 11 |
// Private attributes |
| 12 |
var instance = this; |
| 13 |
if ('undefined' == typeof(IscBlockForm.IFB_counter)) { |
| 14 |
IscBlockForm.IFB_counter = 0; |
| 15 |
} |
| 16 |
var instance_id = IscBlockForm.IFB_counter; |
| 17 |
|
| 18 |
IscBlockForm.IFB_counter++; |
| 19 |
|
| 20 |
if ('string' != typeof(selector)) { |
| 21 |
throw 'Param 1 of IscBlockForm must be a string'; |
| 22 |
} |
| 23 |
var main_selector = selector; |
| 24 |
|
| 25 |
if ('string' != typeof(message)) { |
| 26 |
throw 'Param 2 of IscBlockForm must be a string'; |
| 27 |
} |
| 28 |
var text = message; |
| 29 |
|
| 30 |
if ('string' != typeof(target)) { |
| 31 |
throw 'Param 3 of IscBlockForm must be a string'; |
| 32 |
} |
| 33 |
var target_selector = target; |
| 34 |
|
| 35 |
var l_event = 'submit'; |
| 36 |
if ('live' == event) { |
| 37 |
l_event = 'live'; |
| 38 |
} |
| 39 |
var fields = {}; |
| 40 |
var main_filter = undefined; |
| 41 |
|
| 42 |
// Private methods |
| 43 |
|
| 44 |
/** |
| 45 |
* Scroll the window to the warning box |
| 46 |
*/ |
| 47 |
var jump_to = function(elem) { |
| 48 |
pos = elem.offset(); |
| 49 |
jQuery(document).scrollTop(pos.top); |
| 50 |
jQuery(document).scrollLeft(pos.left); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Display the warning box |
| 55 |
*/ |
| 56 |
var show_box = function() { |
| 57 |
var box_id = 'IBF-' + instance_id; |
| 58 |
if (0 < jQuery('#' + box_id).length) |
| 59 |
jQuery('#' + box_id).remove(); |
| 60 |
d = jQuery('<div />').attr({id : box_id}).css({ |
| 61 |
border : '1px solid #eca', |
| 62 |
backgroundColor : '#feb', |
| 63 |
borderRadius: '2px', |
| 64 |
height : '0%', |
| 65 |
opacity: 0, |
| 66 |
color: '#f30', |
| 67 |
}); |
| 68 |
m = jQuery('<p />').text(text).css({ |
| 69 |
padding : '3px 5px', |
| 70 |
margin: '5px', |
| 71 |
fontWeight: 'bold', |
| 72 |
}); |
| 73 |
jQuery(target_selector).before(d.append(m)); |
| 74 |
|
| 75 |
if ('live' != l_event) |
| 76 |
jump_to(d); |
| 77 |
|
| 78 |
d.animate( |
| 79 |
{opacity: 1, height: '100%'}, |
| 80 |
1500, |
| 81 |
'swing' |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* remove the warning box |
| 87 |
*/ |
| 88 |
var remove_box = function() { |
| 89 |
var box_id = 'IBF-' + instance_id; |
| 90 |
d = jQuery('#' + box_id); |
| 91 |
if (0 < d.length) { |
| 92 |
d.fadeOut(1500, function(){ |
| 93 |
d.remove(); |
| 94 |
}); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Update value of fields |
| 100 |
*/ |
| 101 |
var update_fields = function() { |
| 102 |
for (var id in fields) { |
| 103 |
elem = jQuery(fields[id].selector); |
| 104 |
tagname = elem.prop('tagName').toLowerCase(); |
| 105 |
switch (tagname) { |
| 106 |
case 'input' : |
| 107 |
type = elem.attr('type'); |
| 108 |
switch (type) { |
| 109 |
case 'text': |
| 110 |
fields[id].type = 'text'; |
| 111 |
fields[id].value = elem.val(); |
| 112 |
break; |
| 113 |
case 'checkbox': |
| 114 |
fields[id].type = 'checkbox'; |
| 115 |
fields[id].value = elem.prop('checked'); |
| 116 |
break; |
| 117 |
default : |
| 118 |
break; |
| 119 |
} |
| 120 |
break; |
| 121 |
|
| 122 |
case 'select' : |
| 123 |
break; |
| 124 |
|
| 125 |
case 'textarea' : |
| 126 |
break; |
| 127 |
|
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// Public methods |
| 133 |
|
| 134 |
/** |
| 135 |
* Check the form with the filter |
| 136 |
*/ |
| 137 |
this.is_valid = function() { |
| 138 |
update_fields(); |
| 139 |
return main_filter(fields); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* Register a field to be tested. |
| 145 |
* @param field_id string (required), unique id for this registred field |
| 146 |
* @param selector string (required), jQuery selector corresponding to the field. Id or name attributes are good. |
| 147 |
*/ |
| 148 |
this.register_field = function(field_id, selector) { |
| 149 |
if ('string' != typeof(selector)) { |
| 150 |
throw 'Param 1 of IscBlockForm.addfield must be a string'; |
| 151 |
} |
| 152 |
if ('undefined' != typeof(fields[field_id])) { |
| 153 |
throw 'duplicated id ' + field_id; |
| 154 |
} |
| 155 |
if (1 < jQuery(selector).length) { |
| 156 |
throw 'the jQuery selector ' + selector + ' match more than one element.'; |
| 157 |
} |
| 158 |
fields[field_id] = |
| 159 |
{ |
| 160 |
'selector' : selector, |
| 161 |
'value' : undefined, |
| 162 |
'type' : undefined |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Set the main filter function. |
| 168 |
* @param main_filter_cb callback (required), when the function returns false, the submission is intercepted and the output box displayed. |
| 169 |
* This callback accepts one parameter: the fields object. |
| 170 |
*/ |
| 171 |
this.filter = function(main_filter_cb) { |
| 172 |
main_filter = main_filter_cb; |
| 173 |
}; |
| 174 |
|
| 175 |
/** |
| 176 |
* Attach the event defined in arguments |
| 177 |
*/ |
| 178 |
this.attach = function() { |
| 179 |
switch (l_event) { |
| 180 |
case 'submit' : |
| 181 |
jQuery(main_selector).live('submit', function(){ |
| 182 |
if (instance.is_valid()) { |
| 183 |
return true; |
| 184 |
} else { |
| 185 |
show_box(); |
| 186 |
return false; |
| 187 |
} |
| 188 |
}); |
| 189 |
break; |
| 190 |
case 'live' : |
| 191 |
for (var id in fields) { |
| 192 |
jQuery(fields[id].selector).live('change', function(){ |
| 193 |
if (instance.is_valid()) { |
| 194 |
remove_box(); |
| 195 |
} else { |
| 196 |
show_box(); |
| 197 |
} |
| 198 |
}); |
| 199 |
} |
| 200 |
break; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* The main program |
| 207 |
*/ |
| 208 |
jQuery(function(){ |
| 209 |
if (isc_data.warning_nosource) { |
| 210 |
classic_form = new IscBlockForm('#post', isc_data.block_form_message, '.compat-attachment-fields', 'submit'); |
| 211 |
classic_form.register_field('source', '#post .compat-field-isc_image_source input'); |
| 212 |
classic_form.register_field('own', '#post .compat-field-isc_image_source_own input'); |
| 213 |
classic_form.filter(function(data){ |
| 214 |
if ('' == data['source'].value && false == data['own'].value) { |
| 215 |
return false; |
| 216 |
} else { |
| 217 |
return true; |
| 218 |
} |
| 219 |
}); |
| 220 |
classic_form.attach(); |
| 221 |
|
| 222 |
live_form = new IscBlockForm('.compat-item', isc_data.block_form_message, '.compat-attachment-fields', 'live'); |
| 223 |
live_form.register_field('source', '.compat-item .compat-field-isc_image_source input'); |
| 224 |
live_form.register_field('own', '.compat-item .compat-field-isc_image_source_own input'); |
| 225 |
live_form.filter(function(data){ |
| 226 |
if ('' == data['source'].value && false == data['own'].value) { |
| 227 |
return false; |
| 228 |
} else { |
| 229 |
return true; |
| 230 |
} |
| 231 |
}); |
| 232 |
live_form.attach(); |
| 233 |
} |
| 234 |
}); |
| 235 |
|