blob: 9d6b7e8e0ba34ea39298dff36a70cc77f8ff6531 [file] [log] [blame]
Rushabh Mehta3966f1d2012-02-23 12:35:32 +05301// ERPNext - web based ERP (http://erpnext.com)
2// Copyright (C) 2012 Web Notes Technologies Pvt Ltd
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053017// question toolbar
18// contains - voting widget / tag list and user info / timestamp
19// By XXXXXX on YYYYY
20
21KBItemToolbar = function(args, kb) {
22 $.extend(this, args);
23 var me = this;
24 this.make = function() {
25 this.wrapper = $a(this.parent, 'div', '', {});
26 this.line1 = $a(this.wrapper, 'div', '', {color: '#888', fontSize:'11px', margin:'7px 0px'});
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053027 this.make_timestamp();
Rushabh Mehtac7dbe292012-08-07 12:12:55 +053028 this.make_answers();
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053029 if(this.with_tags)
30 this.make_tags();
Rushabh Mehta81c8ec22012-04-18 17:14:33 +053031 this.setup_del();
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053032 }
33
34 this.make_timestamp = function() {
35 this.line1.innerHTML = repl('By %(name)s | %(when)s', {
36 name: wn.utils.full_name(this.det.first_name, this.det.last_name),
37 when: wn.datetime.comment_when(this.det.modified)
Rushabh Mehtae3393be2011-08-30 15:37:46 +053038 });
39
40 // allow system manager to delete questions / answers
41 if(has_common(user_roles, ['Administrator', 'System Manager'])) {
Rushabh Mehta81c8ec22012-04-18 17:14:33 +053042 this.line1.innerHTML += ' | <a style="cursor:pointer;"\
43 class="del-link">delete</a>';
Rushabh Mehtae3393be2011-08-30 15:37:46 +053044 }
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053045 }
46
Rushabh Mehtac7dbe292012-08-07 12:12:55 +053047 this.make_answers = function() {
48 if(this.doctype=='Question') {
49 if(this.det.answers==0) {
50 this.line1.innerHTML += ' | no answers';
51 } else if(this.det.answers==1) {
52 this.line1.innerHTML += ' | 1 answer';
53 } else {
54 this.line1.innerHTML += ' | '+this.det.answers+' answers';
55 }
56 }
57 }
58
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053059 this.make_tags = function() {
Rushabh Mehtab73fa492012-02-24 15:07:39 +053060 this.line1.innerHTML += ' | '
61 this.tags_area = $a(this.line1, 'span', 'kb-tags')
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053062 this.tags = new TagList(this.tags_area,
63 this.det._user_tags && (this.det._user_tags.split(',')),
64 this.doctype, this.det.name, 0, kb.set_tag_filter)
65 }
66
Rushabh Mehta81c8ec22012-04-18 17:14:33 +053067 this.setup_del = function() {
68 $(this.line1).find('.del-link').click(function() {
69 console.log(1);
70 this.innerHTML = 'deleting...';
71 this.disabled = 1;
Anand Doshi37df8ab2012-04-20 11:17:10 +053072 $c_page('utilities', 'questions', 'delete', {
Rushabh Mehta81c8ec22012-04-18 17:14:33 +053073 dt: me.doctype, dn: me.det.name}, function(r,rt) {
74 // reload the list
75 kb.list.run()
76 });
77 });
78 }
79
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053080 this.make();
81}
82
83
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053084// displays an editable text,
85// needs parent, text, disp_class, inp_class
86// dt, dn
87
88EditableText = function(args) {
89 $.extend(this, args);
90 var me = this;
91
Rushabh Mehtaedab76b2012-04-18 16:55:43 +053092 me.$w = $(repl('<div class="ed-text">\
93 <div class="ed-text-display %(disp_class)s"></div>\
Rushabh Mehtaa11bc072012-04-19 17:48:57 +053094 <a class="ed-text-edit" style="cursor: pointer; float: right; margin-top: -16px;">[edit]</a>\
95 <textarea class="ed-text-input %(inp_class)s hide"></textarea>\
Rushabh Mehta6252c132012-08-07 12:53:49 +053096 <div class="help hide"><br>Formatted as <a href="#markdown-reference"\
Rushabh Mehta46162c42012-04-18 16:59:37 +053097 target="_blank">markdown</a></div>\
Rushabh Mehtaedab76b2012-04-18 16:55:43 +053098 <button class="btn btn-small btn-info hide ed-text-save">Save</button>\
99 <a class="ed-text-cancel hide" style="cursor: pointer;">Cancel</a>\
100 </div>', args)).appendTo(me.parent);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530101
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530102 this.set_display = function(txt) {
103 me.$w.find('.ed-text-display').html(wn.markdown(txt));
104 me.text = txt;
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530105 }
106
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530107 this.set_display(me.text);
108
109 if(me.height) me.$w.find('.ed-text-input').css('height', me.height);
110
111 // edit
112 me.$w.find('.ed-text-edit').click(function() {
113 me.$w.find('.ed-text-input').val(me.text);
114 me.show_as_input();
115 })
116
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530117 // save button - save the new text
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530118 me.$w.find('.ed-text-save').click(
119 function() {
120 var v = me.$w.find('.ed-text-input').val();
121 // check if text is written
122 if(!v) {
123 msgprint('Please write something!');
124 return;
125 }
126 var btn = this;
127 $(btn).set_working();
Anand Doshi37df8ab2012-04-20 11:17:10 +0530128 $c_page('utilities', 'question_view', 'update_item', {
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530129 dt: me.dt, dn: me.dn, fn: me.fieldname, text: v
130 },
131 function(r) {
132 $(btn).done_working();
133 if(r.exc) {msgprint(r.exc); return; }
134 me.set_display(v);
135 me.show_as_text();
136 });
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530137 }
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530138 )
139
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530140
141 // cancel button
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530142 me.$w.find('.ed-text-cancel').click(function() {
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530143 me.show_as_text();
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530144 })
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530145
146 this.show_as_text = function() {
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530147 me.$w.find('.ed-text-display, .ed-text-edit').toggle(true);
148 me.$w.find('.ed-text-input, .ed-text-save, .ed-text-cancel, .help').toggle(false);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530149 }
150
151 this.show_as_input = function() {
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530152 me.$w.find('.ed-text-display, .ed-text-edit').toggle(false);
153 me.$w.find('.ed-text-input, .ed-text-save, .ed-text-cancel, .help').toggle(true);
154 }
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530155
Anand Doshi37df8ab2012-04-20 11:17:10 +0530156}