<commit_before>import Ember from 'ember';

// when godForm call delete, pass selectedItem to success
let deleteObject = function (selectedItem){
    this.toggleProperty('loading');
    this.get('store').deleteRecord(this.get('modelName'), selectedItem).then(Ember.run.bind(this, function(data) {
        this.toggleProperty('loading');
        this.send('success', 'delete', data, selectedItem);
    }), Ember.run.bind(this, function(reason) {
        this.toggleProperty('loading');
        this.send('fail', 'delete', reason, selectedItem);
    }));
};


// godForm mixin is used to controll many object's removing and adding and editing
var godForm = Ember.Mixin.create({
    modelName: '',
    model: null,
    selectedItem: null,
    modalShow: false,
    reason: null,
    store: Ember.inject.service(),
    actions: {
        /**
         * @function add create new record according to modelName
         * @returns  void
         */
        add() {
            this.set('modalShow', true);
            this.set('selectedItem', this.get('store').createRecord(this.get('modelName')));
        },
        /**
         * @function edit triggle when user click edit action
         * @returns  void
         */
        edit(selectedItem) {
            this.set('modalShow', true);
            if (!Ember.isNone(selectedItem)) {
                this.set('selectedItem', selectedItem);
            }
        },
        cancel(){
            this.set('modalShow', false);
        },
        remove(selectedItem){
            deleteObject.call(this, selectedItem);
        },
        /**
         * @function  success ajax request success callback
         * @returns  void
         */
        success(action, data, selectedItem) {
            Ember.Logger.info('subclass override this function for response data');
            this.set('modalShow', false);
            if(this.sendAction) {
                this.sendAction('success', action, data, selectedItem);
            }

            if(action === 'create'){
                this.model.insertAt(0, selectedItem);
            }

            if(action === 'delete'){
                this.model.removeObject(selectedItem);
            }
        },
        /**
         * @function  success ajax request success callback
         * @returns  void
         */
        fail(action, reason, selectedItem) {
            Ember.Logger.info('subclass override this function for fail request');
            this.set('reason', reason);
            if(this.sendAction) {
                this.sendAction('fail', action, reason, selectedItem);
            }
        },
    },
    init(){
        this._super(...arguments);
        if(!this.modelName){
            throw new Error(`mixin godForm modelName is invalid: ${this.modelName}`);
        }
    }
});


var formComponent = Ember.Mixin.create({
    modelName: '',
    model: null,
    store: Ember.inject.service(),
    reason: null,
    actions: {
        /**
         * @function save triggle when user click save action
         * @returns  void
         */
        save() {
            this.toggleProperty('loading');
            if (!this.validate()) {return;}
            let primaryKey = this.get('store').modelFor(this.modelName).primaryKey;
            let actionName = Ember.get(this.model, primaryKey) ? 'update' : 'create';
            this.get('store').save(this.get('modelName'), this.get('model')).then(Ember.run.bind(this, function(data) {
                this.toggleProperty('loading');
                this.send('success', actionName, data);
            }), Ember.run.bind(this, function(reason) {
                this.toggleProperty('loading');
                this.send('fail', actionName, reason);
            }));
        },

        /**
         * @function delete triggle when user click save action
         * @returns  void
         */
        remove() {
            deleteObject.call(this, this.get('model'));
        },

        /**
         * @function  success ajax request success callback
         * @returns  void
         */
        success(action, data) {
            Ember.Logger.info('subclass override this function for response data');
            if((action === 'create'|| action === 'update') && data){
                Ember.setProperties(this.model, data);
            }
            this.sendAction('success', action, data, this.get('model'));
        },
        /**
         * @function  success ajax request success callback
         * @returns  void
         */
        fail(action, reason) {
            Ember.Logger.info('subclass override this function for fail request');
            this.set('reason', reason);
            this.sendAction('fail', action, reason, this.get('model'));
        },
        /**
         * @function validate validate model 
         * @returns  Boolean
         */
        cancel() {
            Ember.Logger.info('subclass override this function for form modify cancel');
            this.sendAction('cancel', this.get('model'));
        },
    },
    validate() {
        Ember.Logger.info('subclass override this function for model validate');
        return true;
    },
    init(){
        this._super(...arguments);
        if(!this.modelName){
            throw new Error(`mixin formComponent modelName is invalid: ${this.modelName}`);
        }
    }
});


