aboutsummaryrefslogtreecommitdiff
path: root/bootstrap/comments/frontend/editcomment.js
blob: 75b1bb396d988ce6996e989a2d13b3473ce81baa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Displays edit form (editcomment.js)
HashOver.prototype.editComment = function (comment)
{
	if (comment['editable'] !== true) {
		return false;
	}

	// Reference to this object
	var hashover = this;

	// Get permalink from comment JSON object
	var permalink = comment.permalink;

	// Get edit link element
	var link = this.elements.get ('edit-link-' + permalink, true);

	// Get file
	var file = this.permalinks.getFile (permalink);

	// Get name and website
	var name = comment.name || '';
	var website = comment.website || '';

	// Get and clean comment body
	var body = comment.body.replace (this.regex.links, '$1');

	// Create edit form element
	var form = this.elements.create ('form', {
		id: 'hashover-edit-' + permalink,
		className: 'hashover-edit-form',
		action: '/formactions',
		method: 'post'
	});

	// Place edit form fields into form
	form.innerHTML = hashover.strings.parseTemplate (hashover.ui['edit-form'], {
		permalink: permalink,
		file: file,
		name: name,
		website: website,
		body: body
	});

	// Prevent input submission
	this.preventSubmit (form);

	// Add edit form to page
	var editForm = this.elements.get ('placeholder-edit-form-' + permalink, true);
	    editForm.appendChild (form);

	// Set status dropdown menu option to comment status
	this.elements.exists ('edit-status-' + permalink, function (status) {
		var statuses = [ 'approved', 'pending', 'deleted' ];

		if (comment.status !== undefined) {
			status.selectedIndex = statuses.indexOf (comment.status);
		}
	});

	// Blank out password field
	setTimeout (function () {
		if (form.password !== undefined) {
			form.password.value = '';
		}
	}, 100);

	// Uncheck subscribe checkbox if user isn't subscribed
	if (comment.subscribed !== true) {
		this.elements.get ('edit-subscribe-' + permalink, true).checked = null;
	}

	// Displays onClick confirmation dialog for comment deletion
	this.elements.get ('edit-delete-' + permalink, true).onclick = function ()
	{
		return confirm (hashover.locale['delete-comment']);
	};

	// Change "Edit" link to "Cancel" link
	this.cancelSwitcher ('edit', link, editForm, permalink);

	// Attach event listeners to "Save Edit" button
	var saveEdit = this.elements.get ('edit-post-' + permalink, true);

	// Get the element of comment being replied to
	var destination = this.elements.get (permalink, true);

	// Attach click event to formatting revealer hyperlink
	this.formattingOnclick ('edit', permalink);

	// Set onclick and onsubmit event handlers
	this.elements.duplicateProperties (saveEdit, [ 'onclick', 'onsubmit' ], function () {
		return hashover.postComment (destination, form, this, hashover.AJAXEdit, 'edit', permalink, link.onclick, false, true);
	});

	return false;
};