[XYS] Toggle Columns

Discuss and share scripts and script files...
Post Reply
TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

[XYS] Toggle Columns

Post by TheQwerty »

This script helps you toggle one or more columns' visibility.
ToggleColumns.xys
It is meant to be used as a script file and called by other scripts.
If run by itself it will walk the user through creating an appropriate calling script.


Latest Release: v1.1 [2014-03-20 20:00Z].
Minimum XYplorer: v13.80.0112+.

Change Log:
v1.1 [2014-03-20 20:00Z]
  - Enabled dragging for XY v13.80.0126


Code: Select all

/*##############################################################################
##  ToggleColumns.xys
##
##  Script to make it easy to toggle one or more columns in a specific position.
##
##    Author: TheQwerty
##   Version: 1.1
##      Date: 2014-03-20 20:00Z
##  Requires: XY v13.80.0112 or newer.
##
##    Thread: http://www.xyplorer.com/xyfc/viewtopic.php?p=101640#p101640
##############################################################################*/

"_Initialize"
  /// --------------------------------------------------------------------------
  /// Do some initializing...
  /// --------------------------------------------------------------------------

  Global $G_INIT;
  if (! $G_INIT) {
    // Ensure compatibility.
    $minVer = '13.80.0112';
    End "<xyver>" < $minVer, "This script requires XYplorer v$minVer or newer.", 1;
    $G_INIT = True;
  }
  ////////////////////////////////////////////////////////////// END _Initialize

"&Toggle Columns||5 : toggleColumns"
  /// --------------------------------------------------------------------------
  /// Toggles the visibility of the specified columns.
  ///
  /// If some of the specified columns are visible or they are all visible but
  /// not as a sequence in the specified ordered then they will be shown.
  ///
  /// INPUTS:
  ///   $G_COLUMN    A comma-separated list of canonic column names.
  ///                This script does not support visibility prefixes or widths.
  ///
  ///   $G_POSITION  Starting position for the specified columns.
  ///                Position 0 is right-most.
  ///                Position 1 is nearly left-most.
  ///                Negative positions start from right end.
  ///                When omitted or not a number '0' is used.
  /// --------------------------------------------------------------------------

  Sub '_Initialize';

  // IN: G_COLUMN
  //  Comma-separated list of canonic column names to toggle.
  Global $G_COLUMN;

  // IN: G_POSITION
  //  Starting position to display G_COLUMN.
  Global $G_POSITION;

  // Sanitize inputs
  $sep = ',';
  $column = FormatList($G_COLUMN, 'ted', $sep);
  $position = $G_POSITION * 1;

  // If no column is specified show full menu.
  if ($column == '') {
    Load '*', '_createCallingScript;_showExamples';
    End True;
  }

  // Get Current Columns.
  $cols = SetColumns();

  // Check for desired columns.
  $found = -1 != StrPos(",$cols,", ",$column,");

  // Remove desired columns.
  // This is done even when hiding to ensure they are kept in sequence.
  $newCols = FormatList($cols, 'fed', $sep, "!$column");

  // Add columns if the sequence was not found.
  if (! $found) {
    $newCols = GetToken($newCols, $position - 1, $sep,, 1) . ",$column," . GetToken($newCols, $position, $sep,, 2);
  }

  // Update Columns if changed.
  $newCols = FormatList($newCols, 'ted', $sep);
  if ($cols != $newCols) {
    SetColumns($newCols);
  }
  Unset $sep, $column, $position, $cols, $found, $newCols, $minVer;
  //////////////////////////////////////////////////////////// END toggleColumns


"- : _-" //---------------------------------------------------------------------


