| 1 |
// $Id: relatedlinks.js,v 1.3 2008/02/17 08:02:48 karthik Exp $
|
| 2 |
|
| 3 |
$(document).ready(function() {
|
| 4 |
var ta = $('#edit-relatedlinks-fieldset-relatedlinks');
|
| 5 |
$(ta).parents('div.form-item').hide();
|
| 6 |
var top = $(ta).parents('fieldset');
|
| 7 |
|
| 8 |
// Parse textarea for links and store them in a global array.
|
| 9 |
l = $(ta).val().split("\n");
|
| 10 |
r = new RegExp('\w*([^\ \w]+)\ *(.*)$', 'i');
|
| 11 |
links = new Array();
|
| 12 |
for (var i = 0; i < l.length; i++) {
|
| 13 |
m = l[i].match(r);
|
| 14 |
if (m) {
|
| 15 |
link_set(m[1], m[2], i);
|
| 16 |
}
|
| 17 |
}
|
| 18 |
|
| 19 |
// -1 signifies that no link is being edited at the moment.
|
| 20 |
current = -1;
|
| 21 |
|
| 22 |
// Insert URL and title textfields.
|
| 23 |
var fields = $('<div class="relatedlinks clear-block"><div id="relatedlinks-preview"></div><div class="relatedlinks-url"><div class="form-item"><label for="field-relatedlinks-url">URL:</label><input id="field-relatedlinks-url" class="form-text" type="text" value="" size="60" name="relatedlinks-url" maxlength="128" /></div></div><div class="relatedlinks-title"><div class="form-item"><label for="field-relatedlinks-title">Title:</label><input id="field-relatedlinks-title" class="form-text" type="text" value="" size="60" name="relatedlinks-title" maxlength="128" /></div></div></div>')
|
| 24 |
.appendTo(top);
|
| 25 |
var preview = $('#relatedlinks-preview', fields);
|
| 26 |
|
| 27 |
links_preview();
|
| 28 |
var l = $('<a id="relatedlinks-add" href="#">Add / Update</a>')
|
| 29 |
.appendTo(fields)
|
| 30 |
.click(function() {
|
| 31 |
url = $('#field-relatedlinks-url', fields).val();
|
| 32 |
if (url.length) {
|
| 33 |
if (current == -1) {
|
| 34 |
link_set(url, $('#field-relatedlinks-title', fields).val(), links.length);
|
| 35 |
}
|
| 36 |
else {
|
| 37 |
link_set(url, $('#field-relatedlinks-title', fields).val(), current);
|
| 38 |
}
|
| 39 |
$('#field-relatedlinks-title', fields).val('');
|
| 40 |
$('#field-relatedlinks-url', fields).val('');
|
| 41 |
}
|
| 42 |
links_preview();
|
| 43 |
return false;
|
| 44 |
}
|
| 45 |
);
|
| 46 |
|
| 47 |
var l = $('<a id="relatedlinks-clear" href="#">Clear</a>')
|
| 48 |
.appendTo(fields)
|
| 49 |
.click(function() {
|
| 50 |
current = -1;
|
| 51 |
$('#field-relatedlinks-url', fields).val('');
|
| 52 |
$('#field-relatedlinks-title', fields).val('');
|
| 53 |
return false;
|
| 54 |
}
|
| 55 |
);
|
| 56 |
|
| 57 |
|
| 58 |
function links_preview() {
|
| 59 |
// Remove the preview list. This function is called after every event and
|
| 60 |
// the list is just recreated every time based on the textarea input.
|
| 61 |
$('ol', preview).remove();
|
| 62 |
set = $('<ol></ol>').appendTo(preview);
|
| 63 |
|
| 64 |
l = '';
|
| 65 |
for (var i = 0; i < links.length; i++) {
|
| 66 |
// Create textarea input.
|
| 67 |
l += links[i].url + ' ' + links[i].title + "\n";
|
| 68 |
|
| 69 |
// Trim and check if title is available; else use the URL as the title.
|
| 70 |
links[i].title = $.trim(links[i].title);
|
| 71 |
title = links[i].title.length ? links[i].title : links[i].url;
|
| 72 |
|
| 73 |
// Add edit button.
|
| 74 |
actions = '<a class="relatedlinks-action relatedlinks-edit" title="Edit this link.">Edit</a>';
|
| 75 |
// Add delete button.
|
| 76 |
actions += '<a class="relatedlinks-action relatedlinks-delete" title="Delete this link.">Delete</a>';
|
| 77 |
|
| 78 |
// If available, add the up button.
|
| 79 |
if (i > 0) {
|
| 80 |
actions += '<a class="relatedlinks-action relatedlinks-up" title="Move this link one row up.">Up</a>';
|
| 81 |
}
|
| 82 |
// If available, add the down button.
|
| 83 |
if (i < links.length - 1) {
|
| 84 |
actions += '<a class="relatedlinks-action relatedlinks-down" title="Move this link one row down.">Down</a>';
|
| 85 |
}
|
| 86 |
|
| 87 |
// Insert action buttons.
|
| 88 |
$('<li><a class="relatedlinks-link" href="' + links[i].url + '">' + title + '</a>' + actions + '</li>')
|
| 89 |
.appendTo(set);
|
| 90 |
}
|
| 91 |
|
| 92 |
// Handle edit button clicks: populate URL and title fields with clicked
|
| 93 |
// link value.
|
| 94 |
$('.relatedlinks-edit', set).each(function(k) {
|
| 95 |
$(this).click(function() {
|
| 96 |
// Track the currently edited link ID.
|
| 97 |
current = k;
|
| 98 |
$('#field-relatedlinks-url', fields).val(links[current].url);
|
| 99 |
$('#field-relatedlinks-title', fields).val(links[current].title);
|
| 100 |
return false;
|
| 101 |
});
|
| 102 |
});
|
| 103 |
|
| 104 |
// Handle delete button clicks. Remove clicked link from the relatedlinks
|
| 105 |
// textfield.
|
| 106 |
$('.relatedlinks-delete', set).each(function(k) {
|
| 107 |
$(this).click(function() {
|
| 108 |
links_delete(k);
|
| 109 |
links_preview();
|
| 110 |
return false;
|
| 111 |
});
|
| 112 |
});
|
| 113 |
|
| 114 |
// Handle up clicks. Move clicked link up one row.
|
| 115 |
$('.relatedlinks-up', set).each(function(k) {
|
| 116 |
$(this).click(function() {
|
| 117 |
links_swap(k, k + 1);
|
| 118 |
links_preview();
|
| 119 |
return false;
|
| 120 |
});
|
| 121 |
});
|
| 122 |
|
| 123 |
// Handle down clicks. Move clicked link down one row.
|
| 124 |
$('.relatedlinks-down', set).each(function(k) {
|
| 125 |
$(this).click(function() {
|
| 126 |
links_swap(k, k + 1);
|
| 127 |
links_preview();
|
| 128 |
return false;
|
| 129 |
});
|
| 130 |
});
|
| 131 |
|
| 132 |
// Update relatedlinks textarea.
|
| 133 |
$(ta).val(l);
|
| 134 |
}
|
| 135 |
|
| 136 |
// Insert/update link input into global links array.
|
| 137 |
function link_set(link, text, i) {
|
| 138 |
links[i] = {url: $.trim(link), title: $.trim(text)};
|
| 139 |
}
|
| 140 |
|
| 141 |
// Delete link from global links array.
|
| 142 |
function links_delete(k) {
|
| 143 |
for (var i = k; i < links.length - 1; i++) {
|
| 144 |
links[i] = links[i + 1];
|
| 145 |
}
|
| 146 |
links.pop();
|
| 147 |
current = -1;
|
| 148 |
}
|
| 149 |
|
| 150 |
// Swap link orders.
|
| 151 |
function links_swap(j, k) {
|
| 152 |
if (k >= 0 && k < links.length) {
|
| 153 |
var t = links[j];
|
| 154 |
links[j] = links[k];
|
| 155 |
links[k] = t;
|
| 156 |
}
|
| 157 |
}
|
| 158 |
});
|