OSDN Git Service

912028034dacefc49d85b5b2b608310a66959f78
[pettanr/pettanr.git] / app / assets / javascripts / views / balloons / element.js.coffee
1 class Pettanr.Views.Balloon.Element extends Backbone.View\r
2   tagName: 'img'\r
3   className: 'pettanr-balloon'\r
4   events: {\r
5     'ready:picture': 'set_style'\r
6   }\r
7   \r
8   initialize: (options) ->\r
9     @element = options.element\r
10     @root = options.root\r
11     @spot = options.spot\r
12     @speech_balloon = options.parent\r
13     @system_picture = @element.system_picture()\r
14   \r
15   clear: () ->\r
16     @system_picture.fetch({cache: true}).done =>\r
17       @render()\r
18     this\r
19   \r
20   render: () ->\r
21     @set_style()\r
22     this\r
23   \r
24   set_style: () ->\r
25     attr = {\r
26       src: @system_picture.r_url(), \r
27       alt: @element.get('caption'),\r
28       style: Pettanr.to_style(@style())\r
29     }\r
30     this.$el.attr(attr)\r
31   \r
32   style: () ->\r
33     {\r
34       'width': '100%',\r
35       'height': '100%',\r
36       'z-index': 1  # play case\r
37     }\r
38   \r
39   add_element: (element) ->\r
40     null\r
41   \r
42 class Pettanr.Views.Balloon.Element.Edit extends Pettanr.Views.Balloon.Element\r
43   \r
44   initialize: (options) ->\r
45     super(options)\r
46     @listenTo(@element, 'input:r', @input_r)\r
47     @listenTo(@element, 'input:width', @resize)\r
48     @listenTo(@element, 'input:height', @resize)\r
49   \r
50   render: () ->\r
51     super()\r
52     sb = @speech_balloon\r
53     elm = @element\r
54     this.$el.resizable {\r
55       stop: (event, ui) ->\r
56         resize_div = $(@)\r
57         speech_balloon_div = sb.$el\r
58         \r
59         resize_div.css('top', '0px')\r
60         resize_div.css('left', '0px')\r
61         resize_div.css('z-index', 1)\r
62         width = ui.size.width\r
63         height = ui.size.height\r
64         top = speech_balloon_div.position().top\r
65         if ui.originalPosition.top != ui.position.top\r
66           top += ui.position.top\r
67           speech_balloon_div.css('top', top.toString() + 'px')\r
68           speech_balloon_div.css('height', height + 'px')\r
69         left = speech_balloon_div.position().left\r
70         if ui.originalPosition.left != ui.position.left\r
71           left += ui.position.left\r
72           speech_balloon_div.css('left', left.toString() + 'px')\r
73           speech_balloon_div.css('width', width + 'px')\r
74         speech_balloon_div.css('width', width.toString() + 'px')\r
75         speech_balloon_div.css('height', height.toString() + 'px')\r
76         elm.set({\r
77           x: Math.floor(left), \r
78           y: Math.floor(top), \r
79           width: width, \r
80           height: height\r
81         }, {silent: true})\r
82         elm.trigger('resize')\r
83      resize: (event, ui) ->\r
84         resize_div = $(@)\r
85         speech_balloon_div = resize_div.parent()\r
86       handles: 'all',\r
87       autoHide: true\r
88     }\r
89     this\r
90   \r
91   input_r: () ->\r
92     @system_picture = @element.system_picture()\r
93     @clear()\r
94     resize_div = this.$el.parent()\r
95     resize_div.css('z-index', 1)\r
96   \r
97   resize: () ->\r
98     resize_div = this.$el.parent()\r
99     resize_div.css('width', '100%')\r
100     resize_div.css('height', '100%')\r
101     this.$el.css('width',  '100%')\r
102     this.$el.css('height', '100%')\r
103   \r