"&Create Calling Script : _createCallingScript"
  /// --------------------------------------------------------------------------
  /// Walks the user through creating a script which uses toggleColumns.
  /// --------------------------------------------------------------------------

  Sub '_Initialize';

  // Ensure the script is saved as a file.
  $file = Self('file');
  End $file == '', 'This script must be run as a script file to create a calling script.', 1;
  $file = ResolvePath($file, "<xyscripts>", 1);

  // Get list of all available columns.
  $columns = SetColumns(, 1);
  $sep = ',';

  // Prompt user to select columns.

  // Enable drag and drop re-ordering if supported.
  $style = 2;
  if ("<xyver>" >= '13.80.0126') {
    $style = $style + 16;
    $dragMsg = "<crlf>You may also drag them to a new position.";
  } else {
    $dragMsg = '';
  }

  $topic = <<<#MSG
Toggle Columns
Tick the columns to toggle with this script.$dragMsg
Press cancel or select nothing to input a custom string instead.#MSG;
  $columns = InputSelect($topic, $columns, $sep, $style, '', 400, 650);
  $columns = FormatList($columns, 'ted', $sep);

  // Prompt user for string of column names instead.
  if ($columns == '') {
    $topic = 'Toggle Columns';
    $notes = 'Enter a comma-separated list of column names.';
    while ($columns == '') {
      $columns = Input($topic, $notes,, 's');
      $columns = FormatList($columns, 'ted', $sep);
    }
  }

  // Prompt user for column position.
  $topic = 'Column Position';
  $notes = <<<#NOTES
Enter a position for these columns.
Position 1 is left-most.
Position 0 is right-most.
Negative positions start from the right end.#NOTES;
  $position = '';
  while ('' == RegexMatches($position, '^-?[0-9]+$')) {
    $position = Input($topic, $notes, 0, 's');
  }

  // Display created script.
  $plural = GetToken($columns, 'Count', $sep) == 1 ? '' : 's';
  Text <<<#SCRIPT
"Toggle Column$plural: $columns"
  Global $G_COLUMN = "$columns";
  Global $G_POSITION = $position;
  Load '$file';
#SCRIPT,, 'Your Script...';

  // Clean up.
  Unset $file, $columns, $sep, $topic, $notes, $position, $plural;
  //////////////////////////////////////////////////////END _createCallingScript


"&Show Usage Examples : _showExamples"
  /// --------------------------------------------------------------------------
  /// Displays some examples of how to call toggleColumns.
  /// --------------------------------------------------------------------------

  // Use subs if the user hasn't saved the script to a file.
  $scriptCall = Self('file');
  $scriptCall = ResolvePath($scriptCall, "<xyscripts>", 1);
  if ($scriptCall == '') {
    $scriptCall = "Sub 'toggleColumns';";
  } else {
    $scriptCall = "Load '$scriptCall';";
  }

  Text <<<#SCRIPT
"Toggle Label Column at Far Right"
  Global $G_COLUMN = 'Label';
  $scriptCall

"Toggle Date Columns at Far Left"
  Global $G_COLUMN = 'Created,Modified,Accessed';
  Global $G_POSITION = 1;
  $scriptCall

"Toggle Custom Columns at Position 3"
  Global $G_COLUMN = 'Custom 1,Custom 2,Custom 3,Custom 4,Custom 5';
  Global $G_POSITION = 3;
  $scriptCall
#SCRIPT;
  Unset $scriptCall;
  /////////////////////////////////////////////////////////////END _showExamples

[/size]
To see the attached files, you need to log into the forum.

LittleBiG
Posts: 1848
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: [XYS] Toggle Columns

Post by LittleBiG »

:appl: Thanks for the great script, I started to use it immediately. Special thanks for the lot of comments!

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: [XYS] Toggle Columns

Post by TheQwerty »

Update: v1.1 [2014-03-20 20:00Z]
- Enabled dragging for XY v13.80.0126

New script in the first post.

LittleBiG
Posts: 1848
Joined: 08 Apr 2011 12:57
Location: Win10x64

Re: [XYS] Toggle Columns

Post by LittleBiG »

TheQwerty, I am using your script but I noticed an unfortunate side effect. When I toggle columns, the order of the columns is changed: visible columns are at the beginning, unvisible ones are at the end. But in this way Path column will be moved to the end (because it is normally not visible). When I click on the Branch View button and Path column appears, it should be the very first column but it is the last instead.

Post Reply