# SYS_ITEMS

F_NAME: Card Manager
F_ITEM_NAME: cardman
F_VIEW_TEMPLATE: main.ui
F_JS_FILENAME: cardman.js
F_CLIENT_MODULE:
  # -*- coding: utf-8 -*-

  import gtk

  def on_before_show_main_form(task):

      def view_item(widget, it):
          if it.item_type == 'report':
              it.print_report(widget)
          else:
              caption_box.get_children()[0].set_markup('<big><b>%s</b></big>' % widget.get_label())
              it.view(widget)
              if task.key_press_id and task.main_form.window.handler_is_connected(task.key_press_id):
                  task.main_form.window.disconnect(task.key_press_id)
              if it.view_keypressed:
                  task.key_press_id = task.main_form.window.connect("key-press-event", it.view_keypressed)

      body = task.main_form.body
      groups = [task.journals, task.reports, task.catalogs];
      caption_box = None
      nb = gtk.Notebook()
      nb.set_properties(visible=True, can_focus=False, tab_border=6, tab_hborder=12, tab_vborder=6)
      task.key_press_id = None
      for group in groups:
          tb = gtk.Toolbar()
          tb.set_style(gtk.TOOLBAR_TEXT)
          tb.set_properties(visible=True, can_focus=False, icon_size=2, icon_size_set=True)
          group_index = nb.append_page(tb, gtk.Label(group.item_caption))
          index = 0
          for it in group.items:
              if it.visible and it.can_view():
                  nm = it.item_caption
                  b = gtk.ToolButton(label=nm)
                  b.set_use_underline(True)
                  b.set_properties(visible=True, can_focus=True, border_width=6)
                  b.set_expand(False)
                  b.set_homogeneous(False)
                  b.connect("clicked", view_item, it)
                  tb.insert(b, index)
                  index += 1
      body.pack_start(nb, False, False, 0)
      nb.set_current_page(0)
      hbox = gtk.HBox(False, 3)
      hbox.props.can_focus = False
      hbox.pack_start(gtk.Label(), False)
      hbox.pack_start(gtk.HSeparator())
      caption_box = hbox
      body.pack_start(hbox, False)
      task.container = gtk.Alignment(0.5, 0.5, 1, 1)
      body.pack_start(task.container)
      body.show_all()
  #    task.main_form.window.maximize()

  def create_print_btns(item):

      def on_menu_click(widget):
          widget.report.print_report(widget)

      def button_pressed(widget, event):
          if event.type == gtk.gdk.BUTTON_PRESS:
              widget.popup(None, None, None, event.button, event.time)
              return True

      reports = []
      for report in item.reports:
          if report.can_view():
              reports.append(report)
      if len(reports):
          report = reports[0]
          table = gtk.Table(1, 2, False)
          print_btn = gtk.Button(report.item_caption)
          table.attach(print_btn, 0, 1, 0, 1)
          item.view_form.btns_hbox.pack_end(table, False, False)
          if len(reports) > 1:
              menu = gtk.Menu()
              for report in reports:
                  menu_item = gtk.MenuItem(report.item_caption)
                  menu_item.report = report
                  menu.append(menu_item)
                  menu_item.connect("activate", on_menu_click)
              menu.show_all()
              arrow_btn = gtk.Button()
              arrow = gtk.Arrow(gtk.ARROW_DOWN, gtk.SHADOW_OUT)
              arrow_btn.add(arrow)
              arrow_btn.connect_object("event", button_pressed, menu)
              table.attach(arrow_btn, 1, 2, 0, 1)
              print_btn.set_label('Reports')
              print_btn.connect_object("event", button_pressed, menu)
              print_btn.set_size_request(100, -1)
          else:
              print_btn.connect("clicked", report.print_report)
          table.show_all()

  def on_before_show_view_form(item):
      if not item.master:
          item.auto_loading = True;

      if not (item.is_lookup_item):
          container = item.task.container
          for child in container:
              container.remove(child)
              child.destroy()
          item.view_form.window.remove(item.view_form.body)
          container.add(item.view_form.body)
          item.view_form.window.destroy()
          item.view_form.window = None

      if item.can_create():
          item.view_form.new_button.connect('clicked', item.insert_record)
      else:
          item.view_form.new_button.set_sensitive(False)
      if item.can_edit():
          item.view_form.edit_button.connect('clicked', item.edit_record)
      else:
          item.view_form.edit_button.set_sensitive(False)
      if item.can_delete():
          item.view_form.delete_button.connect('clicked', item.delete_record)
      else:
          item.view_form.delete_button.set_sensitive(False)

      item.grid = item.create_grid(item.view_form.grid_panel)
      item.grid.show_all()
      create_print_btns(item)

  def on_after_show_view_form(item):
      item.open()

  def on_before_show_edit_form(item):
      dic = {
          "on_ok_button_clicked" : item.apply_record,
          "on_cancel_button_clicked" : item.cancel_edit,
          }

      item.edit_form.builder.connect_signals(dic)
      item.create_entries(item.edit_form.fields_container)

  def on_after_show_edit_form(item):
      for detail in item.details:
          if item.details_active:
              detail.update_controls(common.UPDATE_OPEN)
          else:
              if not detail.disabled:
                  detail.open()

  def on_edit_form_close_query(item):
      if item.modified:
          res = item.yes_no_cancel(item.task.lang['save_changes'])
          if res == 1:
              if not item.apply_record():
                  return True
          elif res == 0:
              item.cancel_edit()
          else:
              return True
      else:
          item.cancel_edit()

  def on_before_show_filter_form(item):
      dic = {
          "on_ok_button_clicked" : item.apply_filter,
          "on_close_button_clicked" : item.close_filter
          }

      item.filter_form.builder.connect_signals(dic)
      item.filter_form.window.set_title(item.item_caption + ' - ' + u'Filter')
      item.create_filter_entries(item.filter_form.fields_container)

  def on_before_show_params_form(item):
      dic = {
          "on_print_button_clicked" : item.process_report,
          "on_close_button_clicked" : item.close_form
          }
      item.params_form.builder.connect_signals(dic)
      item.edit_params(item.params_form.fields_container)

  def on_view_keypressed(item, event):
      widget = item.task.main_form.window
      if event.keyval == gtk.keysyms.Insert:
          if event.state & gtk.gdk.CONTROL_MASK:
              item.insert_record(widget)
      elif event.keyval == gtk.keysyms.Delete:
          if event.state & gtk.gdk.CONTROL_MASK:
              item.delete_record(widget)

  def on_edit_keypressed(item, event):
      widget = item.task.main_form.window
      if event.keyval == gtk.keysyms.Return:
          if event.state & gtk.gdk.CONTROL_MASK:
              item.apply_record(widget)

