add cms, add todo
[ssproject1617.git] / testcms-final-anon / system / admin / theme / assets / js / comments.js
1 (function(base_url) {
2
3 var p = new Popup();
4
5 var publish = function() {
6 var a = this, li = a.getParent();
7 var url = base_url + 'comments/status';
8 var id = a.getParent('li[data-id]').get('data-id');
9
10 new Request.JSON({
11 'url': url,
12 'method': 'post',
13 'data': {'id': id, 'status': 'published'},
14 'onRequest': function() {
15 li.setStyle('opacity', 0.5);
16 a.removeEvent('click', publish);
17 },
18 'onSuccess': function() {
19 li.dispose();
20 $$('[data-status=' + id + ']').set('html', 'Published');
21 }
22 }).send();
23
24 return false;
25 };
26
27 var edit = function() {
28 var a = this;
29 var url = base_url + 'comments/update';
30 var id = a.getParent('li[data-id]').get('data-id');
31 var text = $$('[data-text=' + id + ']').get('html');
32 var status = $$('[data-status=' + id + ']').get('html');
33
34 var html = '<fieldset><legend>Edit Comment</legend><em>Update the comment text here.</em>';
35 html +='<p><label>Text</label><textarea name="comment_text">' + text + '</textarea></p>';
36 html +='<p><label>Status</label><select name="comment_status">';
37
38 html += '<option value="published"' + (status == 'published' ? ' selected' : '') + '>Published</option>';
39 html += '<option value="pending"' + (status == 'pending' ? ' selected' : '') + '>Pending</option>';
40 html += '<option value="spam"' + (status == 'spam' ? ' selected' : '') + '>Spam</option>';
41
42 html += '</select></p>';
43 html += '</fieldset>';
44 html +='<p class="buttons"><button name="update" type="button">Update</button> <a href="#close">Close</a></p>';
45
46 var content = new Element('div', {
47 'class': 'popup_wrapper',
48 'html': html
49 });
50
51 p.open({
52 'content': content
53 });
54
55 // bind functions
56 $$('button[name=update]').addEvent('click', function() {
57 update(id);
58 });
59 $$('a[href$=#close]').addEvent('click', function() {
60 p.close();
61 return false;
62 });
63
64 return false;
65 };
66
67 var update = function(id) {
68 var url = base_url + 'comments/update',
69 comment_text_input = $$('textarea[name=comment_text]').pop(),
70 comment_status_input = $$('select[name=comment_status]').pop();
71
72 // get values
73 var text = comment_text_input.get('value'),
74 status = comment_status_input.get('value');
75
76 // get elements
77 var comment_text_output = $$('[data-text=' + id + ']').pop(),
78 comment_status_output = $$('[data-status=' + id + ']').pop(),
79 li = $$('li[data-id=' + id + ']').pop();
80
81 new Request.JSON({
82 'url': url,
83 'method': 'post',
84 'data': {'id': id, 'text': text, 'status': status},
85 'onRequest': function() {
86 li.setStyle('opacity', 0.5);
87 },
88 'onSuccess': function() {
89 li.setStyle('opacity', 1);
90 comment_text_output.set('html', text);
91 comment_status_output.set('html', status);
92
93 // get publish button if it exists
94 var btn = li.getElement('a[href$=#publish]');
95
96 // hide publish button
97 if(btn) {
98 if(status == 'published') {
99 btn.dispose();
100 }
101 } else {
102 if(status == 'pending') {
103 var ul = li.getElement('ul');
104 btn = new Element('li');
105 btn.grab(new Element('a', {
106 'html': 'Publish',
107 'href': '#publish',
108 'events': {
109 'click': publish
110 }
111 }));
112 ul.grab(btn, 'top');
113 }
114 }
115
116 p.close();
117 }
118 }).send();
119 };
120
121 var remove = function() {
122 var a = this, li = a.getParent('li[data-id]');
123 var url = base_url + 'comments/remove';
124 var id = li.get('data-id');
125
126 new Request.JSON({
127 'url': url,
128 'method': 'post',
129 'data': {'id': id},
130 'onRequest': function() {
131 li.setStyle('opacity', 0.5);
132 },
133 'onSuccess': function() {
134 li.dispose();
135 }
136 }).send();
137
138 return false;
139 };
140
141 // bindings
142 $$('#comments a[href$=publish]').addEvent('click', publish);
143 $$('#comments a[href$=edit]').addEvent('click', edit);
144 $$('#comments a[href$=delete]').addEvent('click', remove);
145
146 }('../../'));