Rushabh Mehta | 3966f1d | 2012-02-23 12:35:32 +0530 | [diff] [blame] | 1 | // 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 17 | // question toolbar |
| 18 | // contains - voting widget / tag list and user info / timestamp |
| 19 | // By XXXXXX on YYYYY |
| 20 | |
| 21 | KBItemToolbar = 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 27 | this.make_timestamp(); |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 28 | this.make_answers(); |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 29 | if(this.with_tags) |
| 30 | this.make_tags(); |
Rushabh Mehta | 81c8ec2 | 2012-04-18 17:14:33 +0530 | [diff] [blame] | 31 | this.setup_del(); |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | this.make_timestamp = function() { |
| 35 | this.line1.innerHTML = repl('By %(name)s | %(when)s', { |
Rushabh Mehta | 647e0d1 | 2012-12-18 14:47:54 +0530 | [diff] [blame] | 36 | name: wn.user_info(this.det.owner).fullname, |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 37 | when: wn.datetime.comment_when(this.det.modified) |
Rushabh Mehta | e3393be | 2011-08-30 15:37:46 +0530 | [diff] [blame] | 38 | }); |
| 39 | |
| 40 | // allow system manager to delete questions / answers |
| 41 | if(has_common(user_roles, ['Administrator', 'System Manager'])) { |
Rushabh Mehta | 81c8ec2 | 2012-04-18 17:14:33 +0530 | [diff] [blame] | 42 | this.line1.innerHTML += ' | <a style="cursor:pointer;"\ |
| 43 | class="del-link">delete</a>'; |
Rushabh Mehta | e3393be | 2011-08-30 15:37:46 +0530 | [diff] [blame] | 44 | } |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 45 | } |
| 46 | |
Rushabh Mehta | c7dbe29 | 2012-08-07 12:12:55 +0530 | [diff] [blame] | 47 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 59 | this.make_tags = function() { |
Rushabh Mehta | b73fa49 | 2012-02-24 15:07:39 +0530 | [diff] [blame] | 60 | this.line1.innerHTML += ' | ' |
| 61 | this.tags_area = $a(this.line1, 'span', 'kb-tags') |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 62 | 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 Mehta | 81c8ec2 | 2012-04-18 17:14:33 +0530 | [diff] [blame] | 67 | this.setup_del = function() { |
| 68 | $(this.line1).find('.del-link').click(function() { |
Rushabh Mehta | 81c8ec2 | 2012-04-18 17:14:33 +0530 | [diff] [blame] | 69 | this.innerHTML = 'deleting...'; |
| 70 | this.disabled = 1; |
Anand Doshi | 37df8ab | 2012-04-20 11:17:10 +0530 | [diff] [blame] | 71 | $c_page('utilities', 'questions', 'delete', { |
Rushabh Mehta | 81c8ec2 | 2012-04-18 17:14:33 +0530 | [diff] [blame] | 72 | dt: me.doctype, dn: me.det.name}, function(r,rt) { |
| 73 | // reload the list |
| 74 | kb.list.run() |
| 75 | }); |
| 76 | }); |
| 77 | } |
| 78 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 79 | this.make(); |
| 80 | } |
| 81 | |
| 82 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 83 | // displays an editable text, |
| 84 | // needs parent, text, disp_class, inp_class |
| 85 | // dt, dn |
| 86 | |
| 87 | EditableText = function(args) { |
| 88 | $.extend(this, args); |
| 89 | var me = this; |
| 90 | |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 91 | me.$w = $(repl('<div class="ed-text">\ |
| 92 | <div class="ed-text-display %(disp_class)s"></div>\ |
Rushabh Mehta | a11bc07 | 2012-04-19 17:48:57 +0530 | [diff] [blame] | 93 | <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 Mehta | 6252c13 | 2012-08-07 12:53:49 +0530 | [diff] [blame] | 95 | <div class="help hide"><br>Formatted as <a href="#markdown-reference"\ |
Rushabh Mehta | 46162c4 | 2012-04-18 16:59:37 +0530 | [diff] [blame] | 96 | target="_blank">markdown</a></div>\ |
Rushabh Mehta | ee472b0 | 2012-12-18 11:47:13 +0530 | [diff] [blame] | 97 | <button class="btn btn-info hide ed-text-save">Save</button>\ |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 98 | <a class="ed-text-cancel hide" style="cursor: pointer;">Cancel</a>\ |
| 99 | </div>', args)).appendTo(me.parent); |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 100 | |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 101 | this.set_display = function(txt) { |
Anand Doshi | fdfa605 | 2012-11-19 18:46:23 +0530 | [diff] [blame] | 102 | 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 Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 105 | me.text = txt; |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 106 | } |
| 107 | |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 108 | this.set_display(me.text); |
| 109 | |
| 110 | if(me.height) me.$w.find('.ed-text-input').css('height', me.height); |
Rushabh Mehta | 0ad9954 | 2013-02-22 10:34:22 +0530 | [diff] [blame] | 111 | if(me.width) me.$w.find('.ed-text-input').css('width', me.width); |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 119 | // save button - save the new text |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 120 | 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 Doshi | 37df8ab | 2012-04-20 11:17:10 +0530 | [diff] [blame] | 130 | $c_page('utilities', 'question_view', 'update_item', { |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 131 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 139 | } |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 140 | ) |
| 141 | |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 142 | |
| 143 | // cancel button |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 144 | me.$w.find('.ed-text-cancel').click(function() { |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 145 | me.show_as_text(); |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 146 | }) |
Pratik Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 147 | |
| 148 | this.show_as_text = function() { |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 149 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | this.show_as_input = function() { |
Rushabh Mehta | edab76b | 2012-04-18 16:55:43 +0530 | [diff] [blame] | 154 | 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 Vyas | c1e6e4c | 2011-06-08 14:37:15 +0530 | [diff] [blame] | 157 | |
Anand Doshi | 37df8ab | 2012-04-20 11:17:10 +0530 | [diff] [blame] | 158 | } |