F_WEB_CLIENT_MODULE:
  function on_page_loaded(task) {
      
      $("title").text(task.item_caption);
      $("#title").text(task.item_caption);
        
      if (task.safe_mode) {
          $("#user-info").text(task.user_info.role_name + ' ' + task.user_info.user_name);
          $('#log-out')
          .show() 
          .click(function(e) {
              e.preventDefault();
              task.logout();
          }); 
      }

      if (task.full_width) {
          $('#container').removeClass('container').addClass('container-fluid');
      }
      $('#container').show();
      
      task.create_menu($("#menu"), $("#content"), {
          // splash_screen: '<h1 class="text-center">Application</h1>',
          view_first: true
      });

      // $(document).ajaxStart(function() { $("html").addClass("wait"); });
      // $(document).ajaxStop(function() { $("html").removeClass("wait"); });
  } 

  function on_view_form_created(item) {
      var table_options_height = item.table_options.height,
          table_container;

      item.clear_filters();
      
      item.view_options.table_container_class = 'view-table';
      item.view_options.detail_container_class = 'view-detail';
      item.view_options.open_item = true;
      
      if (item.view_form.hasClass('modal')) {
          item.view_options.width = 1060;
          item.table_options.height = $(window).height() - 300;
      }
      else {
          if (!item.table_options.height) {
              item.table_options.height = $(window).height() - $('body').height() - 20;
          }
      }
      
      if (item.can_create()) {
          item.view_form.find("#new-btn").on('click.task', function(e) {
              e.preventDefault();
              if (item.master) {
                  item.append_record();
              }
              else {
                  item.insert_record();
              }
          });
      }
      else {
          item.view_form.find("#new-btn").prop("disabled", true);
      }

      item.view_form.find("#edit-btn").on('click.task', function(e) {
          e.preventDefault();
          item.edit_record();
      });

      if (item.can_delete()) {
          item.view_form.find("#delete-btn").on('click.task', function(e) {
              e.preventDefault();
              item.delete_record();
          });
      }
      else {
          item.view_form.find("#delete-btn").prop("disabled", true);
      }
      
      create_print_btns(item);

      task.view_form_created(item);
      
      if (!item.master && item.owner.on_view_form_created) {
          item.owner.on_view_form_created(item);
      }

      if (item.on_view_form_created) {
          item.on_view_form_created(item);
      }
      
      item.create_view_tables();
      
      if (!item.master && item.view_options.open_item) {
          item.open(true);
      }

      if (!table_options_height) {
          item.table_options.height = undefined;
      }
      return true;
  }

  function on_view_form_shown(item) {
      item.view_form.find('.dbtable.' + item.item_name + ' .inner-table').focus();
  }

  function on_view_form_closed(item) {
      if (!item.master && item.view_options.open_item) {    
          item.close();
      }
  }

  function on_edit_form_created(item) {
      item.edit_options.inputs_container_class = 'edit-body';
      item.edit_options.detail_container_class = 'edit-detail';
      
      item.edit_form.find("#cancel-btn").on('click.task', function(e) { item.cancel_edit(e) });
      item.edit_form.find("#ok-btn").on('click.task', function() { item.apply_record() });
      if (!item.is_new() && !item.can_modify) {
          item.edit_form.find("#ok-btn").prop("disabled", true);
      }
      
      task.edit_form_created(item);
      
      if (!item.master && item.owner.on_edit_form_created) {
          item.owner.on_edit_form_created(item);
      }

      if (item.on_edit_form_created) {
          item.on_edit_form_created(item);
      }
          
      item.create_inputs(item.edit_form.find('.' + item.edit_options.inputs_container_class));
      item.create_detail_views(item.edit_form.find('.' + item.edit_options.detail_container_class));

      return true;
  }

  function on_edit_form_close_query(item) {
      var result = true;
      if (item.is_changing()) {
          if (item.is_modified()) {
              item.yes_no_cancel(task.language.save_changes,
                  function() {
                      item.apply_record();
                  },
                  function() {
                      item.cancel_edit();
                  }
              );
              result = false;
          }
          else {
              item.cancel_edit();
          }
      }
      return result;
  }

  function on_filter_form_created(item) {
      item.filter_options.title = item.item_caption + ' - filters';
      item.create_filter_inputs(item.filter_form.find(".edit-body"));
      item.filter_form.find("#cancel-btn").on('click.task', function() {
          item.close_filter_form(); 
      });
      item.filter_form.find("#ok-btn").on('click.task', function() { 
          item.set_order_by(item.view_options.default_order);
          item.apply_filters(item._search_params); 
      });
  }

  function on_param_form_created(item) {
      item.create_param_inputs(item.param_form.find(".edit-body"));
      item.param_form.find("#cancel-btn").on('click.task', function() { 
          item.close_param_form();
      });
      item.param_form.find("#ok-btn").on('click.task', function() { 
          item.process_report();
      });
  }

  function on_before_print_report(report) {
      var select;
      report.extension = 'pdf';
      if (report.param_form) {
          select = report.param_form.find('select');
          if (select && select.val()) {
              report.extension = select.val();
          }
      }
  }

  function on_view_form_keyup(item, event) {
      if (event.keyCode === 45 && event.ctrlKey === true){
          if (item.master) {
              item.append_record();
          }
          else {
              item.insert_record();                
          }
      }
      else if (event.keyCode === 46 && event.ctrlKey === true){
          item.delete_record(); 
      }
  }

  function on_edit_form_keyup(item, event) {
      if (event.keyCode === 13 && event.ctrlKey === true){
          item.edit_form.find("#ok-btn").focus(); 
          item.apply_record();
      }
  }

  function create_print_btns(item) {
      var i,
          $ul,
          $li,
          reports = [];
      if (item.reports) {
          for (i = 0; i < item.reports.length; i++) {
              if (item.reports[i].can_view()) {
                  reports.push(item.reports[i]);
              }
          }
          if (reports.length) {
              $ul = item.view_form.find("#report-btn ul");
              for (i = 0; i < reports.length; i++) {
                  $li = $('<li><a href="#">' + reports[i].item_caption + '</a></li>');
                  $li.find('a').data('report', reports[i]);
                  $li.on('click', 'a', function(e) {
                      e.preventDefault();
                      $(this).data('report').print(false);
                  });
                  $ul.append($li);
              }
          }
          else {
              item.view_form.find("#report-btn").hide();
          }
      }
      else {
          item.view_form.find("#report-btn").hide();
      }
  }

