<commit_before>/* Part API functions
 * Requires api.js to be loaded first
 */

function getPartCategoryList(filters={}, options={}) {
    return inventreeGet('/api/part/category/', filters, options);
}

function getSupplierPartList(filters={}, options={}) {
    return inventreeGet('/api/part/supplier/', filters, options);
}

function getPartList(filters={}, options={}) {
    return inventreeGet('/api/part/', filters, options);
}

function getBomList(filters={}, options={}) {
    return inventreeGet('/api/bom/', filters, options);
}

function toggleStar(options) {
    /* Toggle the 'starred' status of a part.
     * Performs AJAX queries and updates the display on the button.
     * 
     * options:
     * - button: ID of the button (default = '#part-star-icon')
     * - part: pk of the part object
     * - user: pk of the user
     */

    var url = '/api/part/star/';

    inventreeGet(
        url,
        {
            part: options.part,
            user: options.user,
        },
        {
            success: function(response) {
                if (response.length == 0) {
                    // Zero length response = star does not exist
                    // So let's add one!
                    inventreePut(
                        url,
                        {
                            part: options.part,
                            user: options.user,
                        },
                        {
                            method: 'POST',
                            success: function(response, status) {
                                $(options.button).removeClass('glyphicon-star-empty').addClass('glyphicon-star');
                            },
                        }
                    );
                } else {
                    var pk = response[0].pk;
                    // There IS a star (delete it!)
                    inventreePut(
                        url + pk + "/",
                        {
                        },
                        {
                            method: 'DELETE',
                            success: function(response, status) {
                                $(options.button).removeClass('glyphicon-star').addClass('glyphicon-star-empty');
                            },
                        }
                    );
                }
            },
        }
    );
}

