Context Menu

28 Jun 201713 minutes to read

The context-menu has list of items which helps to perform FileExplorer operations, and it appears based on the target such as file or folder.

Context menu items

The below table shows the context menu items corresponding to the location where it is opened:

Context menu location Context menu items Screenshot
While right click on treeview nodes (from navigation pane)

* New folder
* Upload
* Delete
* Rename
* Cut
* Copy
* Paste







While right click on File / Folder

* Open
* Download
* Upload
* Delete
* Rename
* Cut
* Copy
* Paste
* Get info







While right click on layout (content pane)

* Refresh
* Paste
* Sort By
* New folder
* Upload
* Get info







The below table explains the behavior of each context menu item:

Open

It opens the selected folder. When an image file selected it opens the preview of the image. For the remaining files this option becomes disabled.

Download

It downloads the selected file. When a file or number of files selected at that time only download option enabled.

If multiple files selected then it downloads all the files in a zip format.

Cut

It makes the copy of the selected files or folders into the clipboard. When the user paste the files in any location, the files are removed from the source location.

Copy

It makes the copy of the selected files or folders into the clipboard. When the user paste the files, the copy of the files only pasted in the target location.

Paste

It paste the files from the clipboard into the current selected folder. Note that when the files are copied into the clipboard at that time only it enabled.

Delete

It deletes the current selected file or folder. When you select any file or folder at that time only this option gets enabled.

If multiple files selected then it deletes all the selected items.

Rename

This is used to rename the current selected file or folder. When you select any file or folder at that time only this option gets enabled.

Even multiple files selected it renames the single file only.

New folder

It creates a new folder on the current directory.

While click on the NewFolder item a dialog appears to get the folder name. Based on the user input, a new folder create on the current directory.

Upload

It uploads a file or list of files into the current directory.

Get info

It displays the details of the current selected file or folder.

Sort By

It's used to sorting the files and folders from the current directory.The sorting can be done based on the columns available from grid,in both ascending and descending order.

Context menu Visibility