F_JS_FUNCS:
  	this.on_page_loaded = on_page_loaded;
  	this.on_view_form_created = on_view_form_created;
  	this.on_view_form_shown = on_view_form_shown;
  	this.on_view_form_closed = on_view_form_closed;
  	this.on_edit_form_created = on_edit_form_created;
  	this.on_edit_form_close_query = on_edit_form_close_query;
  	this.on_filter_form_created = on_filter_form_created;
  	this.on_param_form_created = on_param_form_created;
  	this.on_before_print_report = on_before_print_report;
  	this.on_view_form_keyup = on_view_form_keyup;
  	this.on_edit_form_keyup = on_edit_form_keyup;
  	this.create_print_btns = create_print_btns;


F_NAME: Справочники
F_ITEM_NAME: catalogs
F_VIEW_TEMPLATE: catalogs_view.ui
F_EDIT_TEMPLATE: edit.ui
F_FILTER_TEMPLATE: filter.ui
F_CLIENT_MODULE:
  # -*- coding: utf-8 -*-

  import gtk

  def on_before_show_view_form(item):

      def search(widget):
          text = item.view_form.find_entry.get_text()
          default_field = item.find_default_field()
          if default_field:
              if text:
                  item.search(default_field.field_name, text)
              else:
                  item.open()

      def find_text(widget, event):
          if event.keyval == gtk.keysyms.Return:
              search(widget)

      def find_changed(entry):
          text = entry.get_text().decode('utf-8')
          search(entry)

      item.view_form.select_button.connect('clicked',  item.set_lookup_field_value)
      item.view_form.find_entry.connect('changed', find_changed)
      btn = item.view_form.select_button
      if btn:
          if item.is_lookup_item:
              btn.set_visible(True)
          else:
              btn.set_visible(False)
      find_box = item.view_form.find_hbox
  #    default_field = item.find_default_field()
      if not item.default_field:
          find_box.set_visible(False)
      else:
          item.view_form.find_entry.grab_focus()


