# frozen_string_literal: true require 'glimmer-dsl-libui' require_relative '../controllers/master_list_controller' require_relative '../controllers/master_controller' require_relative 'master_input_form' require './jew/util/logger_holder' class MasterListView include Glimmer PAGE_SIZE = 20 def initialize @controller = MasterListController.new(self) @current_page = 1 @total_count = 0 LoggerHolder.instance.debug('MasterListView: initialize') end def on_create @controller.on_view_created @controller.refresh_data(@current_page, PAGE_SIZE) end def update(masters) @items = [] i = 0 item_num = 0 masters.each do |author| i += 1 item_num = ((@current_page - 1) * PAGE_SIZE) + i @items << Struct.new(:№, :id, :имя_мастера, :фамилия_мастера, :отчество_мастера).new(item_num, author.master_id, author.first_name, author.last_name, author.father_name) end @table.model_array = @items @page_label.text = "#{@current_page} / #{(@total_count / PAGE_SIZE.to_f).ceil}" end def update_student_count(new_cnt) @total_count = new_cnt @page_label.text = "#{@current_page} / #{(@total_count / PAGE_SIZE.to_f).ceil}" end def create root_container = horizontal_box { # Секция 1 vertical_box { stretchy false vertical_box { stretchy false label { text 'Отчество' } combobox { |c| items ['Не важно','Есть','Нет'] selected 0 on_selected do @controller.filter_father_name(@current_page, PAGE_SIZE, c.selected) end } label { text 'Сортировка' } combobox { |c| items ['ID','Имя мастера','Фамилия мастера', 'Отчество мастера'] selected 0 on_selected do @controller.sort(@current_page, PAGE_SIZE, c.selected) end } } } # Секция 2 vertical_box { @table = refined_table( table_editable: false, filter: lambda do |row_hash, query| utf8_query = query.force_encoding("utf-8") row_hash['Имя мастера'].include?(utf8_query) end, table_columns: { '№' => :text, 'ID' => :text, 'Имя мастера' => :text, 'Фамилия мастера' => :text, 'Отчество мастера' => :text, }, per_page: PAGE_SIZE, ) @pages = horizontal_box { stretchy false button("<") { stretchy true on_clicked do @current_page = [@current_page - 1, 1].max @controller.refresh_data(@current_page, PAGE_SIZE) end } @page_label = label("...") { stretchy false } button(">") { stretchy true on_clicked do @current_page = [@current_page + 1, (@total_count / PAGE_SIZE.to_f).ceil].min @controller.refresh_data(@current_page, PAGE_SIZE) end } } } # Секция 3 vertical_box { stretchy false button('Добавить') { stretchy false on_clicked { @controller.show_modal_add } } button('Изменить') { stretchy false on_clicked { @controller.show_modal_edit(@current_page, PAGE_SIZE, @table.selection) unless @table.selection.nil? } } button('Удалить') { stretchy false on_clicked { @controller.delete_selected(@current_page, PAGE_SIZE, @table.selection) unless @table.selection.nil? @controller.refresh_data(@current_page, PAGE_SIZE) } } button('Обновить') { stretchy false on_clicked { @controller.refresh_data(@current_page, PAGE_SIZE) } } } } on_create root_container end end