function loadPartTable(table, url, options={}) {
    /* Load part listing data into specified table.
     * 
     * Args:
     *  - table: HTML reference to the table
     *  - url: Base URL for API query
     *  - options: object containing following (optional) fields
     *      allowInactive: If true, allow display of inactive parts
     *      checkbox: Show the checkbox column
     *      query: extra query params for API request
     *      buttons: If provided, link buttons to selection status of this table
     */

    var params = options.parms || {};

    var filters = loadTableFilters("parts");

    for (var key in params) {
        filters[key] = params[key];
    }

    setupFilterList("parts", $(table));

    var columns = [
        {
            field: 'pk',
            title: 'ID',
            visible: false,
        }
    ];

    if (options.checkbox) {
        columns.push({
            checkbox: true,
            title: 'Select',
            searchable: false,
        });
    }

    columns.push({
        field: 'name',
        title: 'Part',
        sortable: true,
        formatter: function(value, row, index, field) {

            var name = '';

            if (row.IPN) {
                name += row.IPN;
                name += ' | ';
            }

            name += value;

            if (row.revision) {
                name += ' | ';
                name += row.revision;
            }

            if (row.is_template) {
                name = '<i>' + name + '</i>';
            }

            var display = imageHoverIcon(row.thumbnail) + renderLink(name, '/part/' + row.pk + '/');
            
            if (row.is_template) {
                display = display + `<span class='fas fa-clone label-right' title='Template part'></span>`;
            }
            
            if (row.assembly) {
                display = display + `<span class='fas fa-tools label-right' title='Assembled part'></span>`;
            }

            /*
            if (row.component) {
                display = display + `<span class='fas fa-cogs label-right' title='Component part'></span>`;
            }
            */
            
            if (!row.active) {
                display = display + `<span class='fas fa-times label-right' title='Inactive part'></span>`;
            }
            return display; 
        }
    });

    columns.push({
        sortable: true,
        field: 'description',
        title: 'Description',
        formatter: function(value, row, index, field) {

            if (row.is_template) {
                value = '<i>' + value + '</i>';
            }

            return value;
        }
    });
    
    columns.push({
        sortable: true,
        field: 'category__name',
        title: 'Category',
        formatter: function(value, row, index, field) {
            if (row.category) {
                return renderLink(row.category__name, "/part/category/" + row.category + "/");
            }<commit_msg>Revert display of "INACTIVE" parts<commit_after>/* Part API functions
 * Requires api.js to be loaded first
 */

function getPartCategoryList(filters={}, options={}) {
    return inventreeGet('/api/part/category/', filters, options);
}

function getSupplierPartList(filters={}, options={}) {
    return inventreeGet('/api/part/supplier/', filters, options);
}

function getPartList(filters={}, options={}) {
    return inventreeGet('/api/part/', filters, options);
}

function getBomList(filters={}, options={}) {
    return inventreeGet('/api/bom/', filters, options);
}

function toggleStar(options) {
    /* Toggle the 'starred' status of a part.
     * Performs AJAX queries and updates the display on the button.
     * 
     * options:
     * - button: ID of the button (default = '#part-star-icon')
     * - part: pk of the part object
     * - user: pk of the user
     */

    var url = '/api/part/star/';

    inventreeGet(
        url,
        {
            part: options.part,
            user: options.user,
        },
        {
            success: function(response) {
                if (response.length == 0) {
                    // Zero length response = star does not exist
                    // So let's add one!
                    inventreePut(
                        url,
                        {
                            part: options.part,
                            user: options.user,
                        },
                        {
                            method: 'POST',
                            success: function(response, status) {
                                $(options.button).removeClass('glyphicon-star-empty').addClass('glyphicon-star');
                            },
                        }
                    );
                } else {
                    var pk = response[0].pk;
                    // There IS a star (delete it!)
                    inventreePut(
                        url + pk + "/",
                        {
                        },
                        {
                            method: 'DELETE',
                            success: function(response, status) {
                                $(options.button).removeClass('glyphicon-star').addClass('glyphicon-star-empty');
                            },
                        }
                    );
                }
            },
        }
    );
}

function loadPartTable(table, url, options={}) {
    /* Load part listing data into specified table.
     * 
     * Args:
     *  - table: HTML reference to the table
     *  - url: Base URL for API query
     *  - options: object containing following (optional) fields
     *      allowInactive: If true, allow display of inactive parts
     *      checkbox: Show the checkbox column
     *      query: extra query params for API request
     *      buttons: If provided, link buttons to selection status of this table
     */

    var params = options.parms || {};

    var filters = loadTableFilters("parts");

    for (var key in params) {
        filters[key] = params[key];
    }

    setupFilterList("parts", $(table));

    var columns = [
        {
            field: 'pk',
            title: 'ID',
            visible: false,
        }
    ];

    if (options.checkbox) {
        columns.push({
            checkbox: true,
            title: 'Select',
            searchable: false,
        });
    }

    columns.push({
        field: 'name',
        title: 'Part',
        sortable: true,
        formatter: function(value, row, index, field) {

            var name = '';

            if (row.IPN) {
                name += row.IPN;
                name += ' | ';
            }

            name += value;

            if (row.revision) {
                name += ' | ';
                name += row.revision;
            }

            if (row.is_template) {
                name = '<i>' + name + '</i>';
            }

            var display = imageHoverIcon(row.thumbnail) + renderLink(name, '/part/' + row.pk + '/');
            
            if (row.is_template) {
                display += `<span class='fas fa-clone label-right' title='Template part'></span>`;
            }
            
            if (row.assembly) {
                display += `<span class='fas fa-tools label-right' title='Assembled part'></span>`;
            }

            /*
            if (row.component) {
                display = display + `<span class='fas fa-cogs label-right' title='Component part'></span>`;
            }
            */
            
            if (!row.active) {
                display += `<span class='label label-warning label-right'>INACTIVE</span>`; 
            }
            return display; 
        }
    });

    columns.push({
        sortable: true,
        field: 'description',
        title: 'Description',
        formatter: function(value, row, index, field) {

            if (row.is_template) {
                value = '<i>' + value + '</i>';
            }

            return value;
        }
    });
    
    columns.push({
        sortable: true,
        field: 'category__name',
        title: 'Category',
        formatter: function(value, row, index, field) {
            if (row.category) {
                return renderLink(row.category__name, "/part/category/" + row.category + "/");
            }
{'fraction_non_alphanumeric': 0.12189889945905615, 'fraction_numerical': 0.00037306472673008767, 'mean_word_length': 1.2386221294363258, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, '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': '409745', 'n_tokens_mistral': 2831, 'n_tokens_neox': 2703, 'n_words': 685}