<commit_before>import {bindable, inject, customElement, LogManager} from 'aurelia-framework';
import {Catalogues} from '../../../services/catalogues';
import {Stamps} from '../../../services/stamps';
import {CatalogueNumbers} from '../../../services/catalogueNumbers';
import {Preferences} from '../../../services/preferences';
import {Condition} from '../../../util/common-models';
import {I18N} from 'aurelia-i18n';

import $ from 'jquery';
import _ from 'lodash';
import bootbox from 'bootbox';

const logger = LogManager.getLogger('cn-references');


@customElement('catalogue-number-references')
@bindable('model')
@inject(Element, I18N, Catalogues, CatalogueNumbers, Stamps, Preferences)
export class CatalogueNumberReferences {

    modelCopy;
    updateTemplate;
    editSlot;
    catalogues = [];

    defaultCondition = -1;
    defaultCatalogue = null;

    conditions = Condition.symbols();
    number;

    constructor(element, i18next, catalogueService, catalogueNumberService, stampService, preferenceService) {
        this.element = $(element);
        this.i18next = i18next;
        this.catalogueService = catalogueService;
        this.catalogueNumberService = catalogueNumberService;
        this.stampService = stampService;
        this.preferenceService = preferenceService;
    }

    bind() {
        let self = this;
        // Using a promise here, but not bind (view-models) do not use promises to block
        Promise.all([
            self.catalogueService.find(self.catalogueService.getDefaultSearchOptions()).then(results => {
                self.catalogues = results.models;
            }),
            self.preferenceService.find(self.preferenceService.getDefaultSearchOptions()).then(results => { //eslint-disable-line no-unused-vars
                let cond = self.preferenceService.getByNameAndCategory('condition', 'stamps');
                if( cond ) {
                    self.defaultCondition = +cond.value;
                }
                let cat = self.preferenceService.getByNameAndCategory('catalogueRefSecondary', 'stamps');
                if( cat ) {
                    self.defaultCatalogue = +cat.value;
                }
            })
        ]);
    }

    modelChanged(newModel) {
        if (newModel) {
            let self = this;
            this.modelCopy = _.clone(newModel, true);
            this.modelCopy.catalogueNumbers.forEach(catNum => {
                catNum.currencyCode = self.determineCurrencyCode(catNum.catalogueRef);
            });
        } else {
            this.modelCopy = {};
        }
    }

    determineCurrencyCode(catalogueRef) {
        let code = 'USD';
        let cat = this.catalogueService.getById(catalogueRef);
        if (cat) {
            code = cat.code;
        }
        return code;
    }

    edit(num, index) { //eslint-disable-line no-unused-vars
        num.editing = true;
    }

    cancel(num) {
        let inlineRow = $(this.element.find('.editing-row'));
        if (num.id === 0) {
            inlineRow.remove();
            let index = _.findIndex(this.modelCopy.catalogueNumbers, {id: 0});
            if (index >= 0) {
                this.modelCopy.catalogueNumbers.splice(1, index);
            }
        }
        num.editing = false;
    }

    save(num) { //eslint-disable-line no-unused-vars
        let self = this;
        self.stampService.save(this.modelCopy).then( stamp => {
            self.modelChanged(stamp);
        });
    }

    add() {<commit_msg>Ensure edit catalogue number is closed if successful<commit_after>import {bindable, inject, customElement, LogManager} from 'aurelia-framework';
import {Catalogues} from '../../../services/catalogues';
import {Stamps} from '../../../services/stamps';
import {CatalogueNumbers} from '../../../services/catalogueNumbers';
import {Preferences} from '../../../services/preferences';
import {Condition} from '../../../util/common-models';
import {I18N} from 'aurelia-i18n';

import $ from 'jquery';
import _ from 'lodash';
import bootbox from 'bootbox';

const logger = LogManager.getLogger('cn-references');


@customElement('catalogue-number-references')
@bindable('model')
@inject(Element, I18N, Catalogues, CatalogueNumbers, Stamps, Preferences)
export class CatalogueNumberReferences {

    modelCopy;
    updateTemplate;
    editSlot;
    catalogues = [];

    defaultCondition = -1;
    defaultCatalogue = null;

    conditions = Condition.symbols();
    number;

    constructor(element, i18next, catalogueService, catalogueNumberService, stampService, preferenceService) {
        this.element = $(element);
        this.i18next = i18next;
        this.catalogueService = catalogueService;
        this.catalogueNumberService = catalogueNumberService;
        this.stampService = stampService;
        this.preferenceService = preferenceService;
    }

    bind() {
        let self = this;
        // Using a promise here, but not bind (view-models) do not use promises to block
        Promise.all([
            self.catalogueService.find(self.catalogueService.getDefaultSearchOptions()).then(results => {
                self.catalogues = results.models;
            }),
            self.preferenceService.find(self.preferenceService.getDefaultSearchOptions()).then(results => { //eslint-disable-line no-unused-vars
                let cond = self.preferenceService.getByNameAndCategory('condition', 'stamps');
                if( cond ) {
                    self.defaultCondition = +cond.value;
                }
                let cat = self.preferenceService.getByNameAndCategory('catalogueRefSecondary', 'stamps');
                if( cat ) {
                    self.defaultCatalogue = +cat.value;
                }
            })
        ]);
    }

    modelChanged(newModel) {
        if (newModel) {
            let self = this;
            this.modelCopy = _.clone(newModel, true);
            this.modelCopy.catalogueNumbers.forEach(catNum => {
                catNum.currencyCode = self.determineCurrencyCode(catNum.catalogueRef);
            });
        } else {
            this.modelCopy = {};
        }
    }

    determineCurrencyCode(catalogueRef) {
        let code = 'USD';
        let cat = this.catalogueService.getById(catalogueRef);
        if (cat) {
            code = cat.code;
        }
        return code;
    }

    edit(num, index) { //eslint-disable-line no-unused-vars
        num.editing = true;
    }

    cancel(num) {
        let inlineRow = $(this.element.find('.editing-row'));
        if (num.id === 0) {
            inlineRow.remove();
            let index = _.findIndex(this.modelCopy.catalogueNumbers, {id: 0});
            if (index >= 0) {
                this.modelCopy.catalogueNumbers.splice(1, index);
            }
        }
        num.editing = false;
    }

    save(num) { //eslint-disable-line no-unused-vars
        this.stampService.save(this.modelCopy).then( stamp => {
            this.modelChanged(stamp);
            num.editing = false;
        });
    }

    add() {
{'fraction_non_alphanumeric': 0.1267245057602048, 'fraction_numerical': 0.004835727492533068, 'mean_word_length': 2.222731439046746, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1788946', 'n_tokens_mistral': 1992, 'n_tokens_neox': 1933, 'n_words': 382}