Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to create custom features through config #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ <h3 class="text-center header">Menu Options Available</h3>
<a class="list-group-item">Google Search the text </a>
<a class="list-group-item">Copy the text</a>
<a class="list-group-item">Speak up the text</a>
<a class="list-group-item">Or add your own custom buttons</a>
</div>
</div>
</div>
Expand All @@ -64,6 +65,7 @@ <h2 class="header text-center">Advanced Usage</h2>
<script type="text/javascript" src="index.js"></script>
<script type="text/javascript" src="selection.js"></script>
<script type="text/javascript">
let customSvg = '<svg class="svg-icon" viewBox="0 0 20 20" width="24" height="24"><path fill="white" d="M7.83,3.404l1.741-2.909C9.367,0.407,9.15,0.344,8.913,0.344c-0.954,0-1.728,0.774-1.728,1.728C7.185,2.613,7.441,3.087,7.83,3.404z"></path><path fill="white" d="M19.645,12.002l-0.464-0.31c-0.09-1.505-0.628-2.972-1.575-4.187l-1.212,0.156l0.084-0.125c0.316-0.473,0.188-1.119-0.286-1.435L15.517,5.65c-0.473-0.316-1.119-0.187-1.435,0.286l-0.095,0.142l-0.538-3.219l-3.271-2.033l-1.754,2.93l0.86,0.481L3.04,3.445L2.136,4.798l3.766,2.516c-0.17,0.204-0.333,0.414-0.484,0.639C5.269,8.178,5.137,8.41,5.013,8.645L1.247,6.129L0.344,7.482l4.878,8.537l0.008-0.013c0.098,0.164,0.207,0.322,0.317,0.479l1.628-0.209l-0.302,0.452c-0.316,0.473-0.187,1.119,0.286,1.435l0.675,0.451c0.473,0.316,1.119,0.188,1.435-0.286l0.252-0.378l0.185,1.442c1.418,0.385,2.904,0.334,4.273-0.105l0.552,0.369c1.512-0.533,2.87-1.542,3.829-2.977C19.32,15.244,19.732,13.602,19.645,12.002z M14.09,16.055c-0.106,0.158-0.32,0.201-0.478,0.095c-0.158-0.106-0.201-0.32-0.095-0.478c0.106-0.158,0.32-0.201,0.478-0.095C14.153,15.682,14.196,15.896,14.09,16.055zM13.969,13.664c-0.513,0.769-1.371,1.227-2.296,1.227c-0.546,0-1.075-0.161-1.531-0.465c-1.265-0.845-1.606-2.561-0.762-3.826c0.513-0.768,1.372-1.227,2.297-1.227c0.545,0,1.074,0.161,1.53,0.465C14.472,10.683,14.814,12.4,13.969,13.664z M15.065,17.476c-0.106,0.158-0.32,0.201-0.478,0.095c-0.158-0.106-0.201-0.32-0.095-0.478c0.106-0.158,0.32-0.201,0.478-0.095C15.129,17.104,15.171,17.318,15.065,17.476z M15.334,14.768c-0.106,0.158-0.32,0.201-0.478,0.095c-0.158-0.106-0.201-0.32-0.095-0.478c0.106-0.158,0.32-0.201,0.478-0.095C15.397,14.396,15.44,14.61,15.334,14.768zM15.568,13.222c-0.158-0.106-0.201-0.32-0.095-0.478c0.106-0.158,0.32-0.201,0.478-0.095c0.158,0.106,0.201,0.32,0.095,0.478C15.94,13.285,15.726,13.327,15.568,13.222z M16.665,15.658c-0.106,0.158-0.32,0.201-0.478,0.095c-0.158-0.106-0.201-0.32-0.095-0.478c0.106-0.158,0.32-0.201,0.478-0.095S16.771,15.499,16.665,15.658z M17.733,13.483c-0.106,0.158-0.32,0.201-0.478,0.095c-0.158-0.106-0.201-0.32-0.095-0.478c0.106-0.158,0.32-0.201,0.478-0.095C17.796,13.111,17.839,13.325,17.733,13.483z"></path><path fill="white" d="M12.824,10.412c-0.341-0.228-0.738-0.349-1.146-0.349c-0.694,0-1.338,0.344-1.723,0.92c-0.634,0.949-0.377,2.236,0.571,2.869c0.342,0.228,0.739,0.35,1.147,0.35c0.694,0,1.337-0.344,1.722-0.92C14.029,12.332,13.773,11.045,12.824,10.412z"></path></svg>'; // credits: Jory Raphael - https://dribbble.com/shots/1400124-More-Free-Star-Wars-Icons
var selection = new Selection();
selection.config({
facebook: true,
Expand All @@ -73,6 +75,7 @@ <h2 class="header text-center">Advanced Usage</h2>
speak:true,
translate:true,
backgroundColor: 'crimson',
customActions: [{"callback": (selection) => {alert(selection)}, "icon": customSvg}],
iconColor: '#fff',
}).init();
</script>
Expand Down
41 changes: 26 additions & 15 deletions selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ const Selection = (function() {
copy: true,
speak: true,
translate: true,
disable: false
disable: false,
customActions: []
};
const twitterConfig = {
url: 'https://twitter.com/intent/tweet?text=',
Expand Down Expand Up @@ -115,6 +116,22 @@ const Selection = (function() {
let top = 0;
let left = 0;

function Button(icon, clickFn) {
const btn = document.createElement('div');
btn.style = 'display:inline-block;' + 'margin:7px;' + 'cursor:pointer;' + 'transition:all .2s ease-in-out;';
btn.innerHTML = icon;
btn.addEventListener('click', function() {
clickFn(text);
});
btn.onmouseover = function() {
this.style.transform = 'scale(1.2)';
};
btn.onmouseout = function() {
this.style.transform = 'scale(1)';
};
return btn;
}

function facebookButton() {
const fbbtn = new Button(facebookConfig.icon, function() {
let sharelink = window.location.href;
Expand Down Expand Up @@ -187,6 +204,13 @@ const Selection = (function() {
count++;
}
})
menu.customActions.forEach((item)=>{
if(item.icon !== undefined){
button = new Button(item.icon, item.callback)
div.appendChild(button);
count++;
}
})
return {
icons: div,
length: count
Expand Down Expand Up @@ -305,6 +329,7 @@ const Selection = (function() {
menu.copy = options.copy === undefined ? menu.copy : options.copy;
menu.speak = options.speak === undefined ? menu.speak : options.speak;
menu.disable = options.disable === undefined ? menu.disable : options.disable;
menu.customActions = options.customActions === undefined ? menu.customActions : options.customActions;

bgcolor = options.backgroundColor || '#333';
iconcolor = options.iconColor || '#fff';
Expand All @@ -323,19 +348,5 @@ const Selection = (function() {
};
}

function Button(icon, clickFn) {
const btn = document.createElement('div');
btn.style = 'display:inline-block;' + 'margin:7px;' + 'cursor:pointer;' + 'transition:all .2s ease-in-out;';
btn.innerHTML = icon;
btn.onclick = clickFn;
btn.onmouseover = function() {
this.style.transform = 'scale(1.2)';
};
btn.onmouseout = function() {
this.style.transform = 'scale(1)';
};
return btn;
}

return _selection;
})();
Loading