//================================ // Helper functions for Stack.PHP //================================ // An object containing all Stack.PHP utility methods. var StackPHP = { // The API key to use (blank by default) APIKey: '', ClientID: 0, // Used for generating unique numbers UniqueNumber: 0, // Generates a number that is unique for the current page GenerateUniqueNumber: function() { return StackPHP.UniqueNumber++; }, // Creates a DIV with a given ID CreateDIV: function(div_id, div_html, div_classes) { // Create the element var element = document.createElement('div'); element.setAttribute('id', div_id); document.getElementsByTagName('body')[0].appendChild(element); // Assign the provided HTML to the element if(typeof div_html != 'undefined') element.innerHTML = div_html; // Assign the provided classes to the div if(typeof div_classes != 'undefined') element.className = div_classes; }, // Creates the dialog with the specified text and displays it CreateDialog: function(dialog_title, dialog_contents, dialog_class) { // Create the canvas and dialog StackPHP.CreateDIV('stackphp_canvas'); StackPHP.CreateDIV('stackphp_dialog', '
X

' + dialog_title + '

' + dialog_contents + '
', dialog_class); // Position the dialog in the center of the screen var dialog_width = document.getElementById('stackphp_dialog').offsetWidth; var dialog_height = document.getElementById('stackphp_dialog').offsetHeight; var leftpos = document.getElementsByTagName('body')[0].clientWidth / 2 - (dialog_width / 2); var toppos = window.innerHeight / 2 - (dialog_height / 2); document.getElementById('stackphp_dialog').style.left = leftpos + 'px'; document.getElementById('stackphp_dialog').style.top = toppos + 'px'; }, // Formats a numbers such that larger ones are more // readable. FormatNumber: function(number) { if(number < 10000) return number; else if(number < 1000000) return (parseInt(number / 100) / 10) + 'k'; else return (parseInt(number / 100000) / 10) + 'm'; }, // Retrieves data from the API RunAPIMethod: function(site, method, success_callback, error_callback) { // This is tricky because we need to use JSONP to retrieve the data. Create the callback // function we will use to receive the data that comes in. var jsonp_callback = 'jsonp_' + StackPHP.GenerateUniqueNumber(); // Create the callback that we will use window[jsonp_callback] = function(data) { if(typeof data['error_id'] == 'undefined') success_callback(data); else error_callback(data['error_message']); }; // Create the script element and set its source var script_response = document.createElement('script'); script_response.src = 'http://api.stackexchange.com/2.0' + method + ((method.indexOf('?') != -1)?'&':'?') + 'key=' + StackPHP.APIKey + '&callback=' + jsonp_callback + '&site=' + site; document.getElementsByTagName('head')[0].appendChild(script_response); }, // Finds a user based on their ID FindUser: function(site, element_id) { StackPHP.CreateDialog('Find User ID', 'Enter part of your username:


', 'stackphp_find_dialog'); // Focus the input element and set an event handler to activate // the search when enter is pushed. document.getElementById('stackphp_find_search').focus(); document.getElementById('stackphp_find_search').onkeydown = function(event) { if(event.keyCode == 13) document.getElementById('stackphp_find_button').onclick(); } // When the find button is clicked, this is the event handler: document.getElementById('stackphp_find_button').onclick = function() { // Show the loading message document.getElementById('stackphp_find_results').innerHTML = '
Loading
'; // Create a timer to run the loading message var timer_id; var loading_offset = 0; var UpdateLoadingMessage = function() { document.getElementById('stackphp_find_loading').innerHTML = 'Loading'; var num_periods = (loading_offset++) % 5; for(var i=0; i⋄ ' + data['items'][i]['display_name'] + ' [' + StackPHP.FormatNumber(data['items'][i]['reputation']) + ']'; } else document.getElementById('stackphp_find_results').innerHTML = '
No results
'; }, function(message) { // Stop the timer window.clearTimeout(timer_id); // Display the error document.getElementById('stackphp_find_results').innerHTML = '
' + message + '
'; }); }; }, // Closes and dismisses the dialog DismissDialog: function() { document.getElementsByTagName('body')[0].removeChild(document.getElementById('stackphp_dialog')); document.getElementsByTagName('body')[0].removeChild(document.getElementById('stackphp_canvas')); }, // Selects a user with the given ID FindUserSelect: function(user_id, element_id) { // Hide everything StackPHP.DismissDialog(); document.getElementById(element_id).value = user_id; }, // Begins the implicit authentication flow BeginImplicitFlow: function(redirect_uri, scope, success_callback, error_callback) { // Generate the storage location of the callbacks so that we // can pass it as the state parameter. var callback = 'callback_' + StackPHP.GenerateUniqueNumber(); // Create the new window var window_url = 'https://stackexchange.com/oauth/dialog?client_id=' + StackPHP.ClientID + '&scope=' + scope + '&redirect_uri=' + encodeURIComponent(redirect_uri) + '&state=' + callback; window.open(window_url, 'auth_window', 'width=640,height=400,menubar=no,toolbar=no,location=no,status=no'); // Store the callbacks under a unique name window[callback] = { success: success_callback, error: error_callback }; }, // Completes the implicit authentication flow (this is called from the // context of the opened window) CompleteImplicitFlow: function(hash) { // Trim the '#' and split against '&' if(hash.indexOf('#') === 0) hash = hash.substr(1); hash = hash.split('&'); // Convert to an array var hash_map = {}; for(var i=0;i