Adding a Button – JScript and Mashup

In this example I’ll show two simple examples how to add a button with JScript to a M3 form and also how to add a Button in a Mashup. I’ll add a button and make it launch something. Let’s start with the JScript.

The following example will add a button and when you click it you’ll launch a m3 program.
This is a good starting point for launching another app or a web page or whatever and by using script to get data from the panel you can send that information in the link you are opening.

import System;
import System.Windows;
import System.Windows.Controls;
import Mango.UI.Services;
import MForms;

package MForms.JScript {
   class ButtonExample {

      var content, debug, button;
      var controller;

      public function Init(element: Object, args: Object, controller : Object, debug : Object) {
         this.controller = controller;
         this.debug = debug;
         content = controller.RenderEngine.Content;

         var button : Button = new Button();
         button.Content = "Button 1";
         button.Name = "button1";
         Grid.SetColumnSpan(button, 10);
         Grid.SetRowSpan(button, 3);
         Grid.SetColumn(button,50);
         Grid.SetRow(button,1);
         content.Children.Add(button);

         this.button = button;
         button.add_Click(OnClick);
         controller.add_Requested(OnRequested);
      }

      public function OnClick(sender: Object, e: RoutedEventArgs) {
         MessageBox.Show("Button 1 script");
         DashboardTaskService.Current.LaunchTask(new Uri("mforms://mms001"));
      }

      public function OnRequested(sender: Object, e: RequestEventArgs) {
         button.remove_Click(OnClick);
         controller.remove_Requested(OnRequested);
      }
   }
}

I had to add the namespace Mango.UI.Services which is a excellent namespace to have for all types of services. In this case I use DashboardTaskService to launch a Task. A task is something you can run. All applications in LSO uses an URI syntax. This way we can plug in any number of applications and one application is not just one form.

Also notice that I unregister the event handlers in the OnRequested method. This is extremly important. I don’t use the Unloaded method on for example the Button because objects might be pooled internaly by LSO.

Launching antoher application is done with DashboardTaskService.Current.LaunchTask(new Uri(“mforms://mms001”));

The button is placed in a grid on the form and here is what the example would look like in MMS001.

Add a Button to a Mashup

When you have a mashup you can boundle a complete set of different mashups and open a Mashup with data from another mashup.

Start by adding the Button. The configure the Event on the Button. Setting the property LinkUri will result in that link being opened.

<Button Content="Open MMS001" Width="100" Margin="8">
      <Button.CommandParameter>
        <mashup:Events>
          <mashup:Event SourceEventName="Click" LinkUri="mforms://mms001">
          </mashup:Event>
        </mashup:Events>
      </Button.CommandParameter>
</Button>

If I wanted to open another mashup file in the same solution I would enter the relative path to that file. For example LinkUri=”Customer.xaml”. In order to pass parameters you specify the TargetKey and pass in the values. In my example below I use a converter to parse out the passed value that will be visible if the Mashup was opened by clicking the “Open again” button.

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ui="clr-namespace:Mango.UI.Controls;assembly=Mango.UI" xmlns:mashup="clr-namespace:Mango.UI.Services.Mashup;assembly=Mango.UI">
  <Grid.Resources>
    <mashup:CurrentItemValue x:Key="CurrentItemValueConverter" />
  </Grid.Resources>

  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
  </Grid.RowDefinitions>

  <Button Content="Open again" Width="100" Margin="8">
        <Button.CommandParameter>
          <mashup:Events>
            <mashup:Event SourceEventName="Click" LinkUri="{Binding Path=Properties[RelativeUri]}">
              <mashup:Parameter TargetKey="Customer" Value="This is the passed value!" />
            </mashup:Event>
          </mashup:Events>
        </Button.CommandParameter>
  </Button>
  <TextBlock Grid.Row="1" Text="{Binding Converter={StaticResource CurrentItemValueConverter}, ConverterParameter=Customer, Mode=OneTime}" HorizontalAlignment="Center" />
</Grid>

The path to the file itself is found in a Properties map with the key RelativeUri. This is just for the example to work without having to share a Mashup project. This could just as well be Customer.xml and the Value for the Parameter Customer you can bind that to whatever information you have available.

Happy coding!

4 thoughts on “Adding a Button – JScript and Mashup

    1. karinpb Post author

      Yes.
      There is an example in the Designer under Help -> Common Controls -> Window.
      You basically use the URL to the mashup and add a DefaultValues parameter. All parameters are combined in string with the following format”Name1;Value1;Name2;Value2″ This means that values are not allowed to have semicolon and that the entire string needs to be URL encoded in the JScript.

      Mashup URI: mashup:///?BaseUri=Karin5.mashup&RelativeUri=ChangeCustomer.xaml
      The BaseUri us the name of the mashup project file and the RelativeUri is the relative path to the XAML file. This way you can open Mashups that are not visible in the Navigator. To add parameters add the DefaultValues parameter. Then if you have a start up event you can pick the default values from any passed parameters.

      mashup:///?BaseUri=Karin5.mashup&RelativeUri=ChangeCustomer.xaml&DefaultValues=InputText:Hej;InputCheckBox:True;

      Reply
    1. karinpb Post author

      Do you mean a button in a Mashup or with javascript in a H5 script?

      For Mashup look here: https://smartofficeblog.com/2013/09/23/mashup-enable-a-button-based-on-status-and-currentitemchanged/

      There is also a OptionButton that you can use – I have not used it myself but it was in the SDK documentation from the designer.

      Or do you mean JScript in Smart Office in MForms? Then you use the IInstanceController -> controller.ListOption(“F17”).

      If you mean in a javascript in the H5 web client then you also use IInstanceController -> controller.ListOption(“F17”).

      So please be more specific when you ask questions – you can never give too much information – but often too little.

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s