blob: 1e3fcba15a37ea1c3789991c27da8a8eb4a159f4 [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', {
Rushabh Mehta647e0d12012-12-18 14:47:54 +053036 name: wn.user_info(this.det.owner).fullname,
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053037 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() {
Rushabh Mehta81c8ec22012-04-18 17:14:33 +053069 this.innerHTML = 'deleting...';
70 this.disabled = 1;
Anand Doshi37df8ab2012-04-20 11:17:10 +053071 $c_page('utilities', 'questions', 'delete', {
Rushabh Mehta81c8ec22012-04-18 17:14:33 +053072 dt: me.doctype, dn: me.det.name}, function(r,rt) {
73 // reload the list
74 kb.list.run()
75 });
76 });
77 }
78
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053079 this.make();
80}
81
82
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +053083// displays an editable text,
84// needs parent, text, disp_class, inp_class
85// dt, dn
86
87EditableText = function(args) {
88 $.extend(this, args);
89 var me = this;
90
Rushabh Mehtaedab76b2012-04-18 16:55:43 +053091 me.$w = $(repl('<div class="ed-text">\
92 <div class="ed-text-display %(disp_class)s"></div>\
Rushabh Mehtaa11bc072012-04-19 17:48:57 +053093 <a class="ed-text-edit" style="cursor: pointer; float: right; margin-top: -16px;">[edit]</a>\
94 <textarea class="ed-text-input %(inp_class)s hide"></textarea>\
Rushabh Mehta6252c132012-08-07 12:53:49 +053095 <div class="help hide"><br>Formatted as <a href="#markdown-reference"\
Rushabh Mehta46162c42012-04-18 16:59:37 +053096 target="_blank">markdown</a></div>\
Rushabh Mehtaee472b02012-12-18 11:47:13 +053097 <button class="btn btn-info hide ed-text-save">Save</button>\
Rushabh Mehtaedab76b2012-04-18 16:55:43 +053098 <a class="ed-text-cancel hide" style="cursor: pointer;">Cancel</a>\
99 </div>', args)).appendTo(me.parent);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530100
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530101 this.set_display = function(txt) {
Anand Doshifdfa6052012-11-19 18:46:23 +0530102 var display_wrapper = me.$w.find('.ed-text-display');
103 display_wrapper.html(wn.markdown(txt));
104 display_wrapper.find("a").attr("target", "blank");
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530105 me.text = txt;
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530106 }
107
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530108 this.set_display(me.text);
109
110 if(me.height) me.$w.find('.ed-text-input').css('height', me.height);
111
112 // edit
113 me.$w.find('.ed-text-edit').click(function() {
114 me.$w.find('.ed-text-input').val(me.text);
115 me.show_as_input();
116 })
117
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530118 // save button - save the new text
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530119 me.$w.find('.ed-text-save').click(
120 function() {
121 var v = me.$w.find('.ed-text-input').val();
122 // check if text is written
123 if(!v) {
124 msgprint('Please write something!');
125 return;
126 }
127 var btn = this;
128 $(btn).set_working();
Anand Doshi37df8ab2012-04-20 11:17:10 +0530129 $c_page('utilities', 'question_view', 'update_item', {
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530130 dt: me.dt, dn: me.dn, fn: me.fieldname, text: v
131 },
132 function(r) {
133 $(btn).done_working();
134 if(r.exc) {msgprint(r.exc); return; }
135 me.set_display(v);
136 me.show_as_text();
137 });
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530138 }
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530139 )
140
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530141
142 // cancel button
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530143 me.$w.find('.ed-text-cancel').click(function() {
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530144 me.show_as_text();
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530145 })
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530146
147 this.show_as_text = function() {
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530148 me.$w.find('.ed-text-display, .ed-text-edit').toggle(true);
149 me.$w.find('.ed-text-input, .ed-text-save, .ed-text-cancel, .help').toggle(false);
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530150 }
151
152 this.show_as_input = function() {
Rushabh Mehtaedab76b2012-04-18 16:55:43 +0530153 me.$w.find('.ed-text-display, .ed-text-edit').toggle(false);
154 me.$w.find('.ed-text-input, .ed-text-save, .ed-text-cancel, .help').toggle(true);
155 }
Pratik Vyasc1e6e4c2011-06-08 14:37:15 +0530156
Anand Doshi37df8ab2012-04-20 11:17:10 +0530157}