OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openproject/modules/costs/frontend/module/augment/planned-costs-form.ts

102 lines
3.2 KiB

// -- copyright
// OpenProject is a project management system.
// Copyright (C) 2012-2015 the OpenProject Foundation (OPF)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License version 3.
//
// OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
// Copyright (C) 2006-2013 Jean-Philippe Lang
// Copyright (C) 2010-2013 the ChiliProject Team
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// See doc/COPYRIGHT.rdoc for more details.
// ++
export class PlannedCostsFormAugment {
public obj:JQuery;
public objId:string;
public objName:string;
static listen() {
jQuery(document).on('click', '.costs--edit-planned-costs-btn', (evt) => {
const form = jQuery(evt.target).closest('cost-unit-subform');
new PlannedCostsFormAugment(form);
});
}
constructor(public $element:JQuery) {
this.objId = this.$element.attr('obj-id')!;
this.objName = this.$element.attr('obj-name')!;
this.obj = jQuery(this.objId);
this.makeEditable('#' + this.objId, this.objName);
}
private getCurrencyValue(str:string) {
var result = str.match(/^\s*(([0-9]+[.,])+[0-9]+) (.+)\s*/);
return result ? new Array(result[1], result[3]) : new Array(str, "");
}
public makeEditable(id:string, name:string) {
this.edit_and_focus();
}
private edit_and_focus() {
this.edit();
jQuery('#' + this.objId + '_edit').trigger('focus');
jQuery('#' + this.objId + '_edit').trigger('select');
}
private edit() {
this.obj.hide();
let obj_value = this.obj[0].innerHTML;
let id = this.obj[0].id;
let parsed = this.getCurrencyValue(obj_value);
let value = parsed[0];
let currency = parsed[1];
let name = this.objName;
let template = `
<section class="form--section" id="${id}_section">
<div class="form--field">
<div class="form--field-container">
<div id="${id}_cancel" class="form--field-affix -transparent icon icon-close"></div>';
<div id="${id}_editor" class="form--text-field-container">
<input id="${id}_edit" class="form--text-field" name="${name}" value="${value}" class="currency" type="text" />
</div>
<div class="form--field-affix" id="${id}_affix">${currency}</div>
</div>
</div>
</section>
`;
jQuery(template).insertAfter(this.obj);
let that = this;
jQuery('#' + id + '_cancel').on('click', function () {
jQuery('#' + id + '_section').remove();
that.obj.show();
return false;
});
}
}