/* * ContextMenuSetup class by Wade Walker. * wadedwalker.com * wadedwalker@gmail.com * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, * copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following * conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ package com.wadedwalker.utilities { //---------------------------------------------------------------------------------------------------------\\ import flash.display.InteractiveObject; import flash.events.ContextMenuEvent; import flash.net.URLRequest; import flash.net.navigateToURL; import flash.ui.ContextMenu; import flash.ui.ContextMenuItem; /** * The ContextMenuSetup class allows you easily add copyright info or links to a website or email address * to the menu that appears whenever you right-click within the Flash Player. */ final public class ContextMenuSetup { //---------------------------------------------------------------------------------------------------------\\ static private const TRACE_STRING:String = "com.wadedwalker.utilities : "; static private const ERROR_INIT:String = ContextMenuSetup.TRACE_STRING + "Initialize first!"; static private const ERROR_SHUTDOWN:String = ContextMenuSetup.TRACE_STRING + "Nothing to shutdown!"; static public const TYPE_LINK:String = "link"; static public const TYPE_EMAIL:String = "email"; private var cMenu:ContextMenu; private var linkObjectList:Vector.; private var linkValueList:Vector.; private var emailObjectList:Vector.; private var emailValueList:Vector.; private var interactiveObject:InteractiveObject; //---------------------------------------------------------------------------------------------------------\\ final public function ContextMenuSetup() { // } //------------------------------------------------------------------------------------------------------------------\\ //------------------------------------------------ PUBLIC FUNCTIONS ------------------------------------------------\\ //------------------------------------------------------------------------------------------------------------------\\ //---------------------------------------------------------------------------------------------------------\\ /** * Initializes all needed objects in order to function properly. * * @param intObj The interactive object that you right-click on in order to bring up the custom menu. Can be MovieClip, TextField, anything that extends InteractiveObject. */ final public function initialize(intObj:InteractiveObject):void { linkObjectList = new Vector.(); linkValueList = new Vector.(); emailObjectList = new Vector.(); emailValueList = new Vector.(); cMenu = new ContextMenu(); cMenu.hideBuiltInItems(); interactiveObject = intObj; interactiveObject.contextMenu = cMenu; } //---------------------------------------------------------------------------------------------------------\\ /** * Add new menu items to the right-click menu. * * @param caption The string message to display. Example: My Portfolio * @param separatorBefore Boolean value that determines whether or not there will be a horizontal line separation above this item in the list of right-click menu items. * @param clickable Boolean value to determine if the item can be clicked or not. If false, it will be grayed out. * @param linkType String value that says if it is a website link or email link that would launch the users default email client such as Windwos Outlook or Apple Mail. * @param linkAddress String value that represents the website or email address that will be linked to when the user clicks this menu item. * * @throws Error If initialize() has not been called first. */ final public function addMenuItem( caption:String, separatorBefore:Boolean = false, clickable:Boolean = true, linkType:String = ContextMenuSetup.TYPE_LINK, linkAddress:String = "http://wadedwalker.com"):void { if (interactiveObject) { var contextMenuItem:ContextMenuItem = new ContextMenuItem(caption,separatorBefore,clickable,true); cMenu.customItems.push(contextMenuItem); if (linkType == ContextMenuSetup.TYPE_LINK) { linkObjectList.push(contextMenuItem); linkValueList.push(linkAddress); contextMenuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, _onLinkSelect) } else if (linkType == ContextMenuSetup.TYPE_EMAIL) { emailObjectList.push(contextMenuItem); emailValueList.push(linkAddress); contextMenuItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, _onEmailSelect) } } else { throw new Error(ContextMenuSetup.ERROR_INIT); } } //---------------------------------------------------------------------------------------------------------\\ /** * Removes all event listeners and sets all objects to a null value for garbage collection. * * @throws Error If initialize() has not been called first. */ final public function shutdown():void { if (interactiveObject) { while (linkObjectList.length > 0) { linkObjectList[0].removeEventListener(ContextMenuEvent.MENU_ITEM_SELECT, _onLinkSelect); linkObjectList[0] = null; linkObjectList.shift(); } while (emailObjectList.length > 0) { emailObjectList[0].removeEventListener(ContextMenuEvent.MENU_ITEM_SELECT, _onEmailSelect); emailObjectList[0] = null; emailObjectList.shift(); } linkObjectList = null; emailObjectList = null; cMenu = null; } else { throw new Error(ContextMenuSetup.ERROR_SHUTDOWN); } } //------------------------------------------------------------------------------------------------------------------\\ //----------------------------------------------- PRIVATE FUNCTIONS ------------------------------------------------\\ //------------------------------------------------------------------------------------------------------------------\\ //---------------------------------------------------------------------------------------------------------\\ private function _onLinkSelect(e:ContextMenuEvent) { navigateToURL(new URLRequest(linkValueList[linkObjectList.indexOf(e.currentTarget)]), "_blank"); } //---------------------------------------------------------------------------------------------------------\\ private function _onEmailSelect(e:ContextMenuEvent) { navigateToURL(new URLRequest("mailto:" + emailValueList[emailObjectList.indexOf(e.currentTarget)]), "_blank"); } } }