blob: ae80acf4f8a8fde8397fa0c7d90a2f615751a201 [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) {
Anand Doshifdfa6052012-11-19 18:46:23 +0530103 var display_wrapper = me.$w.find('.ed-text-display');
104 display_wrapper.html(wn.markdown(txt));
105 display_wrapper.find("a").attr("target", "blank");
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530106 me.text = txt;
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530107 }
108
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530109 this.set_display(me.text);
110
111 if(me.height) me.$w.find('.ed-text-input').css('height', me.height);
112
113 // edit
114 me.$w.find('.ed-text-edit').click(function() {
115 me.$w.find('.ed-text-input').val(me.text);
116 me.show_as_input();
117 })
118
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530119 // save button - save the new text
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530120 me.$w.find('.ed-text-save').click(
121 function() {
122 var v = me.$w.find('.ed-text-input').val();
123 // check if text is written
124 if(!v) {
125 msgprint('Please write something!');
126 return;
127 }
128 var btn = this;
129 $(btn).set_working();
Anand Doshi37df8ab2012-04-20 11:17:10 +0530130 $c_page('utilities', 'question_view', 'update_item', {
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530131 dt: me.dt, dn: me.dn, fn: me.fieldname, text: v
132 },
133 function(r) {
134 $(btn).done_working();
135 if(r.exc) {msgprint(r.exc); return; }
136 me.set_display(v);
137 me.show_as_text();
138 });
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530139 }
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530140 )
141
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530142
143 // cancel button
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530144 me.$w.find('.ed-text-cancel').click(function() {
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530145 me.show_as_text();
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530146 })
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530147
148 this.show_as_text = function() {
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530149 me.$w.find('.ed-text-display, .ed-text-edit').toggle(true);
150 me.$w.find('.ed-text-input, .ed-text-save, .ed-text-cancel, .help').toggle(false);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530151 }
152
153 this.show_as_input = function() {
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530154 me.$w.find('.ed-text-display, .ed-text-edit').toggle(false);
155 me.$w.find('.ed-text-input, .ed-text-save, .ed-text-cancel, .help').toggle(true);
156 }
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530157
Anand Doshi37df8ab2012-04-20 11:17:10 +0530158}