(function($) { function IconBrowser($el, options) { var iconBrowser = this; var modalSelector = "#" + options.modalId; var scrollPositionDogear; $.extend(iconBrowser, { element: $el, modal: $(modalSelector), modalId: options.modalId }); $(options.triggerSelector).attr("data-reveal-id", options.modalId); new Options(this.modal, options.preferences); $(document) .on('open.fndtn.reveal', modalSelector, function(e) { // hackety hack -- being triggered on both 'open' and 'open.fndtn.reveal'; just want one if (e.namespace === "") return; // If the page scrolled sideways, don't put the modal way out in left field. iconBrowser.modal.css('left', window.scrollX); iconBrowser.loadIcons(); iconBrowser.registerListeners(); }) // Save and restore the scroll position when opening and closing the // modal. This is crucial on mobile if you have dozens of icons, because // otherwise it'll ditch you miles out into the comment thread, as you // wonder where you left your reply form and whether you have enough // water to survive the walk back to the gas station. .on('opened.fndtn.reveal', modalSelector, function(e) { // hackety hack -- being triggered on both 'opened' and 'opened.fndtn.reveal'; just want one if (e.namespace === "") return; scrollPositionDogear = $(window).scrollTop(); iconBrowser.modal.removeAttr('tabindex'); // WHY does foundation.reveal set this. iconBrowser.focusActive(); }) .on('closed.fndtn.reveal', modalSelector, function(e) { // hackety hack -- being triggered on both 'closed' and 'closed.fndtn.reveal'; just want one if (e.namespace === "") return; if ( Math.abs( $(window).scrollTop() - scrollPositionDogear ) > 500 ) { $(window).scrollTop(scrollPositionDogear); } // the browser blew away the user's tab-through position, so restore // it on whatever makes most sense to focus. Defaults to the icon // menu, since that's what they just indirectly set a value for, but // in comment forms we ask to focus the message body instead. var $focusTarget = $el; if ( options.focusAfterBrowse ) { var $altTarget = $( options.focusAfterBrowse ).first(); if ( $altTarget.length === 1 ) { $focusTarget = $altTarget; } } // Only force-reset the focus if we know it's still wrong! If the // user somehow managed to focus something else before this handler // fired, don't jerk them around. if ( document.activeElement.tagName === 'BODY' ) { $focusTarget.focus(); } }); } IconBrowser.prototype = { kwToIcon: {}, selectedId: undefined, selectedKeyword: undefined, iconBrowserItems: [], iconsList: undefined, isLoaded: false, listenersRegistered: false, loadIcons: function() { var iconBrowser = this; if ( iconBrowser.isLoaded ) { iconBrowser.resetFilter(); iconBrowser.initializeKeyword(); } else { var searchField = $("#js-icon-browser-search"); searchField.prop("disabled", true); var url = Site.currentJournalBase ? "/" + Site.currentJournal + "/__rpc_userpicselect" : "/__rpc_userpicselect"; $.getJSON(url).then(function(data) { var $content = $("#js-icon-browser-content"); var $status = $content.find(".icon-browser-status"); if ( !data ) { $status.html("

Unable to load icons

"); return; } if ( data.alert ) { $status.html("

" + data.alert + "

"); return; } $status.remove(); var $iconslist = $content.find("ul"); // Save it, we'll need it for sorting later. iconBrowser.iconsList = $iconslist; $iconslist.empty(); var pics = data.pics; $.each(data.ids, function(index,id) { var icon = pics[id]; var idstring = "js-icon-browser-icon-"+id; var $img = $("").attr( { src: icon.url, alt: icon.alt, title: icon.keywords.join(', '), height: icon.height, width: icon.width, "class": "th" } ) .wrap("