"use strict"; var scramble = (function(rn, rndEl) { //var div = $('
'); //var title = $('').addClass('title'); //var select = $(''); //var select2 = $(''); //var scrLen = $(''); //var sdiv = $(''); var alias = { '333fm': '333', '333oh': '333', '333ft': '333', '444bld': '444wca', '555bld': '555wca' }; //var inputText = $(''); var inputScramble = []; function mega(turns, suffixes, length) { if (suffixes == undefined) { suffixes = [""]; } if (length == undefined) { length = len; } var donemoves = 0; var lastaxis = -1; var s = []; var first, second; for (var i = 0; i < length; i++) { do { first = rn(turns.length); second = rn(turns[first].length); if (first != lastaxis) { donemoves = 0; lastaxis = first; } } while (((donemoves >> second) & 1) != 0); donemoves |= 1 << second; if (turns[first][second].constructor == Array) { s.push(rndEl(turns[first][second]) + rndEl(suffixes)); } else { s.push(turns[first][second] + rndEl(suffixes)); } } return s.join(' '); } // #################### SCRAMBLING #################### var type, lasttype, typeExIn = '333o'; var len = 0; var cubesuff = ["", "2", "'"]; var minxsuff = ["", "2", "'", "2'"]; var scramble, lastscramble; function doScrambleIt() { calcScramble(); if (scramble) { scrambleOK(); } else { sdiv.html("Scramble Error. "); } } function calcScramble() { scramble = ""; var realType = type; if (type in alias) { realType = alias[type]; } if (realType in scramblers) { scramble = scramblers[realType](realType, len); return; } switch (realType) { case "input": if (inputScramble.length == 0) { inputText.val(""); kernel.showDialog([inputText, inputOK, inputCancel], 'input', SCRAMBLE_INPUT); return; } else { scramble = inputScramble.shift(); } break; } } function scrambleOK(scrStr) { scramble = (scrStr || scramble).replace(/(\s*)$/, ""); sdiv.html(scramble); kernel.pushSignal('scramble', [type, scramble]); } function inputOK() { if (!parseInput(inputText.val())) { kernel.setProp('scrType', typeExIn); } else { doScrambleIt(); } } function inputCancel() { kernel.setProp('scrType', typeExIn); } /** * {type: callback(type, length)} * callback return: scramble string or undefined means delay */ var scramblers = {}; function regScrambler(type, callback) { // console.log(type); if (Array.isArray(type)) { for (var i = 0; i < type.length; i++) { scramblers[type[i]] = callback; } } else { scramblers[type] = callback; } return regScrambler; } /** function loadSelect2(idx) { if (!$.isNumeric(idx)) { idx = 0; var selectedStr = scrdata[select[0].selectedIndex][0]; if (selectedStr && selectedStr.match(/^===/)) { select[0].selectedIndex++; } } else { kernel.blur(); } var box2 = scrdata[select[0].selectedIndex][1]; select2.empty(); for (var i = 0; i < box2.length; i++) { select2.append(' scramblers[scrType](scrType, scrArg) * #{args} => mega(args) */ function formatScramble(str,length,manuallyAssignLength,skipPassingSuffixes) { if (length === undefined){ length = 25; } if (manuallyAssignLength === undefined){ manuallyAssignLength = false; } if (skipPassingSuffixes === undefined){ skipPassingSuffixes = false; } var repfunc = function(match, p1) { // console.log(match); if (match[0] == '$') { var args = [p1]; if (p1[0] == '[') { args = JSON.parse(p1); } // console.log(args[0]); // console.log(scramblers[args[0]]); return scramblers[args[0]].apply(this, args); } else if (match[0] == '#') { var theArgs = JSON.parse('[' + p1 + ']'); if (manuallyAssignLength) { if (skipPassingSuffixes){ theArgs.push(undefined); } theArgs.push(length); } return mega.apply(this, theArgs); } else { return ''; } }; var re1 = /[$#]\{([^\}]+)\}/g; return str.replace(re1, repfunc); } /** $(function() { kernel.regListener('scramble', 'time', procSignal); kernel.regListener('scramble', 'property', procSignal, /^scr(?:Size|Mono|Type|Lim|Align)$/); kernel.regListener('scramble', 'button', procSignal, /^scramble$/); kernel.regListener('scramble', 'ctrl', procSignal, /^scramble$/); kernel.regProp('scramble', 'scrSize', 2, PROPERTY_SCRSIZE, [15, 5, 50]); kernel.regProp('scramble', 'scrMono', 0, PROPERTY_SCRMONO, [true]); kernel.regProp('scramble', 'scrLim', 0, PROPERTY_SCRLIM, [true]); kernel.regProp('scramble', 'scrAlign', 1, PROPERTY_SCRALIGN, ['c', ['c', 'l', 'r'], PROPERTY_SCRALIGN_STR.split('|')]); kernel.regProp('scramble', 'preScr', 1, "pre-scramble", ['', ['', 'z2', "z'", 'z', "x'", 'x'], ['', 'z2', "z'", 'z', "x'", 'x'] ]); for (var i = 0; i < scrdata.length; i++) { select.append(''); } kernel.getProp('scrType', '333'); select.change(loadSelect2); select2.change(function() { kernel.blur(); scrLen.val(Math.abs(scrdata[select[0].selectedIndex][1][select2[0].selectedIndex][2])); scrLen[0].disabled = scrdata[select[0].selectedIndex][1][select2[0].selectedIndex][2] <= 0; genScramble(); }); scrLen.change(genScramble); var last = $('').addClass('click').html(SCRAMBLE_LAST).click(function() { sdiv.html(lastscramble); if (lastscramble != undefined) { kernel.pushSignal('scrambleX', [lasttype, lastscramble]); } }); var next = $('').addClass('click').html(SCRAMBLE_NEXT).click(function() { if (sdiv.html() == lastscramble) { sdiv.html(scramble); kernel.pushSignal('scrambleX', [type, scramble]); } else { genScramble(); } }); title.append($('