class Editor.EditorModule.DockModule.ElementBay extends Editor.EditorModule.DockModule.BoardBay initialize: (options) -> super(options) @current_index = 1 @boards = new Editor.EditorModule.DockModule.ElementBay.Boards({ parent: this }) @listenTo(@boards, 'sorted', @sorted) @removed_elements = [] add_element: (element) -> @shift_z() tab = new Editor.EditorModule.DockModule.ElementBoard({ parent: this, index: @current_index, name: element.item_name(), element: element }) bl = new Editor.EditorModule.DockModule.TabModule.ElementBoardLabel({ parent: tab, name: element.item_name() }) bb = new Editor.EditorModule.DockModule.TabModule.ElementBoardBody({parent: tab}) @listenTo(bl, 'destroy', @destroy) @add_tab(tab, bl, bb) @boards.add_element_tab(tab) @boards.$el.tabs('refresh') if element.constructor.has_picture() @trigger('add:credit', element) @sorted() # recalc z @current_index++ find_tab: (element) -> t = null _.each @tabs, (tab) => if tab.element if tab.element.cid == element.cid t = tab return t remove_element: (element) -> if !element.isNew() element.set({_destroy: true}, {silent: true}) @removed_elements.push element tab = @find_tab(element) tab.label.remove() tab.body.remove() @tabs.splice(@tabs.indexOf(tab), 1) #@tabs = _.compact(@tabs) @unshift_z() @sorted() # recalc t shift_z: () -> new_tab_z = @new_tab.label.z() _.each @tabs, (tab) -> z = tab.label.z() tab.label.$el.attr('data-z', z + 1) if z >= new_tab_z # new tab too unshift_z: () -> @boards.labels.reorder() add_new_tab: () -> @new_tab = new Editor.EditorModule.DockModule.NewElementBoard({ parent: this, index: 0, name: 'new' }) bb = new Editor.EditorModule.DockModule.TabModule.NewElementBoardBody({parent: @new_tab}) @add_tab( @new_tab, new Editor.EditorModule.DockModule.TabModule.NewElementBoardLabel({ parent: @new_tab }), bb ) @listenTo(bb, 'pick', @pick) @listenTo(bb, 'dialog:open', @open_dialog) @listenTo(bb, 'dialog:close', @close_dialog) @boards.add_element_tab(@new_tab) render: () -> this.$el.html('') @delegateEvents({'activate': 'activate'}) this.$el.attr('id', @body.dom_id()) this.$el.append(@boards.render().el) _this = this @boards.$el.tabs({ activate: (e, ui) -> ui.newPanel.trigger('activate') }) this init_tabs: () -> @add_new_tab() list = @root_item().zorderd_elements() _.each list, (element) => @add_element(element) activate: () -> _.each @tabs, (tab) -> return if not tab.element # without new element tab if tab.body.$el.css('display') != 'none' tab.body.refresh() sorted: () -> # data-z copy to model new_tab_z = @new_tab.label.z() _.each @tabs, (tab) -> if tab.element # without new_tab z = tab.label.z() z-- if z > new_tab_z # from 0, 1, new tab, 3... to 1, 2, 3... tab.element.set({ z: z + 1 }, {silent: true}) tab.element.trigger('sort') panel_body: () -> @dock().editor().body save_data: () -> attrs = {} _.each @tabs, (tab) -> return if not tab.element # without new element tab form = tab.body.form name = form.item.table_name() + '_attributes' attrs[name] ||= [] attrs[name].push(form.save_data()) _.each @removed_elements, (element) -> name = element.table_name() + '_attributes' attrs[name] ||= [] attrs[name].push({id: element.get('id'), _destroy: 1}) attrs pick: (new_item) -> @add_element(new_item) z = @new_tab.label.z() new_item.set({z: z}, {silent: true}) @trigger('pick', new_item) open_dialog: (dialog) -> @trigger('dialog:open', dialog) close_dialog: (dialog) -> @trigger('dialog:close', dialog) destroy: (element) -> @remove_element(element) @trigger('destroy', element) class Editor.EditorModule.DockModule.TabModule.ElementBayLabel extends Editor.EditorModule.DockModule.TabModule.BayLabel class Editor.EditorModule.DockModule.TabModule.ElementBayBody extends Editor.EditorModule.DockModule.TabModule.BayBody render: () -> @body.render()