The presence of the context menu can be controlled by the showContextMenu property. This was enabled by default, and by disabling this property you can prevent our built-in context menu.

  • HTML
  • <div id="fileExplorer" ej-fileexplorer e-path="path" e-ajaxaction="ajaxaction" e-isresponsive="true" e-showcontextmenu="false" ></div>
  • JAVASCRIPT
  • angular.module('FileCtrl', ['ejangular']).controller('FileCtrl', function ($scope) {
            $scope.path = "http://js.syncfusion.com/demos/ejServices/Content/FileBrowser/",
            $scope.ajaxaction = http://js.syncfusion.com/demos/ejServices/api/FileExplorer/FileOperations
        });

    Enable / Disable the Context menu Item

    The context menu items can be enabled or disabled through the client side public methods. It enables or disables the item from all the context menu where it is present.

    For example, if you disable the “Upload” item, it disables in all places wherever it appears such as open the context menu on treeview, open on file/folder, and open on layout.

    These methods only accepts the context menu item name as the parameter.

  • JAVASCRIPT
  • var fileExpObj = $("#fileExplorer").data("ejFileExplorer");
    
                // this disables the New Folder item
    
                fileExpObj.disableMenuItem("New folder");
    
                // this disables the Download item
    
                fileExpObj.disableMenuItem("Download");

    Context Menu Customization

    You can customize the ContextMenu of FileExplorer control by using contextMenuSettings property.

    You can add your own context menu items and its action along with default context menu items of FileExplorer control. You can also remove the default context menu items in FileExplorer control.

    To add/remove/re-arrange context menu items, you need to use contextMenuSettings.items property and to bind required actions for newly added menu items and add sub menu items, use contextMenuSettings.customMenuFields property.

  • HTML
  • <div id="fileExplorer" ej-fileexplorer e-path="path" e-ajaxaction="ajaxaction" e-isresponsive="true" e-contextmenusettings="contextMenuSettings" e-layoutchange="onLayoutChange" menuOpen= "onMenuOpen"></div>
  • JAVASCRIPT
  • angular.module('FileCtrl', ['ejangular']).controller('FileCtrl', function ($scope) {
            $scope.path = "http://js.syncfusion.com/demos/ejServices/Content/FileBrowser/",
            $scope.ajaxaction = http://js.syncfusion.com/demos/ejServices/api/FileExplorer/FileOperations,
            $scope.contextMenuSettings: {
                //define the ContextMenu items
                items: {
                    // removed the "NewFolder" item from NavigationPane ContextMenu
                    navbar: ["Upload", "|", "Delete", "Rename", "|", "Cut", "Copy", "Paste", "|", "Getinfo"],
                    // added the custom ContextMenu item (View) to Current working directory ContextMenu
                    cwd: ["Refresh", "Paste", "|", "SortBy", "View", "|", "NewFolder", "Upload", "|", "Getinfo"],
                    // removed "Upload" item from Selected files/ folder's ContextMenu
                    files: ["Open", "Download", "|", "Delete", "Rename", "|", "Cut", "Copy", "Paste", "|",
                        "OpenFolderLocation", "Getinfo"]
                },
                //added the custom ContextMenu item's (View) functionality
                customMenuFields: [
                {
                    id: "View",
                    text: "View by",
                    spriteCssClass: "custom-grid",
                    //added sub menu items
                    child: [
                    {
                        id: "tile",
                        text: "Tile view",
                        //specify the action for this menu item
                        action: "onLayout"
                    },
                    {
                        id: "grid",
                        text: "Grid view",
                        //specify the action for this menu item
                        action: "onLayout"
                    },
                    {
                        id: "largeicons",
                        text: "Large icons view",
                        //specify the action for this menu item
                        action: "onLayout"
                    }, ]
                }, ]
            }
        });
        function onLayout(args) {
            //perform actions while click custom menu items
            var feObj = $('#fileExplorer').data("ejFileExplorer");
            feObj && feObj.option("layout", args.ID);
        }
        function onMenuOpen(args) {
            //customize the custom menu items
            if (args.contextMenu == "cwd") {
                $(".fe-context-menu").find(".e-fe-activeicon").removeClass("e-fe-activeicon");
                $(".fe-context-menu").find("." + this.model.layout).addClass("e-fe-activeicon");
            }
        }
        function onLayoutChange(args) {
            $(".fe-context-menu .View").removeClass("custom-grid custom-tile custom-largeicons");
            $(".fe-context-menu .View").addClass("custom-" + this.model.layout);
        }

    Icons of context menu items can be customized by overriding the default context menu item style. The following code example illustrates how to customize the icon of context menu items.

  • CSS
  • <style>
        .fe-context-menu .custom-grid:before {
            content: "\e7b9";
        }
    
        .fe-context-menu .custom-largeicons:before {
            content: "\e7bb";
        }
    
        .fe-context-menu .custom-tile:before {
            content: "\e7be";
        }
    </style>

    The following screenshot displays the customization of context menu in FileExplorer control.

    Context Menu Events

    You would be notified with events when you try to open the context menu items (menuBeforeOpen), after context menu items is opened (menuOpen) and when you click the menu items (menuClick). The following code example illustrates how to define those events.

  • HTML
  • <div id="fileExplorer" ej-fileexplorer e-path="path" e-ajaxaction="ajaxaction" e-isresponsive="true" e-menubeforeopen="menuBeforeOpen" e-menuopen="menuOpen" menuclick= "menuClick"></div>
  • JAVASCRIPT
  • angular.module('FileCtrl', ['ejangular']).controller('FileCtrl', function ($scope) {
            $scope.path = "http://js.syncfusion.com/demos/ejServices/Content/FileBrowser/",
            $scope.ajaxaction = http://js.syncfusion.com/demos/ejServices/api/FileExplorer/FileOperations
        });
        function menuBeforeOpen(args){
            //you add/remove the context menu items in run time
            //do your custom action here.
            args.dataSource.pop();
        },
        function menuOpen(args){
            //you can also idendify which context menu is opened by 
            if (args.contextMenu == "cwd") {
                //do your custom action here.
            }
        },
        function menuClick(args){
            switch (args.text) {
                case "largeicons":
                    //do your custom action here.
                    break;
            }
        }