Created
December 13, 2011 00:15
-
-
Save takimo/1469778 to your computer and use it in GitHub Desktop.
select2checkbox.user.js の前回押したcheckboxを記録するバージョン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name select2checkbox | |
// @match http://*/* | |
// ==/UserScript== | |
(function(){ | |
// setting | |
var RECORD_MAX_COUNT = 5; | |
// base html & template | |
var CHECKBOX_TEMPLATE = '<label class="EX_select2checkbox"><input type="checkbox" name="%name" value="%value" title="%title">%text</input></label></br>'; | |
var allList = document.createElement('div'); | |
var lastSelectList = document.createElement('div'); | |
// load last select data | |
var lastSelectData = loadLastSelectData(); | |
// search select box | |
var select = document.querySelector('#components'); | |
var options = Array.prototype.slice.apply(select.options); | |
var name = select.name; | |
// convert select to checkbox | |
var injectHTML = ""; | |
for(var i=0;i<options.length;i++){ | |
var html = CHECKBOX_TEMPLATE; | |
html = html.replace('%name', name); | |
html = html.replace('%value', options[i].value); | |
html = html.replace('%title', options[i].innerHTML); | |
html = html.replace('%text', options[i].innerHTML); | |
injectHTML += html; | |
} | |
allList.innerHTML = injectHTML; | |
// append last select list | |
console.log(lastSelectData.length); | |
for(var j=0;j<lastSelectData.length;j++){ | |
var html = CHECKBOX_TEMPLATE; | |
console.log(lastSelectData[j].name); | |
console.log(lastSelectData[j].value); | |
console.log(lastSelectData[j].text); | |
/* | |
html = html.replace('%name', name); | |
html = html.replace('%value', options[i].value); | |
html = html.replace('%text', options[i].innerHTML); | |
injectHTML += html; | |
*/ | |
} | |
// create revert link | |
var revertLink = document.createElement('a'); | |
revertLink.href = "javascript:void(null)"; | |
revertLink.innerHTML = "元に戻す"; | |
revertLink.addEventListener('click', function(){ | |
allList.style.display = "none"; | |
revertLink.style.display = "none"; | |
select.style.display = ""; | |
}); | |
// select box hide | |
select.style.display = "none"; | |
// insert | |
var injectElement = select.parentNode.insertBefore(allList, select); | |
select.parentNode.insertBefore(revertLink, select); | |
// bind evnet | |
setTimeout(function(){ | |
var labels = Array.prototype.slice.apply(document.querySelectorAll('.EX_select2checkbox')); | |
for(var i=0;i<labels.length;i++){ | |
var input = labels[i].querySelector('input'); | |
input.addEventListener('click', function(event){ | |
var component = event.target; | |
var data = { | |
"name": component.name, | |
"value": component.value, | |
"title": component.title | |
}; | |
appendLastSelectData(data); | |
}); | |
} | |
}, 0); | |
function enabledStorage(){ | |
return !!(localStorage && localStorage.getItem && localStorage.setItem); | |
} | |
function loadLastSelectData(){ | |
return (enabledStorage()) ? JSON.parse(localStorage.getItem('net.takimo.extentions.select2checkbox.lastselect')) : null; | |
} | |
function saveLastSelectData(data){ | |
localStorage.setItem('net.takimo.extentions.select2checkbox.lastselect', data); | |
} | |
function removeLastSelectData(data){ | |
localStorage.removeItem('net.takimo.extentions.select2checkbox.lastselect'); | |
} | |
function appendLastSelectData(data){ | |
var lastData = loadLastSelectData(); | |
if(!lastData) lastData = []; | |
// over RECORD_MAX_COUNT | |
lastData = lastData.slice(lastData.length - RECORD_MAX_COUNT, lastData.length); | |
// cull data | |
lastData = cullData(lastData, data); | |
saveLastSelectData(JSON.stringify((lastData))); | |
} | |
function cullData(data, newItem){ | |
console.log("cullData"); | |
var result = []; | |
console.log(data.length); | |
result = data.filter(function(item, index, array){ | |
console.log(index); | |
console.log(item.value, this.value); | |
return !(item.value == this.value); | |
}, newItem); | |
console.log(result); | |
if(result.length < RECORD_MAX_COUNT){ | |
result.push(newItem); | |
}else{ | |
result.shift(); | |
result.push(newItem); | |
} | |
return result; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment