Browse Source

Removed macro event for TicketCreateFormHandler. This is not needed, because there are no macros when creating tickets.

Denny Korsukéwitz 5 years ago
parent
commit
feac4bb938

+ 30 - 5
app/assets/javascripts/app/controllers/ticket_overview.coffee

@@ -87,9 +87,9 @@ class App.TicketOverview extends App.Controller
 
   startDragItem: (event) =>
     return if !@batchSupport
-    @grabbedItem = $(event.currentTarget)
-    offset = @grabbedItem.offset()
-    @batchDragger = $(App.view('ticket_overview/batch_dragger')())
+    @grabbedItem      = $(event.currentTarget)
+    offset            = @grabbedItem.offset()
+    @batchDragger     = $(App.view('ticket_overview/batch_dragger')())
     @grabbedItemClone = @grabbedItem.clone()
     @grabbedItemClone.data('offset', @grabbedItem.offset())
     @grabbedItemClone.addClass('batch-dragger-item js-main-item')
@@ -642,10 +642,35 @@ class App.TicketOverview extends App.Controller
     ))
 
   renderOptionsMacros: =>
-    macros = App.Macro.search(filter: { active: true }, sortBy:'name', order:'DESC')
+
+    @possibleMacros = []
+    macros          = App.Macro.search(filter: { active: true }, sortBy:'name', order:'DESC')
+
+    items = @el.find('[name="bulk"]:checked')
+
+    group_ids =[]
+    for item in items
+      ticket = App.Ticket.find($(item).val())
+      group_ids.push ticket.group_id
+
+    group_ids = _.uniq(group_ids)
+
+    for macro in macros
+
+      # push if no group_ids exists
+      if _.isEmpty(macro.group_ids) && !_.includes(@possibleMacros, macro)
+        @possibleMacros.push macro
+
+      # push if group_ids are equal
+      if _.isEqual(macro.group_ids, group_ids) && !_.includes(@possibleMacros, macro)
+        @possibleMacros.push macro
+
+      # push if all group_ids of tickets are in macro.group_ids
+      if !_.isEmpty(macro.group_ids) && _.isEmpty(_.difference(group_ids,macro.group_ids)) && !_.includes(@possibleMacros, macro)
+        @possibleMacros.push macro
 
     @batchMacro.html $(App.view('ticket_overview/batch_overlay_macro')(
-      macros: macros
+      macros: @possibleMacros
     ))
 
   active: (state) =>

+ 20 - 5
app/assets/javascripts/app/controllers/ticket_zoom/attribute_bar.coffee

@@ -1,7 +1,7 @@
 class App.TicketZoomAttributeBar extends App.Controller
   elements:
     '.js-submitDropdown': 'buttonDropdown'
-    '.js-reset': 'resetButton'
+    '.js-reset':          'resetButton'
 
   events:
     'mousedown .js-openDropdownMacro':    'toggleMacroMenu'
@@ -11,6 +11,7 @@ class App.TicketZoomAttributeBar extends App.Controller
     'mouseleave .js-dropdownActionMacro': 'onActionMacroMouseLeave'
     'click .js-secondaryAction':          'chooseSecondaryAction'
 
+  searchCondition: {}
   constructor: ->
     super
 
@@ -24,6 +25,12 @@ class App.TicketZoomAttributeBar extends App.Controller
       @render()
     )
 
+    @bind('MacroPreconditionUpdate', (data) =>
+      return if data.taskKey isnt @taskKey
+      @searchCondition = data.params
+      @render()
+    )
+
   release: =>
     App.Macro.unsubscribe(@subscribeId)
 
@@ -34,16 +41,24 @@ class App.TicketZoomAttributeBar extends App.Controller
     if @resetButton.get(0) && !@resetButton.hasClass('hide')
       resetButtonShown = true
 
-    macros = App.Macro.findAllByAttribute('active', true)
+    macros = App.Macro.search(filter: { active: true }, sortBy:'name', order:'DESC')
+
     @macroLastUpdated = App.Macro.lastUpdatedAt()
+    @possibleMacros   = []
 
     if _.isEmpty(macros) || !@permissionCheck('ticket.agent')
       macroDisabled = true
+    else
+      for macro in macros
+        if !_.isEmpty(macro.group_ids) && @searchCondition.group_id && !_.includes(macro.group_ids, parseInt(@searchCondition.group_id))
+          continue
+
+        @possibleMacros.push macro
 
     localeEl = $(App.view('ticket_zoom/attribute_bar')(
-      macros: macros
-      macroDisabled: macroDisabled
-      overview_id: @overview_id
+      macros:           @possibleMacros
+      macroDisabled:    macroDisabled
+      overview_id:      @overview_id
       resetButtonShown: resetButtonShown
     ))
     @setSecondaryAction(@secondaryAction, localeEl)

+ 11 - 0
app/assets/javascripts/app/controllers/ticket_zoom/form_handler_macro.coffee

@@ -0,0 +1,11 @@
+class TicketZoomFormHandlerMacro
+
+  # central method, is getting called on every ticket form change
+  # but only trigger event for group_id changes
+  @run: (params, attribute, attributes, classname, form, ui) ->
+
+    return if attribute.name isnt 'group_id'
+
+    App.Event.trigger('MacroPreconditionUpdate', { taskKey: ui.taskKey, params: params })
+
+App.Config.set('120-ticketFormMacro', TicketZoomFormHandlerMacro, 'TicketZoomFormHandler')

+ 11 - 42
app/assets/javascripts/app/models/macro.coffee

@@ -1,57 +1,26 @@
 class App.Macro extends App.Model
-  @configure 'Macro', 'name', 'perform', 'ux_flow_next_up', 'note', 'active'
+  @configure 'Macro', 'name', 'perform', 'ux_flow_next_up', 'note', 'group_ids', 'active'
   @extend Spine.Model.Ajax
   @url: @apiPath + '/macros'
   @configure_attributes = [
-    {
-      name: 'name',
-      display: 'Name',
-      tag: 'input',
-      type: 'text',
-      limit: 100,
-      null: false
+    { name: 'name',            display: 'Name',              tag: 'input', type: 'text', limit: 100, null: false },
+    { name: 'perform',         display: 'Actions',           tag: 'ticket_perform_action',           null: true
     },
-    {
-      name: 'perform',
-      display: 'Actions',
-      tag: 'ticket_perform_action',
-      null: true
-    },
-    {
-      name: 'ux_flow_next_up',
-      display: 'Once completed...',
-      tag: 'select',
-      default: 'none',
-      options: {
-        none: 'Stay on tab',
-        next_task: 'Close tab',
-        next_from_overview: 'Advance to next ticket from overview'
+    { name: 'ux_flow_next_up', display: 'Once completed...', tag: 'select', default: 'none', options: {
+        none: 'Stay on tab', next_task: 'Close tab', next_from_overview: 'Advance to next ticket from overview'
       }
     },
-    {
-      name: 'updated_at',
-      display: 'Updated',
-      tag: 'datetime',
-      readonly: 1
-    },
-    {
-      name: 'note',
-      display: 'Note',
-      tag: 'textarea',
-      limit: 250,
-      null: true
-    },
-    {
-      name: 'active',
-      display: 'Active',
-      tag: 'active',
-      default: true
-    },
+    { name: 'updated_at',      display: 'Updated',  tag: 'datetime',      readonly: 1 },
+    { name: 'note',            display: 'Note',     tag: 'textarea',      limit:   250,      null: true },
+    { name: 'group_ids',       display: 'Groups',   tag: 'column_select', relation: 'Group', null: true },
+    { name: 'active',          display: 'Active',   tag: 'active',        default: true },
   ]
   @configure_delete = true
   @configure_clone = true
   @configure_overview = [
     'name',
+    'note',
+    'group_ids',
   ]
 
   @description = '''

+ 2 - 0
app/models/macro.rb

@@ -8,4 +8,6 @@ class Macro < ApplicationModel
   store     :perform
   validates :name, presence: true
   validates :ux_flow_next_up, inclusion: { in: %w[none next_task next_from_overview] }
+
+  has_and_belongs_to_many :groups, after_add: :cache_update, after_remove: :cache_update, class_name: 'Group'
 end

+ 18 - 0
db/migrate/20190713000001_group_dependent_macros.rb

@@ -0,0 +1,18 @@
+class GroupDependentMacros < ActiveRecord::Migration[4.2]
+  def up
+
+    create_table :groups_macros, id: false do |t| # rubocop:disable Rails/CreateTableWithTimestamps
+      t.references :macro, null: false
+      t.references :group, null: false
+    end
+    add_index :groups_macros, [:macro_id]
+    add_index :groups_macros, [:group_id]
+    add_foreign_key :groups_macros, :macros
+    add_foreign_key :groups_macros, :groups
+
+  end
+
+  def self.down
+    drop_table :groups_macros
+  end
+end

+ 1 - 1
spec/factories/macro.rb

@@ -1,7 +1,7 @@
 FactoryBot.define do
   factory :macro do
     sequence(:name) { |n| "Macro #{n}" }
-    perform         { {} }
+    perform         { { 'ticket.state_id' => { 'value' => 1 } } }
     ux_flow_next_up { 'next_task' }
     note            { '' }
     active          { true }

+ 31 - 0
spec/support/capybara/browser_test_helper.rb

@@ -27,6 +27,37 @@ module BrowserTestHelper
     Waiter.new(wait_handle)
   end
 
+  # Moves the mouse from its current position by the given offset.
+  # If the coordinates provided are outside the viewport (the mouse will end up outside the browser window)
+  # then the viewport is scrolled to match.
+  #
+  # @example
+  # move_mouse_by(x, y)
+  # move_mouse_by(100, 200)
+  #
+  def move_mouse_by(x_axis, y_axis)
+    page.driver.browser.action.move_by(x_axis, y_axis).perform
+  end
+
+  # Clicks and hold (without releasing) in the middle of the given element.
+  #
+  # @example
+  # click_and_hold(ticket)
+  # click_and_hold(tr[data-id='1'])
+  #
+  def click_and_hold(element)
+    page.driver.browser.action.click_and_hold(element).perform
+  end
+
+  # Releases the depressed left mouse button at the current mouse location.
+  #
+  # @example
+  # release_mouse
+  #
+  def release_mouse
+    page.driver.browser.action.release.perform
+  end
+
   class Waiter < SimpleDelegator
 
     # This method is a derivation of Selenium::WebDriver::Wait#until

+ 10 - 0
spec/support/capybara/common_actions.rb

@@ -173,6 +173,16 @@ module CommonActions
     page.driver.browser.navigate.refresh
     attribute
   end
+
+  # opens the macro list in the ticket view via click
+  #
+  # @example
+  #  open_macro_list
+  #
+  def open_macro_list
+    click '.js-openDropdownMacro'
+  end
+
 end
 
 RSpec.configure do |config|

+ 12 - 0
spec/support/capybara/selectors.rb

@@ -23,3 +23,15 @@ end
 Capybara.add_selector(:text_module) do
   css { |id| %(.shortcut > ul > li[data-id="#{id}"]) }
 end
+
+Capybara.add_selector(:macro) do
+  css { |id| %(.js-submitDropdown > ul > li[data-id="#{id}"]) }
+end
+
+Capybara.add_selector(:macro_batch) do
+  css { |id| %(.batch-overlay-macro-entry[data-id='#{id}']) }
+end
+
+Capybara.add_selector(:table_row) do
+  css { |id| %(tr[data-id='#{id}']) }
+end

Some files were not shown because too many files changed in this diff