<commit_before>/**
 * @license
 * Patterns @VERSION@ setclass - update class on click
 *
 * Copyright 2008-2012 Simplon B.V.
 * Copyright 2011 <NAME>
 * Copyright 2011 SYSLAB.COM GmbH
 */
define([
    'require',
    '../core/store',
    '../utils'
], function(require) {
    var store = require('../core/store');
    var utils = require('../utils');

    var setclass = {
        init: function() {
            store.initPatternStore('setclass');

            $(store.getPatternAttributes('setclass')).each(function(index) {
                var values = this.split('!'); // 0: id, 1: attribute, 2: value, 3: other
                var obj = {
                        'index': index+1,
                        "id": values[0],
                        "attr": values[1],
                        'value': values[2],
                        'other': values[3]
                    };

                setclass.store[obj.id + "." + obj.attr] = obj;
            });

            $('[data-setclass]').live('click', setclass.handleClick).each(function() {
                var $this = $(this);
                var obj = setclass.getObjFromParams(
                              $this,
                                utils.extractParameters('!' + $this.attr('data-setclass'))
                          );

                if (obj === null) return;

                if ( !obj.store ) {
                     if (setclass.store[obj.id + "." + obj.attr] ) {
                        delete setclass.store[obj.id + "." + obj.attr];
                     }
                } else {
                     if (setclass.store[obj.id + "." + obj.attr] ) return;
                }
                if ( obj.attr === 'class' ) {
                //    $( "#" + obj.id ).addClass( obj.value );  // removed the removeClass which was used in toggle
                } else {
                    $( "#" + obj.id ).attr( obj.attr, obj.value );
                }

                if (obj.store) {
                    setclass.storeValue(obj.id, obj.attr, obj.value, obj.other);
                }
            });

            var key;
            for (key in setclass.store ) {
                var obj = setclass.store[key];
                if ( obj.attr === 'class' ) {
                    $( "#" + obj.id ).removeClass( obj.other ).addClass( obj.value );
                } else {
                    $( "#" + obj.id ).attr( obj.attr, obj.value );
                }
            }
        },

        getObjFromParams: function($elem, params) {
            var values = params.values;
            var obj = {};

            obj.id = params.id || $elem.attr("id");
            obj.attr = params.attr || 'class';
            obj.store = params.store || false;

            if (typeof obj.id !== "string" || obj.id.length === 0 ||
                typeof obj.attr !== 'string' || obj.attr.length === 0 ||
                typeof values  !== 'string' || values.length === 0 ) {
                return null;
            }

            values = values.split(':');
            if ( values.length == 1) {
                values.push('');
            }

            obj.value = values[0];
            obj.other = values[1];
            return obj;
        },

        handleClick: function(event) {
            var $this = $(this);
            if ($this.hasClass('cant-touch-this')) return;
            var params = utils.extractParameters('!' + $this.attr('data-setclass'));

            setclass.execute($this, '', '', params, event);

            event.preventDefault();
        },

        store: {},

        dataAttr: true,

        execute: function( elem, url, sources, params, event ) {
            var value, other;
            var obj = setclass.getObjFromParams( elem, params );
            if (obj === null) return false;

            var $setclass = $("#" + obj.id);
            if ($setclass.length === 0) return false;

            if (obj.attr === 'class') {
                if (obj.other.length > 0 ) {
                    var cls = $setclass.attr('class').split(' ');
                    regval = new RegExp(obj.value);
                    for (i=0;i<cls.length;i++){
                        if (cls[i].match(regval)) {
                            $setclass.removeClass(cls[i]);
                        }
                    }
                    $setclass.addClass(obj.other);
                } else if ($setclass.hasClass(obj.value) || $setclass.hasClass(obj.other)) {
                    /* obj.value already set and no other present. pass */
                } else {
                    $setclass.addClass(obj.value);
                }
            } else {
                /* cave, haven't touched that yet, is still behaving like toggle */
            /*  var current = $setclass.attr(obj.attr);
                if (current === obj.value) {
                    $setclass.attr(obj.attr, obj.other);
                    value = obj.other;
                    other = obj.value;
                } else if (current === obj.other) {
                    $setclass.attr(obj.attr, obj.value);
                    value = obj.value;
                    other = obj.other;
                } else {
                    $setclass.attr(obj.attr, obj.other);
                    value = obj.other;
                    other = obj.value;
                }*/
            }

            if (obj.store) setclass.storeValue(obj.id, obj.attr, value, other);

            return true;
        },

        storeValue: function(id, attr, value, other) {
            var aStore = setclass.store[id + '.' + attr];
            if ( aStore ) {
                store.setPatternAttribute('setclass', aStore.index, id + "!" + attr + "!" + value + "!" + other);
            } else {
                store.addPatternAttribute('setclass', id + "!" + attr + "!" + value + "!" + other);
            }
        }
    };
    return setclass;
});
// jshint indent: 4, browser: true, jquery: true, quotmark: double
// vim: sw=4 expandtab<commit_msg>Update setclass for new storage API.<commit_after>/**
 * @license
 * Patterns @VERSION@ setclass - update class on click
 *
 * Copyright 2008-2012 Simplon B.V.
 * Copyright 2011 <NAME>
 * Copyright 2011 SYSLAB.COM GmbH
 */
define([
    'require',
    '../core/store',
    '../utils'
], function(require, store, utils) {
    var storage = store.session("setclass");

    var setclass = {
        init: function() {

            $(store.getPatternAttributes('setclass')).each(function(index) {
                var values = this.split('!'); // 0: id, 1: attribute, 2: value, 3: other
                var obj = {
                        'index': index+1,
                        "id": values[0],
                        "attr": values[1],
                        'value': values[2],
                        'other': values[3]
                    };

                setclass.store[obj.id + "." + obj.attr] = obj;
            });

            $(document).on("click", '[data-setclass]').live('click', setclass.handleClick);
            $("[data-setclass]").each(function() {
                var $this = $(this);
                var obj = setclass.getObjFromParams(
                              $this,
                              utils.extractParameters('!' + $this.attr('data-setclass')));

                if (obj === null)
                    return;

                if ( !obj.store ) {
                    storage.remove(obj.id + "." + obj.attr);
                } else if (storage.get(obj.id + "." + obj.attr))
                    return;

                if (obj.attr === 'class') {
                //    $( "#" + obj.id ).addClass( obj.value );  // removed the removeClass which was used in toggle
                } else {
                    $( "#" + obj.id ).attr( obj.attr, obj.value );
                }

                if (obj.store)
                    storage.set(obj.id + "." + obj.attr, obj);
            });

            var all = storage.all();
            for (var key in all ) {
                var obj = all[key];
                if ( obj.attr === 'class' ) {
                    $( "#" + obj.id ).removeClass( obj.other ).addClass( obj.value );
                } else {
                    $( "#" + obj.id ).attr( obj.attr, obj.value );
                }
            }
        },

        getObjFromParams: function($elem, params) {
            var values = params.values;
            var obj = {};

            obj.id = params.id || $elem.attr("id");
            obj.attr = params.attr || 'class';
            obj.store = params.store || false;

            if (typeof obj.id !== "string" || obj.id.length === 0 ||
                typeof obj.attr !== 'string' || obj.attr.length === 0 ||
                typeof values  !== 'string' || values.length === 0 ) {
                return null;
            }

            values = values.split(':');
            if ( values.length == 1) {
                values.push('');
            }

            obj.value = values[0];
            obj.other = values[1];
            return obj;
        },

        handleClick: function(event) {
            var $this = $(this);
            if ($this.hasClass('cant-touch-this')) return;
            var params = utils.extractParameters('!' + $this.attr('data-setclass'));

            setclass.execute($this, '', '', params, event);

            event.preventDefault();
        },

        store: {},

        dataAttr: true,

        execute: function( elem, url, sources, params, event ) {
            var value, other;
            var obj = setclass.getObjFromParams( elem, params );
            if (obj === null) return false;

            var $setclass = $("#" + obj.id);
            if ($setclass.length === 0) return false;

            if (obj.attr === 'class') {
                if (obj.other.length > 0 ) {
                    var cls = $setclass.attr('class').split(' ');
                    regval = new RegExp(obj.value);
                    for (i=0;i<cls.length;i++){
                        if (cls[i].match(regval)) {
                            $setclass.removeClass(cls[i]);
                        }
                    }
                    $setclass.addClass(obj.other);
                } else if ($setclass.hasClass(obj.value) || $setclass.hasClass(obj.other)) {
                    /* obj.value already set and no other present. pass */
                } else {
                    $setclass.addClass(obj.value);
                }
            } else {
                /* cave, haven't touched that yet, is still behaving like toggle */
            /*  var current = $setclass.attr(obj.attr);
                if (current === obj.value) {
                    $setclass.attr(obj.attr, obj.other);
                    value = obj.other;
                    other = obj.value;
                } else if (current === obj.other) {
                    $setclass.attr(obj.attr, obj.value);
                    value = obj.value;
                    other = obj.other;
                } else {
                    $setclass.attr(obj.attr, obj.other);
                    value = obj.other;
                    other = obj.value;
                }*/
            }

            if (obj.store)
                storage.set(obj.id + "." + obj.attr, obj);

            return true;
        }
    };
    return setclass;
});
// jshint indent: 4, browser: true, jquery: true, quotmark: double
// vim: sw=4 expandtab
{'fraction_non_alphanumeric': 0.14648899798369422, 'fraction_numerical': 0.006311913737178925, 'mean_word_length': 1.1913177103342298, 'pattern_counts': {'":': 4, '<': 7, '<?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': '4520096', 'n_tokens_mistral': 3186, 'n_tokens_neox': 2952, 'n_words': 675}