Skip to content

Instantly share code, notes, and snippets.

@davidjgraph
Created October 24, 2013 13:47
Show Gist options
  • Select an option

  • Save davidjgraph/7137663 to your computer and use it in GitHub Desktop.

Select an option

Save davidjgraph/7137663 to your computer and use it in GitHub Desktop.
The iconfinder widget in draw.io
Sidebar.prototype.addIconfinder = function()
{
// TODO: Fix delayed typing, occasional error in library creation in quirks mode
var elt = this.createTitle(mxResources.get('images'));
this.container.appendChild(elt);
var div = document.createElement('div');
div.className = 'geSidebar';
div.style.display = 'none';
div.style.overflow = 'hidden';
div.style.width = '100%';
div.style.padding = '0px';
var inner = document.createElement('div');
inner.className = 'geTitle';
inner.style.backgroundColor = 'transparent';
inner.style.borderColor = 'transparent';
inner.style.padding = '4px';
inner.style.textOverflow = 'clip';
inner.style.cursor = 'default';
if (!mxClient.IS_VML)
{
inner.style.paddingRight = '20px';
}
var searchResource = mxResources.get('search');
var input = document.createElement('input');
input.setAttribute('type', 'text');
input.value = searchResource;
input.style.border = 'solid 1px #d5d5d5';
input.style.width = '100%';
input.style.backgroundImage = 'url(' + IMAGE_PATH + '/clear.gif)';
input.style.backgroundRepeat = 'no-repeat';
input.style.backgroundPosition = '100% 50%';
input.style.paddingRight = '14px';
inner.appendChild(input);
var cross = document.createElement('div');
cross.setAttribute('title', mxResources.get('reset'));
cross.style.position = 'relative';
cross.style.left = '-16px';
cross.style.width = '12px';
cross.style.height = '14px';
cross.style.cursor = 'pointer';
// Workaround for inline-block not supported in IE
cross.style.display = (mxClient.IS_VML) ? 'inline' : 'inline-block';
cross.style.top = ((mxClient.IS_VML) ? 0 : 3) + 'px';
// Needed to block event transparency in IE
cross.style.background = 'url(' + IMAGE_PATH + '/transparent.gif)';
var find;
mxEvent.addListener(cross, 'click', function()
{
input.value = '';
find();
input.focus();
});
inner.appendChild(cross);
div.appendChild(inner);
var center = document.createElement('center');
var button = mxUtils.button(searchResource, function()
{
find();
});
button.setAttribute('disabled', 'true');
// Workaround for inherited line-height in quirks mode
button.style.lineHeight = 'normal';
center.style.paddingTop = '4px';
center.style.marginBottom = '12px';
center.appendChild(button);
div.appendChild(center);
var searchTerm = '';
var modified = false;
var active = false;
var complete = false;
var page = 0;
var count = 25;
function clearDiv()
{
var child = div.firstChild;
while (child != null)
{
var next = child.nextSibling;
if (child != inner && child != center)
{
child.parentNode.removeChild(child);
}
child = next;
}
};
find = mxUtils.bind(this, function(callback)
{
if (input.value != '' || (!modified && input.value == searchResource))
{
if (button.getAttribute('disabled') != 'true')
{
if (center.parentNode != null)
{
if (searchTerm != input.value)
{
clearDiv();
searchTerm = input.value;
complete = false;
page = 0;
}
if (!active)
{
button.style.cursor = 'wait';
button.innerHTML = mxResources.get('loading') + '...';
active = true;
mxUtils.get(ICONFINDER_PATH + '?q=' + encodeURIComponent(searchTerm) + '&c=' + count +
'&p=' + page, mxUtils.bind(this, function(req)
{
active = false;
page++;
center.parentNode.removeChild(center);
var icons = req.getXml().getElementsByTagName('icon');
for (var i = 0; i < icons.length; i++)
{
var size = null;
var url = null;
var child = icons[i].firstChild;
while (child != null && (size == null || url == null))
{
if (child.nodeName == 'size')
{
size = parseInt(mxUtils.getTextContent(child));
}
else if (child.nodeName == 'image')
{
url = mxUtils.getTextContent(child);
}
child = child.nextSibling;
}
if (size != null && url != null)
{
div.appendChild(this.createVertexTemplate('shape=image;image=' + url, size, size, ''));
}
}
if (icons.length < count)
{
button.setAttribute('disabled', 'true');
button.innerHTML = mxResources.get('noMoreResults');
complete = true;
}
else
{
button.innerHTML = mxResources.get('moreResults');
}
button.style.cursor = '';
if (icons.length == 0 && page == 1)
{
var err = document.createElement('div');
err.className = 'geTitle';
err.style.backgroundColor = 'transparent';
err.style.borderColor = 'transparent';
err.style.padding = '4px';
err.style.textAlign = 'center';
err.style.cursor = 'default';
mxUtils.write(err, mxResources.get('noResultsFor', [searchTerm]));
div.appendChild(err);
}
div.appendChild(center);
},
function()
{
button.style.cursor = '';
}));
}
}
}
}
else
{
clearDiv();
searchTerm = '';
button.innerHTML = searchResource;
button.setAttribute('disabled', 'true');
}
});
mxEvent.addListener(input, 'keydown', mxUtils.bind(this, function(evt)
{
if (evt.keyCode == 13 /* Enter */)
{
find();
}
}));
mxEvent.addListener(input, 'keyup', mxUtils.bind(this, function(evt)
{
modified = true;
if (input.value == '' || (!modified && input.value == searchResource))
{
button.setAttribute('disabled', 'true');
}
else if (input.value != searchTerm)
{
button.removeAttribute('disabled');
button.innerHTML = searchResource;
}
else if (!active)
{
if (complete)
{
button.setAttribute('disabled', 'true');
button.innerHTML = mxResources.get('noMoreResults');
}
else
{
button.removeAttribute('disabled');
button.innerHTML = mxResources.get('moreResults');
}
}
}));
mxEvent.addListener(input, 'focus', mxUtils.bind(this, function(evt)
{
if (input.value == searchResource && !modified)
{
input.value = '';
}
}));
mxEvent.addListener(input, 'blur', mxUtils.bind(this, function(evt)
{
if (input.value == '')
{
input.value = searchResource;
modified = false;
}
}));
// Workaround for blocked text selection in Editor
mxEvent.addListener(input, 'mousedown', function(evt)
{
if (evt.stopPropagation)
{
evt.stopPropagation();
}
evt.cancelBubble = true;
});
// Workaround for blocked text selection in Editor
mxEvent.addListener(input, 'selectstart', function(evt)
{
if (evt.stopPropagation)
{
evt.stopPropagation();
}
evt.cancelBubble = true;
});
this.addFoldingHandler(elt, div, function()
{
// not lazy
}, false);
var outer = document.createElement('div');
outer.appendChild(div);
this.container.appendChild(outer);
// Keeps references to the DOM nodes
this.palettes['images'] = [elt, outer];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment