{"id":2798,"date":"2022-10-07T09:21:27","date_gmt":"2022-10-07T09:21:27","guid":{"rendered":"https:\/\/mindfusion.eu\/blog\/?p=2798"},"modified":"2022-10-07T10:11:03","modified_gmt":"2022-10-07T10:11:03","slug":"using-mindfusion-toolstrip-library-in-javascript","status":"publish","type":"post","link":"https:\/\/mindfusion.dev\/blog\/using-mindfusion-toolstrip-library-in-javascript\/","title":{"rendered":"Using MindFusion ToolStrip Library in JavaScript"},"content":{"rendered":"<p>In this blog post we are going to look at the way you can build the ToolStrip that you see at the image below. We use the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStrip_Members.htm\" title=\"MindFusion Pack for JavaScript: ToolStrip\">ToolStrip<\/a> control, which is part of MindFusion Pack for JavaScript. Here is the final result:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/mindfusion.dev\/samples\/javascript\/ui\/toolstrip_trains.jpg\" title=\"ToolStrip in JavaScript\"><\/p>\n<p>We will go through the process of creating the toolstrip step-by-step, from scratch, clarifying the most important details.<\/p>\n<p>You can run the sample online at <a href=\"https:\/\/mindfusion.dev\/samples\/javascript\/grid\/TrainSchedule.html\" title=\"MindFusion ToolStrip Control in TrainSchedule for JS\">https:\/\/mindfusion.dev\/samples\/javascript\/grid\/TrainSchedule.html<\/a><\/p>\n<p><!--more--><\/p>\n<p><strong>I. General Setup<\/strong><br \/>\nThe <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStrip.htm\" title=\"MindFusion Pack for JavaScript: ToolStrip\">ToolStrip<\/a> class is part of the UI controls that ship with <a href=\"https:\/\/mindfusion.dev\/javascript-pack.html\" title=\"MindFusion Pack for JavaScript\">MindFusion Pack for JavaScript<\/a>. The UI controls are found in the common-ui.js file and we need to add a reference to it. We also need to reference a few other JavaScript libraries:<\/p>\n<pre>&lt;script src=\"..\/scripts\/drawing.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;\n&lt;script src=\"..\/scripts\/controls.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;\n&lt;script src=\"..\/scripts\/common.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;\n&lt;script src=\"..\/scripts\/common-collections.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;\n&lt;script src=\"..\/scripts\/common-ui.js\" type=\"text\/javascript\"&gt;&lt;\/script&gt;\n<\/pre>\n<p>We need a DIV element for the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStrip_Members.htm\" title=\"MindFusion Pack for JavaScript: ToolStrip\">ToolStrip<\/a> control to render itself onto. The location and the size of the DIV specify the location and size of the toolstrip. It is important that we provide the DIV with an id, because we will reference it from code.<\/p>\n<pre>&lt;div style=\"position: absolute; top: 100px; width: 98%; bottom: 20px;\"&gt;\n\t&lt;div id=\"toolstrip\" style=\"font-size:large;\"&gt;\n&lt;\/div&gt;\n&lt;\/div&gt;\n<\/pre>\n<p>With that the setup in the HTML part of our application is done. We are ready to write some JavaScrit code that will create and customize the toolstrip.<\/p>\n<p><strong>II. The ToolStrip: Initialization and Settings<\/strong><\/p>\n<p>The constructor of the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStrip.htm\" title=\"MindFusion Pack for JavaScript: ToolStrip\">ToolStrip<\/a> class requires the DOM element of the DIV that represents the toolstrip. So, we get it thanks to the id that we assigned to it and create the ToolStrip object. By default a toolstrip can be scrolled and dragged. We want to prevent this and set <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?P_MindFusion_Common_UI_ToolStrip_scrollable_0.htm\" title=\"MindFusion Pack for JavaScript: scrollable\">scrollable<\/a> and <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?P_MindFusion_Common_UI_ListContainer_allowDrag_0.htm\" title=\"MindFusion Pack for JavaScript: allowDrag\">allowDrag<\/a> to false.<\/p>\n<pre>var strip = new MindFusion.Common.UI.ToolStrip(document.getElementById(\"toolstrip\"));\nstrip.scrollable = false;\nstrip.allowDrag = false;\n<\/pre>\n<p>The menus and content in general of at ToolStrip is set with instances of the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStripItem_Members.htm\" title=\"MindFusion Pack for JavaScript: ToolStripItem\">ToolStripItem<\/a> class. Items can be empty and their content is set with the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?P_MindFusion_Common_UI_Tooltip_template_0.htm\" title=\"MindFusion Pack for JavaScript: template\">template<\/a> property. Then can also be icons, labels or separators. The type is set with the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStripItemType.htm\" title=\"MindFusion Pack for JavaScript: ToolStripItemType\">ToolStripItemType<\/a> enumeration in the constructor. This is how you create a <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStripItem_Members.htm\" title=\"MindFusion Pack for JavaScript: ToolStripItem\">ToolStripItem<\/a> of type label:<\/p>\n<pre>strip.items.add(new MindFusion.Common.UI.ToolStripItem(MindFusion.Common.UI.ToolStripItemType.Label, \"hour:\"));\n<\/pre>\n<p>The template of a <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStripItem_Members.htm\" title=\"MindFusion Pack for JavaScript: ToolStripItem\">ToolStripItem<\/a> is set as HTML code. With the template below we set an input as the content of the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStripItem_Members.htm\" title=\"MindFusion Pack for JavaScript: ToolStripItem\">ToolStripItem<\/a>:<\/p>\n<pre>item = new MindFusion.Common.UI.ToolStripItem(MindFusion.Common.UI.ToolStripItemType.Default);\nitem.template = \"&lt;input type=\"number\" id=\"hour_input\" min=\"0\" value=\"0\"&gt; 0-23\";\nstrip.items.add(item);\n<\/pre>\n<p>When you create the HTML template you can use any HTML tags, styles and attributes that you would normally use when you write pure HTML.<\/p>\n<p>The separator item renders as a line. It has no special properties and is styled according to the theme:<\/p>\n<pre>strip.items.add(new MindFusion.Common.UI.ToolStripItem(ui.ToolStripItemType.Separator));\n<\/pre>\n<p>The icon ToolStripItem represents clickable images in the toolstrip. Here we use one of the numerous properties of <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStripItem.htm\" title=\"MindFusion Pack for JavaScript: ToolStripItem\">ToolStripItem<\/a> <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?P_MindFusion_Common_UI_ListItem_size_0.htm\" title=\"MindFusion Pack for JavaScript: size\">size<\/a><\/p>\n<pre>item = new MindFusion.Common.UI.ToolStripItem(MindFusion.Common.UI.ToolStripItemType.Icon);\nitem.imageSrc = \"..\/images\/next_train.png\";\nitem.size = MindFusion.Common.Unit.pixel(36);\nstrip.items.add(item);\n<\/pre>\n<p>It is worth browsing through the members of the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStripItem_Members.htm\" title=\"MindFusion Pack for JavaScript: ToolStripItem\">ToolStripItem<\/a> class &#8211; you can find many useful properties that allow you to customize the looks and feels of your toolstrip.<\/p>\n<p>It is important to remember to call the <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?M_MindFusion_Common_Control_render_0.htm\" title=\"MindFusion Pack for JavaScript: render\">render<\/a> method after you&#8217;ve finished initialization of the toolstrip:<\/p>\n<pre>strip.render();\n<\/pre>\n<p>We have demonstrated how you can create items for your toolstrip from all item types. Now it is time to deal with interactivity: how to handle events for the items.<\/p>\n<p><strong>III. Handling ToolStripItem Events<\/strong><\/p>\n<p>The events for toolstrip items are raised by the container: the ToolStrip control. For handling clicks on the items, we use <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?E_MindFusion_Common_UI_ListContainer_itemClick.htm\" title=\"MindFusion Pack for JavaScript: itemClick\">itemClick<\/a> The ToolStrip control is of type <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ListContainer_Members.htm\" title=\"MindFusion Pack for JavaScript: ListContainer\">ListContainer<\/a> and boasts many events: <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?E_MindFusion_Common_UI_ListContainer_itemDrag.htm\" title=\"MindFusion Pack for JavaScript: itemDrag,\">itemDrag,<\/a> <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?E_MindFusion_Common_UI_ListContainer_itemMouseEnter.htm\" title=\"MindFusion Pack for JavaScript: itemMouseEnter,\">itemMouseEnter,<\/a> <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?E_MindFusion_Scheduling_Calendar_selectionChanged.htm\" title=\"MindFusion Pack for JavaScript: selectionChanged\">selectionChanged<\/a> to name a few. It is worth going through them. This is how to handle clicks on a <a href=\"https:\/\/www.mindfusion.dev\/onlinehelp\/javascript.pack\/index.htm?T_MindFusion_Common_UI_ToolStripItem_Members.htm\" title=\"MindFusion Pack for JavaScript: ToolStripItem\">ToolStripItem<\/a>:<\/p>\n<pre>strip.itemClick.addEventListener(function(sender, args)\n{\n    var index = sender.items.indexOfItem(args.item);\n\n\n    switch (index)\n    {\n       case 0:\n         handleClickOnItem0();\n         break;\n       case 1:\n         handleClickOnItem1();\n         break;\n      default:\n        return;\n     }\n});\n<\/pre>\n<p>The ToolStrip control used in various samples that ship with <a href=\"https:\/\/mindfusion.dev\/javascript-pack.html\" title=\"MindFusion Pack for JavaScript\">MindFusion JavaScript Pack<\/a>. The code for this sample is taken from the &#8220;Train Schedule&#8221; demo, which can be downloaded from the following link:<\/p>\n<p align=\"center\"><a href=\"https:\/\/mindfusion.dev\/samples\/javascript\/grid\/TrainSchedule.zip\">Download the Train Schedule Sample<\/a><\/p>\n<p>Technical support is available through <a href=\"https:\/\/mindfusion.dev\/Forum\/YaBB.pl\" title=\"MindFusion Technical Support Forums\">MindFusion forums<\/a> or the <a href=\"https:\/\/mindfusion.dev\/HelpDesk\/index.php\" title=\"MindFusion Help Desk\">HelpDesk<\/a>. You can also write at support@mindfusion.dev<\/p>\n<p><i>About MindFusion Pack for JavaScript:<\/i> A rich set of fully-interactive and customizable libraries written in pure JavaScript. The pack includes heavy-weight controls like flowchart, scheduling and charts, as well as versatile UI controls like ToolStrip, a Window, ImagePicker and more. Each library features numerous properties, which you can use to make it fit best into your application. The pack features various samples that demonstrate how the libraries are used and provides plenty of sample code to study and implement in your own projects. Appearance is theme-based, the pack boasts a set of more than 10 CSS themes, which are also editable. MindFusion first-class customer support is available for all licensees and evaluators free of charge. Each license is permanent and included 12-month upgrades. Find out more at <a href=\"https:\/\/mindfusion.dev\/buy-javascript-pack.html\" title=\"JavaScript Pack: Licenses\"> the JavaScript Pack page<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post we are going to look at the way you can build the ToolStrip that you see at the image below. We use the ToolStrip control, which is part of MindFusion Pack for JavaScript. Here is the &hellip; <a href=\"https:\/\/mindfusion.dev\/blog\/using-mindfusion-toolstrip-library-in-javascript\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[513,74,343],"tags":[80,624,612,706,48],"class_list":["post-2798","post","type-post","status-publish","format-standard","hentry","category-javascript","category-sample-code","category-ui","tag-javascript","tag-javascript-library","tag-js-tutorial","tag-toolstrip","tag-ui"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3RlKs-J8","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2798","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/comments?post=2798"}],"version-history":[{"count":9,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2798\/revisions"}],"predecessor-version":[{"id":2807,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/posts\/2798\/revisions\/2807"}],"wp:attachment":[{"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/media?parent=2798"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/categories?post=2798"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mindfusion.dev\/blog\/wp-json\/wp\/v2\/tags?post=2798"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}