Files

122 lines
4.6 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
Friends.CloneEmail = new function(){
this.createFromTag = function(element, params){
new Friends.CloneEmail.Constructor(element, params);
}
}
Friends.CloneEmail.Constructor = function(element, params){
this.element = element;
this.invitesCount = params.invitesCount;
this.num = params.num || null;
this.validEmailField = params.validEmailField || null;
this.button = y5.cssQuery('input[type=button]', this.element)[0];
if (typeof(this.button) == y5.Vars.UNDEF){
return;
}
this.input = y5.cssQuery('input[type=text]', this.element)[0];
var notes = y5.Dom.getDescendants(this.input.form, 'b', 'b-note-attention');
this.note = notes[notes.length - 1]; // элемент с сообщением о некорректности заполнения формы
this.init();
}
Friends.CloneEmail.Constructor.prototype = new function(){
this.init = function(){
if(this.button.value == '+'){
this.doAction = this.clone;
}else{
this.doAction = this.remove;
}
var _this = this;
function click(){
_this.doAction(); // clone or remove, deoends on button
}
y5.Events.create('click', click, this.button, true);
var num = parseInt(this.input.name.substr(this.input.name.indexOf('_') + 1));
this.num = this.num ? this.num + 1 : num ;
this.input.name = this.input.name.replace(new RegExp('_' + num, ''), '_' + this.num);
this.input.focus();
// при попытке отправить форму, проверяем ее на заполненность корректными данными,
// если форма не заполнена, то ее отправку отменяем
if (this.num == 1){
new y5.AEventListener('submit',
function(e){
if (!_this.isFormFilled()){
_this.preventSubmit(e);
}
},
this.input.form, true
);
}
};
this.clone = function(){
var newElement = this.element.cloneNode(true);
newElement.innerHTML = newElement.innerHTML; // for IE it doesn't clear events after clone.
var input = y5.cssQuery('input[type=text]', newElement)[0];
input.value = ''; // в IE7 при клонировании значение input'а сохраняется
var button = y5.cssQuery('input[type=button]', newElement)[0];
this.button.parentNode.removeChild(this.button);
this.button = button;
if (this.isLastInvite()) {
this.button.parentNode.removeChild(this.button);
}
var _this = this;
newElement.onclick = function(){
return {
type: 'Email',
invitesCount: _this.invitesCount,
num: _this.num,
validEmailField: _this.validEmailField}
};
y5.Classes.add(newElement, 'Friends-c-FieldValidator');
y5.Classes.add(newElement, 'Friends-c-CloneEmail');
this.element.parentNode.appendChild(newElement);
y5.Components.init(this.element.parentNode);
};
this.isLastInvite = function(){
var emailFields = y5.Dom.getDescendants(this.element.parentNode, 'div', 'address');
if (emailFields.length + 1 == parseInt(this.invitesCount)) {
return true;
}
return false;
}
this.remove = function(){
y5.Dom.removeNode(this.element);
};
this.isFormFilled = function() {
// скрываем сообщение о некорректности заполнения формы
this.note.style.visibility = 'hidden';
var emailFields = y5.Dom.getDescendants(this.input.form, 'input', 'FieldValidator-field');
var regExp = /[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-z]{2,7}/i;
// проверяем форму на наличие хотя бы одного введенного валидного email'а
for (var i = 0, l = emailFields.length; i < l; i++) {
if (regExp.test(emailFields[i].value)) {
return true;
}
}
return false;
};
this.preventSubmit = function(e, msg) {
if (msg) {
this.note.innerHTML = msg;
}
// показываем сообщение о некорректности заполнения формы
this.note.style.visibility = 'visible';
// отменяем отправку формы
e.preventDefault();
};
}
y5.require(['cssQuery', 'Events', 'Classes', 'Dom'], function(){y5.loaded('{Friends}.CloneEmail')});