| 1 |
/** |
| 2 |
* |
| 3 |
*/ |
| 4 |
|
| 5 |
var CJTSimpleErrorDialog; |
| 6 |
|
| 7 |
/** |
| 8 |
* |
| 9 |
*/ |
| 10 |
(function($) { |
| 11 |
|
| 12 |
/** |
| 13 |
* put your comment there... |
| 14 |
* |
| 15 |
* @param form |
| 16 |
*/ |
| 17 |
CJTSimpleErrorDialog = function(form) { |
| 18 |
|
| 19 |
/** |
| 20 |
* put your comment there... |
| 21 |
* |
| 22 |
*/ |
| 23 |
var inlineElement; |
| 24 |
|
| 25 |
/** |
| 26 |
* put your comment there... |
| 27 |
* |
| 28 |
* @type String |
| 29 |
*/ |
| 30 |
var onset = ''; |
| 31 |
|
| 32 |
/** |
| 33 |
* |
| 34 |
*/ |
| 35 |
this.errors = []; |
| 36 |
|
| 37 |
/** |
| 38 |
* |
| 39 |
*/ |
| 40 |
this.fields = {}; |
| 41 |
|
| 42 |
/** |
| 43 |
* |
| 44 |
*/ |
| 45 |
this.add = function(name, expression, message) { |
| 46 |
// Set fieldset name. |
| 47 |
name = onset + name; |
| 48 |
this.fields[name] = { |
| 49 |
name: name, |
| 50 |
expression: expression, |
| 51 |
message : message |
| 52 |
} |
| 53 |
return this; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* |
| 58 |
*/ |
| 59 |
this.clear = function() { |
| 60 |
this.errors = []; |
| 61 |
return this; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* |
| 66 |
*/ |
| 67 |
this.fetchFieldInfo = function(field) { |
| 68 |
// Initialize vars! |
| 69 |
var info = {}; |
| 70 |
// Use Object element directly of get it if the name is passed! |
| 71 |
if (typeof field != 'object') { |
| 72 |
if (!this.fields[field]) { |
| 73 |
throw 'Field name doesn\'t exists'; |
| 74 |
} |
| 75 |
// Fetch field by name! |
| 76 |
field = form.prop(field); |
| 77 |
} |
| 78 |
// Make sure its jQuery object! |
| 79 |
field = $(field); |
| 80 |
//Fetch info. |
| 81 |
info.text = form.find('label[for=' + field.prop('id') + ']').text().replace('*', ''); |
| 82 |
return info; |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* |
| 87 |
*/ |
| 88 |
this.hasError = function() { |
| 89 |
return this.errors.length ? true : false; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* |
| 94 |
*/ |
| 95 |
this.onSet = function(name) { |
| 96 |
onset = name; |
| 97 |
return this; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* put your comment there... |
| 102 |
* |
| 103 |
* @param tab_name |
| 104 |
* |
| 105 |
* @returns {Boolean} |
| 106 |
*/ |
| 107 |
this.show = function(tbParams, showName) { |
| 108 |
// Thick box URI. |
| 109 |
var thickBoxParameters = '?TB_inline&_TB-PARAMS_&inlineId=' + inlineElement.prop('id'); |
| 110 |
// Add tbParams if defined! |
| 111 |
if (tbParams != undefined) { |
| 112 |
thickBoxParameters = thickBoxParameters.replace('_TB-PARAMS_', tbParams); |
| 113 |
} |
| 114 |
// Remove all child elements inside the Error inline element. |
| 115 |
inlineElement.find('ul').remove(); |
| 116 |
// Add error list element. |
| 117 |
var errsList = $('<ul class="cjt-error-list"></ul>').appendTo(inlineElement); |
| 118 |
// Build Unordered list of all errors. |
| 119 |
$.each(this.errors, $.proxy( |
| 120 |
function(index, error) { |
| 121 |
var name = ''; |
| 122 |
if (showName && error.bname) { |
| 123 |
name = '<span class="name">' + error.name + '</span>: '; |
| 124 |
} |
| 125 |
var message = '<span class="msg">' + error.message + '</span>'; |
| 126 |
// Note: name mighy be empty! |
| 127 |
errsList.append('<li>' + name + message + '</li>'); |
| 128 |
}, this) |
| 129 |
); |
| 130 |
// Display Thickbox dialog. |
| 131 |
tb_show(CJTSimpleErrorDialogI18N.dialogTitle, thickBoxParameters); |
| 132 |
return this; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* |
| 137 |
*/ |
| 138 |
this.validate = function() { |
| 139 |
// Clear errors! |
| 140 |
this.clear(); |
| 141 |
// Validate fields. |
| 142 |
$.each(this.fields, $.proxy( |
| 143 |
function(key, field) { |
| 144 |
var element = $(form.prop(field.name)); |
| 145 |
var value; |
| 146 |
// Handle various HTML element types! |
| 147 |
switch (element.type) { |
| 148 |
case 'checkbox': |
| 149 |
value = element.prop('checked'); |
| 150 |
break; |
| 151 |
default: |
| 152 |
value = element.val(); |
| 153 |
break; |
| 154 |
} |
| 155 |
// Check the value matched the expression! |
| 156 |
if (!value.match(field.expression)) { |
| 157 |
// use label text as name! |
| 158 |
var name = this.fetchFieldInfo(element).text; |
| 159 |
// Add error! |
| 160 |
this.errors.push({name : name, message: field.message}); |
| 161 |
} |
| 162 |
}, this) |
| 163 |
); |
| 164 |
return this; |
| 165 |
} |
| 166 |
|
| 167 |
// If there is no form id use current time as unique Id. |
| 168 |
if (!(inlineElement = $(form).prop('id'))) { |
| 169 |
inlineElement = (new Date()).getTime(); |
| 170 |
} |
| 171 |
// Prefix Dialog Id! |
| 172 |
inlineElement = 'CJTSimpleErrorDialog__' + inlineElement; |
| 173 |
$('<div id="' + inlineElement + '" class="cjt-error-dialog"><div class="cjt-error-dialog-icon"></div></div>').appendTo('body'); |
| 174 |
inlineElement = $('#' + inlineElement); |
| 175 |
} // End class. |
| 176 |
})(jQuery); |
| 177 |
|