<commit_before>/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. Camunda licenses this file to you under the Apache License,
 * Version 2.0; you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

var angular = require('camunda-commons-ui/vendor/angular');

module.exports = [
  '$rootScope',
  '$scope',
  '$translate',
  '$timeout',
  'debounce',
  'Notifications',
  'processData',
  'assignNotification',
  '$location',
  'search',
  function(
    $rootScope,
    $scope,
    $translate,
    $timeout,
    debounce,
    Notifications,
    processData,
    assignNotification,
    $location,
    search
  ) {
    function errorNotification(src, err) {
      $translate(src)
        .then(function(translated) {
          Notifications.addError({
            status: translated,
            message: err ? err.message : '',
            scope: $scope
          });
        })
        .catch(angular.noop);
    }

    function successNotification(src) {
      $translate(src)
        .then(function(translated) {
          Notifications.addMessage({
            duration: 3000,
            status: translated
          });
        })
        .catch(angular.noop);
    }

    // setup ////////////////////////////////////////////////////////////////////////

    var processStartData = processData.newChild($scope);

    // initially always reset the current selected process definition id to null
    processStartData.set('currentProcessDefinitionId', {id: null});

    var DEFAULT_OPTIONS = ($scope.options = {
      hideCompleteButton: true,
      hideLoadVariablesButton: true,
      autoFocus: true,
      disableForm: false,
      disableAddVariableButton: false
    });

    $scope.PROCESS_TO_START_SELECTED = false;

    var query = null;

    var page = ($scope.page = {
      total: 0,
      current: 1,
      searchValue: null
    });

    $scope.triggerOnStart = function() {};

    // observe /////////////////////////////////////////////////////////////////////////////////////

    processStartData.observe('processDefinitionQuery', function(_query) {
      query = angular.copy(_query);

      page.size = _query.maxResults;
      page.current = _query.firstResult / page.size + 1;
    });

    $scope.startFormState = processStartData.observe('startForm', function(
      startForm
    ) {
      $scope.startForm = angular.copy(startForm);
    });

    $scope.processDefinitionState = processStartData.observe(
      'processDefinitions',
      function(processDefinitions) {
        page.total = processDefinitions.count;

        $scope.processDefinitions = processDefinitions.items.sort(function(
          a,
          b
        ) {
          // order by process definition name / key and secondary by tenant id
          var aName = (a.name || a.key).toLowerCase();
          var bName = (b.name || b.key).toLowerCase();

          var aTenantId = a.tenantId ? a.tenantId.toLowerCase() : '';
          var bTenantId = b.tenantId ? b.tenantId.toLowerCase() : '';

          if (aName < bName) return -1;
          else if (aName > bName) return 1;
          else if (aTenantId < bTenantId) return -1;
          else if (aTenantId > bTenantId) return 1;
          else return 0;
        });

        if (page.total > 0) {
          $timeout(function() {
            var element = document.querySelectorAll(
              'div.modal-content ul.processes a'
            )[0];
            if (element) {
              element.focus();
            }
          });
        }
      }
    );

    // select process definition view //////////////////////////////////////////////////////

    $scope.pageChange = function() {
      query.firstResult = page.size * (page.current - 1);
      processStartData.set('processDefinitionQuery', query);
    };

    $scope.lookupProcessDefinitionByName = debounce(function() {
      var nameLike = page.searchValue;

      if (!nameLike) {
        delete query.nameLike;
      } else {
        query.nameLike = '%' + nameLike + '%';
      }

      // reset first result of query
      query.firstResult = 0;

      processStartData.set('processDefinitionQuery', query);
    }, 2000);

    $scope.selectProcessDefinition = function(processDefinition) {
      $scope.PROCESS_TO_START_SELECTED = true;

      var processDefinitionId = processDefinition.id;
      var processDefinitionKey = processDefinition.key;
      var deploymentId = processDefinition.deploymentId;

      $scope.options = angular.copy(DEFAULT_OPTIONS);

      $scope.params = {
        processDefinitionId: processDefinitionId,
        processDefinitionKey: processDefinitionKey,
        deploymentId: deploymentId
      };

      search.updateSilently({processStart: processDefinitionId});

      processStartData.set('currentProcessDefinitionId', {
        id: processDefinitionId
      });
    };

    var processToStart = $location.search()['processStart'];
    if (processToStart && typeof processToStart === 'string') {
      processStartData.set('currentProcessDefinitionId', {
        id: $location.search()['processStart']
      });
      $scope.params = {
        processDefinitionId: processToStart
      };
      $scope.PROCESS_TO_START_SELECTED = true;
    }

    // start a process view /////////////////////////////////////////////////////////////////

    $scope.$invalid = true;
    $scope.requestInProgress = false;

    $scope.$on('embedded.form.rendered', function() {
      $timeout(function() {
        var focusElement = document.querySelectorAll(
          '.modal-body .form-container input'<commit_msg>feat(start-form): use processKey for deep links<commit_after>/*
 * Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
 * under one or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership. Camunda licenses this file to you under the Apache License,
 * Version 2.0; you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

'use strict';

var angular = require('camunda-commons-ui/vendor/angular');

module.exports = [
  '$rootScope',
  '$scope',
  '$translate',
  '$timeout',
  'debounce',
  'Notifications',
  'processData',
  'assignNotification',
  '$location',
  'search',
  'camAPI',
  function(
    $rootScope,
    $scope,
    $translate,
    $timeout,
    debounce,
    Notifications,
    processData,
    assignNotification,
    $location,
    search,
    camAPI
  ) {
    function errorNotification(src, err) {
      $translate(src)
        .then(function(translated) {
          Notifications.addError({
            status: translated,
            message: err ? err.message : '',
            scope: $scope
          });
        })
        .catch(angular.noop);
    }

    function successNotification(src) {
      $translate(src)
        .then(function(translated) {
          Notifications.addMessage({
            duration: 3000,
            status: translated
          });
        })
        .catch(angular.noop);
    }

    // setup ////////////////////////////////////////////////////////////////////////

    var processStartData = processData.newChild($scope);

    // initially always reset the current selected process definition id to null
    processStartData.set('currentProcessDefinitionId', {id: null});

    var DEFAULT_OPTIONS = ($scope.options = {
      hideCompleteButton: true,
      hideLoadVariablesButton: true,
      autoFocus: true,
      disableForm: false,
      disableAddVariableButton: false
    });

    $scope.PROCESS_TO_START_SELECTED = false;

    var query = null;

    var page = ($scope.page = {
      total: 0,
      current: 1,
      searchValue: null
    });

    $scope.triggerOnStart = function() {};

    // observe /////////////////////////////////////////////////////////////////////////////////////

    processStartData.observe('processDefinitionQuery', function(_query) {
      query = angular.copy(_query);

      page.size = _query.maxResults;
      page.current = _query.firstResult / page.size + 1;
    });

    $scope.startFormState = processStartData.observe('startForm', function(
      startForm
    ) {
      $scope.startForm = angular.copy(startForm);
    });

    $scope.processDefinitionState = processStartData.observe(
      'processDefinitions',
      function(processDefinitions) {
        page.total = processDefinitions.count;

        $scope.processDefinitions = processDefinitions.items.sort(function(
          a,
          b
        ) {
          // order by process definition name / key and secondary by tenant id
          var aName = (a.name || a.key).toLowerCase();
          var bName = (b.name || b.key).toLowerCase();

          var aTenantId = a.tenantId ? a.tenantId.toLowerCase() : '';
          var bTenantId = b.tenantId ? b.tenantId.toLowerCase() : '';

          if (aName < bName) return -1;
          else if (aName > bName) return 1;
          else if (aTenantId < bTenantId) return -1;
          else if (aTenantId > bTenantId) return 1;
          else return 0;
        });

        if (page.total > 0) {
          $timeout(function() {
            var element = document.querySelectorAll(
              'div.modal-content ul.processes a'
            )[0];
            if (element) {
              element.focus();
            }
          });
        }
      }
    );

    // select process definition view //////////////////////////////////////////////////////

    $scope.pageChange = function() {
      query.firstResult = page.size * (page.current - 1);
      processStartData.set('processDefinitionQuery', query);
    };

    $scope.lookupProcessDefinitionByName = debounce(function() {
      var nameLike = page.searchValue;

      if (!nameLike) {
        delete query.nameLike;
      } else {
        query.nameLike = '%' + nameLike + '%';
      }

      // reset first result of query
      query.firstResult = 0;

      processStartData.set('processDefinitionQuery', query);
    }, 2000);

    $scope.selectProcessDefinition = function(processDefinition) {
      $scope.PROCESS_TO_START_SELECTED = true;

      var processDefinitionId = processDefinition.id;
      var processDefinitionKey = processDefinition.key;
      var deploymentId = processDefinition.deploymentId;

      $scope.options = angular.copy(DEFAULT_OPTIONS);

      $scope.params = {
        processDefinitionId: processDefinitionId,
        processDefinitionKey: processDefinitionKey,
        deploymentId: deploymentId
      };

      search.updateSilently({processStart: processDefinitionKey});

      processStartData.set('currentProcessDefinitionId', {
        id: processDefinitionId
      });
    };

    var processToStart = $location.search()['processStart'];
    if (processToStart && typeof processToStart === 'string') {
      camAPI.resource('process-definition').list(
        {
          key: processToStart,
          latest: true,
          active: true,
          startableInTasklist: true,
          startablePermissionCheck: true,
          maxResults: 1
        },
        function(err, res) {
          if (err || res.items.length === 0) {
            return err;
          }

          $scope.selectProcessDefinition(res.items[0]);
        }
      );
    }

    // start a process view /////////////////////////////////////////////////////////////////

    $scope.$invalid = true;
    $scope.requestInProgress = false;

    $scope.$on('embedded.form.rendered', function() {
      $timeout(function() {
        var focusElement = document.querySelectorAll(
          '.modal-body .form-container input'
{'fraction_non_alphanumeric': 0.14699367088607596, 'fraction_numerical': 0.004034810126582278, 'mean_word_length': 2.0952497551420177, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7156070', 'n_tokens_mistral': 3350, 'n_tokens_neox': 3240, 'n_words': 818}