// LINKS var LinksElement = Class.create(); LinksElement.prototype = { initialize: function(opt) { for (i in opt) { this[i] = opt[i]; } this.container = $('#'+this.linksContainerId); this.elAdd = $('#'+this.addLinkId); this.form = $('#'+this.id_form); this.maxIndex = $('input[name^='+this.elementName+']:hidden', this.form).size(); // init methods obj = this; this.elAdd.get(0).onclick = function() { obj.addOnClick(); return false; } $('a.link_edit', this.form).each(function(){ this.onclick = function() { obj.edit($(this).attr('index')); return false; } }); $('a.link_del', this.form).each(function(){ this.onclick = function() { obj.del($(this).attr('index')); return false; } }); }, addOnClick: function() { var value = 'http://'; var res; var valid; do { value = prompt("Enter link address:\nexample: http:://google.com", value); if (value == null || value == '') { return; } valid = this.validUrl(value); if (!valid) { alert('Link Address is in wrong format') } } while(!valid); this.add(value); }, getElementByIndex: function(index) { return $('input[name="'+this.elementName+'['+index+']"]:hidden', this.form); }, add: function(url) { var cnt = ++this.maxIndex; this.form.prepend(''); obj = this; var link = $(''+url.substr(0, this.linkTextLength)+''); var link_edit = $('edit'); link_edit.click(function() { obj.edit(cnt); return false; }); var link_del = $('delete'); link_del.click(function() { obj.del(cnt); return false; }); var options = $(''); options.append(' (').append(link_edit).append(' | ').append(link_del).append(')'); var linkItemContainer = $('').append(link).append(options); this.container.append(linkItemContainer); }, validUrl: function(url) { var regex = /^((http|https|ftp):\/\/)?(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?\/?/i return regex.test(url); }, edit: function(index) { var el = this.getElementByIndex(index); var value = el.val(); var res; var valid; do { value = prompt("Enter link address:\nexample: http:://google.com", value); if (value == null || value == '') { return; } valid = this.validUrl(value); if (!valid) { alert('Link Address is in wrong format') } } while(!valid); this.update(index, value); }, update: function(index, url) { var el = this.getElementByIndex(index); el.val(url); $('#link_'+index).attr('href', url).attr('title', url).text(url.substr(0, this.linkTextLength)); }, del: function(index) { if (confirm('Do you really want to delete this '+this.itemName+'?')) { $('#link_container_'+index).remove(); this.getElementByIndex(index).remove(); } } } function initMemberLingsElement(id_form, linkTextLength) { // Recommended links var optLinks = { id_form: id_form, itemName: 'Recommended Link', elementName: 'link', linksContainerId: 'links_container', addLinkId: 'link_add', linkTextLength: linkTextLength }; links = new LinksElement(optLinks); }