F_NAME: Журналы
F_ITEM_NAME: journals
F_VIEW_TEMPLATE: view.ui
F_EDIT_TEMPLATE: edit.ui
F_FILTER_TEMPLATE: filter.ui
F_CLIENT_MODULE:
  # -*- coding: utf-8 -*-

  def on_before_show_view_form(item):

      def filter_applied(item):
          if item.view_form and item.view_form.statusbar:
              item.view_form.statusbar.push(0, item.get_status_text())

      item.view_form.select_button.connect("clicked", item.create_filter_form)
      item.on_filter_applied = filter_applied


F_NAME: Cards
F_ITEM_NAME: cards
F_TABLE_NAME: CARDMAN_CARDS
F_INFO:
  json{"view": {"0": ["", {"buttons_on_top": true}, [], {"summary_fields": [11]}, [[9, ""], [10, ""], [11, ""], [24, ""], [26, ""], [25, ""]], []]}, "edit": {"0": ["", {"width": 1000}, [], [["", [[{}, [[9], [10], [11], [24], [23], [26], [25]], ""]]]]]}, "order": [], "reports": []}

F_WEB_CLIENT_MODULE:
  function on_edit_form_shown(item) {
      item.edit_form.find("textarea.authors").height(50);
      item.edit_form.find("textarea.title").height(50);
      item.edit_form.find("textarea.publication_data").height(50);
      item.edit_form.find("textarea.key_words_txt").height(50);
      item.edit_form.find("textarea.annotation").height(200);
  }


  function on_field_changed(field,lookup_item) {
      var item = field.owner;
      if (field.field_name === 'key_words') {
          if (item.key_words_txt.value>'') {
          item.key_words_txt.value=item.key_words_txt.value+';\n'+lookup_item.key_word.value;
          }
          else{ item.key_words_txt.value=lookup_item.key_word.value;
          }
         
          
      }
      
  }


  function on_filter_select_value(field, lookup_item) {
      var item = field.owner;
      //alert(field.field_name);
      //alert(item.key_words.value);
       alert(lookup_item.field_name.value);
          
          
      }


  function test() {
     
      cards.clear_filters();
      cards.filters.key_words_txt.value = 'dsddsdsds';

      cards.open({where: {key_words_txt__contains: 'dsddsdsds'}});

      cards.set_where({key_words_txt__contains: 'dsddsdsds'});
      cards.open();

      cards.open();
  }

  function on_filters_apply(item) {
     // test();
    // alert('1dsdsdsd');
    //  alert(cards.filters.key_words_txt1.value);
    //  cards.filter_by_name('key_words').value = null;
      //cards.filter_by_name('key_words_txt').value = 'ewewewewe';
      
  }

F_JS_FUNCS:
  	this.on_edit_form_shown = on_edit_form_shown;
  	this.on_field_changed = on_field_changed;
  	this.on_filter_select_value = on_filter_select_value;
  	this.test = test;
  	this.on_filters_apply = on_filters_apply;


