OSDN Git Service

operator move to cache
[pettanr/pettanr.git] / app / assets / javascripts / editor / panel_editor / element.js.coffee
1 class Editor.EditorModule.DockModule.ElementBay extends Editor.EditorModule.DockModule.BoardBay\r
2   \r
3   initialize: (options) ->\r
4     super(options)\r
5     @boards = new Editor.EditorModule.DockModule.ElementBay.Boards({\r
6       parent: this\r
7     })\r
8     @listenTo(@boards, 'sorted', @sorted)\r
9   \r
10   add_element: (element) ->\r
11     index = @tabs.length\r
12     @shift_z()\r
13     tab = new Editor.EditorModule.DockModule.ElementBoard({\r
14       parent: this, index: index, name: element.item_name(), element: element\r
15     })\r
16     bb = new Editor.EditorModule.DockModule.TabModule.ElementBoardBody({parent: tab})\r
17     @add_tab(\r
18       tab, \r
19       new Editor.EditorModule.DockModule.TabModule.ElementBoardLabel({\r
20         parent: tab, \r
21         name: element.item_name()\r
22       }), \r
23       bb\r
24     )\r
25     @boards.add_element_tab(tab)\r
26     @boards.$el.tabs('refresh')\r
27     if element.constructor.has_picture()\r
28       @trigger('add:credit', element)\r
29   \r
30   shift_z: () ->\r
31     new_tab_z = @new_tab.label.z()\r
32     _.each @tabs, (tab) ->\r
33       z = tab.label.z()\r
34       tab.label.$el.attr('data-z', z + 1) if z >= new_tab_z  # new tab too\r
35   \r
36   add_new_tab: () ->\r
37     @new_tab = new Editor.EditorModule.DockModule.NewElementBoard({\r
38       parent: this, index: 0, name: 'new'\r
39     })\r
40     bb = new Editor.EditorModule.DockModule.TabModule.NewElementBoardBody({parent: @new_tab})\r
41     @add_tab(\r
42       @new_tab, \r
43       new Editor.EditorModule.DockModule.TabModule.NewElementBoardLabel({\r
44         parent: @new_tab\r
45       }), \r
46       bb\r
47     )\r
48     @listenTo(bb, 'pick', @pick)\r
49     @boards.add_element_tab(@new_tab)\r
50   \r
51   render: () ->\r
52     this.$el.html('')\r
53     @delegateEvents({'activate': 'activate'})\r
54     this.$el.attr('id', @body.dom_id())\r
55     this.$el.append(@boards.render().el)\r
56     _this = this\r
57     @boards.$el.tabs({\r
58       activate: (e, ui) ->\r
59         ui.newPanel.trigger('activate')\r
60     })\r
61     this\r
62   \r
63   init_tabs: () ->\r
64     @add_new_tab()\r
65     list = @root_item().zorderd_elements()\r
66     _.each list, (element) =>\r
67       @add_element(element)\r
68   \r
69   activate: () ->\r
70     _.each @tabs, (tab) ->\r
71       return if not tab.element  # without new element tab\r
72       if tab.body.$el.css('display') != 'none'\r
73         tab.body.refresh()\r
74   \r
75   sorted: () ->\r
76     # data-z copy to model\r
77     new_tab_z = @new_tab.label.z()\r
78     _.each @tabs, (tab) ->\r
79       if tab.element   # without new_tab\r
80         z = tab.label.z()\r
81         z-- if z > new_tab_z  # from 0, 1, new tab, 3... to 1, 2, 3...\r
82         tab.element.set({\r
83           z: z\r
84         }, {silent: true})\r
85         tab.element.trigger('sort')\r
86   \r
87   panel_body: () ->\r
88     @dock().editor().body\r
89   \r
90   save_data: () ->\r
91     attrs = {}\r
92     _.each @tabs, (tab) ->\r
93       return if not tab.element  # without new element tab\r
94       form = tab.body.form\r
95       name = form.item.table_name() + '_attributes'\r
96       attrs[name] ||= []\r
97       attrs[name].push(form.save_data())\r
98     attrs\r
99   \r
100   pick: (new_item) ->\r
101     @add_element(new_item)\r
102     @trigger('pick', new_item)\r
103   \r
104 class Editor.EditorModule.DockModule.TabModule.ElementBayLabel extends Editor.EditorModule.DockModule.TabModule.BayLabel\r
105   \r
106 class Editor.EditorModule.DockModule.TabModule.ElementBayBody extends Editor.EditorModule.DockModule.TabModule.BayBody\r
107   \r
108   render: () ->\r
109     @body.render()\r
110   \r