export {
    formComponent,
    godForm<commit_msg>fix #14<commit_after>import Ember from 'ember';

// when godForm call delete, pass selectedItem to success
let deleteObject = function (selectedItem){
    if(!this.modelName){
        throw new Error(`mixin component modelName is invalid: ${this.modelName}`);
    }
    this.set('loading', true);
    this.get('store').deleteRecord(this.get('modelName'), selectedItem).then((data)=>{
        this.set('loading', false);
        this.send('success', 'delete', data, selectedItem);
    }).catch((reason)=>{
        this.set('loading', false);
        this.send('fail', 'delete', reason, selectedItem);
    });
};


// godForm mixin is used to controll many object's removing and adding and editing
var godForm = Ember.Mixin.create({
    modelName: '',
    model: null,
    selectedItem: null,
    modalShow: false,
    reason: null,
    store: Ember.inject.service(),
    actions: {
        /**
         * @function add create new record according to modelName
         * @returns  void
         */
        add() {
            this.set('modalShow', true);
            this.set('selectedItem', this.get('store').createRecord(this.get('modelName')));
        },
        /**
         * @function edit triggle when user click edit action
         * @returns  void
         */
        edit(selectedItem) {
            this.set('modalShow', true);
            if (!Ember.isNone(selectedItem)) {
                this.set('selectedItem', selectedItem);
            }
        },
        cancel(){
            this.set('modalShow', false);
        },
        remove(selectedItem){
            deleteObject.call(this, selectedItem);
        },
        /**
         * @function  success ajax request success callback
         * @returns  void
         */
        success(action, data, selectedItem) {
            Ember.Logger.info('subclass override this function for response data');
            this.set('modalShow', false);
            if(this.sendAction) {
                this.sendAction('success', action, data, selectedItem);
            }

            if(action === 'create'){
                this.model.insertAt(0, selectedItem);
            }

            if(action === 'delete'){
                this.model.removeObject(selectedItem);
            }
        },
        /**
         * @function  success ajax request success callback
         * @returns  void
         */
        fail(action, reason, selectedItem) {
            Ember.Logger.info('subclass override this function for fail request');
            this.set('reason', reason);
            if(this.sendAction) {
                this.sendAction('fail', action, reason, selectedItem);
            }
        },
    }
});


var formComponent = Ember.Mixin.create({
    modelName: '',
    model: null,
    store: Ember.inject.service(),
    reason: null,
    actions: {
        /**
         * @function save triggle when user click save action
         * @returns  void
         */
        save() {
            if(!this.modelName){
                throw new Error(`mixin formComponent modelName is invalid: ${this.modelName}`);
            }
            this.set('loading', true);
            if (!this.validate()) {return;}
            let primaryKey = this.get('store').modelFor(this.modelName).primaryKey;
            let actionName = Ember.get(this.model, primaryKey) ? 'update' : 'create';
            this.get('store').save(this.get('modelName'), this.get('model')).then((data)=>{
                this.set('loading', false);
                this.send('success', actionName, data);
            }).catch((reason)=>{
                this.set('loading', false);
                this.send('fail', actionName, reason);
            });
        },

        /**
         * @function delete triggle when user click save action
         * @returns  void
         */
        remove() {
            deleteObject.call(this, this.get('model'));
        },

        /**
         * @function  success ajax request success callback
         * @returns  void
         */
        success(action, data) {
            Ember.Logger.info('subclass override this function for response data');
            if((action === 'create'|| action === 'update') && data){
                Ember.setProperties(this.model, data);
            }
            this.sendAction('success', action, data, this.get('model'));
        },
        /**
         * @function  success ajax request success callback
         * @returns  void
         */
        fail(action, reason) {
            Ember.Logger.info('subclass override this function for fail request');
            this.set('reason', reason);
            this.sendAction('fail', action, reason, this.get('model'));
        },
        /**
         * @function validate validate model 
         * @returns  Boolean
         */
        cancel() {
            Ember.Logger.info('subclass override this function for form modify cancel');
            this.sendAction('cancel', this.get('model'));
        },
    },
    validate() {
        Ember.Logger.info('subclass override this function for model validate');
        return true;
    }
});


export {
    formComponent,
    godForm
{'fraction_non_alphanumeric': 0.13465553235908143, 'fraction_numerical': 0.0003795786676788764, 'mean_word_length': 1.8560975609756099, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3995913', 'n_tokens_mistral': 2758, 'n_tokens_neox': 2705, 'n_words': 636}