質問するログイン新規登録

質問編集履歴

1

コード全体を追記しました、字数の関係で一部コードを省いております。

2019/03/14 03:07

投稿

alberorana
alberorana

スコア52

title CHANGED
File without changes
body CHANGED
@@ -24,4 +24,253 @@
24
24
  おそらく動的に追加した要素なので、` .on('click', クラス名, function(e) {` とする必要があり、 ` $('#edit-iframe').contents().find('.js-section-hidden').on('click',$('#edit-iframe').contents().find('.js-section-hidden'), function(e) {
25
25
  ` このようにしてみたのですが、うまく行かなかったため行き詰まっています。
26
26
 
27
- この問題の解決策をご存知の方がいらっしゃいましたら、ご回答いただけますと幸いです。
27
+ この問題の解決策をご存知の方がいらっしゃいましたら、ご回答いただけますと幸いです。
28
+
29
+ 同じシステム(ドメイン)の /example/ID/ID をiframeのsrcにしています。
30
+
31
+ ```php
32
+ <!--{layout_header}-->
33
+ <link href="/sass/css/edit.css" rel="stylesheet">
34
+ <link href="/css/huebee.css" rel="stylesheet">
35
+ <script>
36
+ window.vars = [];
37
+ window.user = <?= $json_user ?> ;
38
+ window.templateId = <?= $user['template_id'] ?> ;
39
+ </script>
40
+ <!--{/layout_header}-->
41
+
42
+ <!--{layout_footer}-->
43
+ <script src="/plugins/jQueryUI/jquery-ui.min.js"></script>
44
+ <script src="/js/HP/edit.js"></script>
45
+
46
+
47
+ <a href="/view/<?= $this->current_user->id ?>" class="mb10" style="display: block;" target="_blank">実際のページを確認</a>
48
+
49
+ <button class="js-edit-submit save-button">保存 <br><i class="fas fa-save mt5"></i></button>
50
+ <button class="js-edit-section section-button">パーツ<br> 追加 <br><i class="fas fa-save mt5"></i></button>
51
+
52
+ <? if($source): ?>
53
+ <iframe src="/view/<?= $user['id'] ?>" width="99%" height="80%" class="js-iframe" id='iframe' device="pc"></iframe>
54
+ <iframe src="/view/<?= $user['id'] ?>" width="99%" height="80%" id='edit-iframe' device="pc" contenteditable="true"></iframe>
55
+ <? else: ?>
56
+ <iframe src="/example/<?= $user['template_id'] ?>/<?= $user['id'] ?>" width="99%" height="80%" class="js-iframe" id='iframe'device="pc"></iframe>
57
+ <iframe src="/example/<?= $user['template_id'] ?>/<?= $user['id'] ?>" width="99%" height="80%" id='edit-iframe' device="pc" contenteditable="true"></iframe>
58
+ <? endif; ?>
59
+ ```
60
+ ```edit.js
61
+ var protocol = location.protocol;
62
+ var url = location.host;
63
+ window.editingDom = null;
64
+
65
+ //要素の中身変更
66
+ $(window).on("load", function(){
67
+ $('#edit-iframe').contents().find('head').append('<link rel="stylesheet" href="/sass/css/edit-iframe.css" type="text/css" />');
68
+ //保存ボタン
69
+ $(document).on("click", ".js-edit-submit", function () {
70
+ //画像変更だけした場合の対策
71
+ //初回に文言だけ変更して保存した場合の分岐
72
+ console.log(window.editingDom);
73
+ if(window.editingDom)
74
+ {
75
+ if(window.editingDom.prop('tagName') != 'img')
76
+ {
77
+ var childVar= {'html_id': window.editingDom.attr('id'), 'content': window.editingDom.html(), 'image_flag': '0'};
78
+ console.log('childVar');
79
+ console.log(childVar);
80
+ window.vars.push(childVar);
81
+ }
82
+ }
83
+ //編集のための要素を削除してから、本番ソースにする
84
+ $('#edit-iframe').contents().find(".js-remove-element").remove();
85
+ $('#edit-iframe').contents().find(".is-hidden").remove();
86
+ $('#edit-iframe').contents().find(".js-section-hidden").each(function(i){
87
+ $(this).remove();
88
+ });
89
+ $('#edit-iframe').contents().find(".js-section-appear").each(function(i){
90
+ $(this).remove();
91
+ });
92
+ //bodyのcontenteditableを削除
93
+ var iframe = $('#edit-iframe')[0];
94
+ iframe.contentDocument.body.contentEditable = false;
95
+ iframe.designMode = 'off';
96
+
97
+ console.log($('#edit-iframe').contents().find('html').html());
98
+ commit($('#edit-iframe').contents().find('html').html(), window.vars);
99
+
100
+ //以下編集を解除する処理
101
+ unEdit();
102
+ //updatePreview();
103
+ //window.updatePreviewImage();
104
+ window.vars = [];
105
+ alert('保存が完了しました。');
106
+ location.reload();
107
+ });
108
+
109
+ function commit(source,vars=null)
110
+ {
111
+ //source = source.replace( /このエリアを非表示にする/g , "" ) ;
112
+ //ソースコード自体を保存する処理
113
+ console.log(source);
114
+ //return false;
115
+ $.ajax({
116
+ url:protocol + '//' + url + '/api/v1/source/commit' ,
117
+ type: 'POST',
118
+ dataType: 'json',
119
+ data: {'source' : source},
120
+ success: function(response){
121
+
122
+ },
123
+ error : function(XMLHttpRequest, textStatus, errorThrown) {
124
+
125
+ }
126
+ });//AJAX
127
+ //文言と画像をまとめてインサート
128
+ var i;
129
+ for(i=0;i<vars.length;i++)
130
+ {
131
+ console.log(vars[i]);
132
+ if(vars[i].content && vars[i].html_id)
133
+ {
134
+ $.ajax({
135
+ url:protocol + '//' + url + '/api/v1/' + vars[i].html_id + '/commit' ,
136
+ type: 'POST',
137
+ dataType: 'json',
138
+ data: {'content' : vars[i].content, 'image_flag': vars[i].image_flag},
139
+ success: function(response){
140
+
141
+ },
142
+ error : function(XMLHttpRequest, textStatus, errorThrown) {
143
+
144
+ }
145
+ });//AJAX
146
+ }//if
147
+ }
148
+ }
149
+
150
+
151
+
152
+
153
+
154
+ //非表示要素の取得と適用
155
+ $(window).on("load", function(e){
156
+ //非表示要素をまとめて取得
157
+ $.get({
158
+ url: protocol + '//' + url + '/api/v1/hiddens/get',
159
+ headers:{
160
+ 'Content-Type': 'application/json'
161
+ },
162
+ type: 'GET',
163
+ dataType: 'json',
164
+ data:JSON.stringify(),
165
+ success: function(response){
166
+ //セクションの数だけループ
167
+ $('#edit-iframe').contents().find("js-edit-window section").each(function(i){
168
+ //hidden要素の数だけループし、その中で一致するものがあれば非表示要素にチェンジ!
169
+ console.log('hidden');
170
+ for(h=0; h < response.length; h++)
171
+ {
172
+ if(($(this).attr('id')== response[h].hidden_html_id))
173
+ {
174
+ $(this).css('opacity', '0.2');
175
+ $(this).addClass('js-remove-element');
176
+ $(this).find('.js-section-hidden').remove();
177
+ $(this).before(`<label class="js-section-appear hidden-btn hidden-btn--appear">このエリアを表示する</label>`);
178
+ $(this).find('js-hidden-btn').remove();
179
+ $('iframe').contents().find($(this)).hide();
180
+ }
181
+ }
182
+ });
183
+ },
184
+
185
+ });
186
+ $('.js-edit-window').hide();
187
+ //sectionの数だけ非表示ボタンを付与
188
+
189
+ });
190
+ //非表示要素の取得と適用ここまで
191
+
192
+
193
+
194
+ //非表示決定ボタンとDBへのインサート
195
+ // iframeRef($('#edit-iframe')).on("input", function() {
196
+ // alert('test');
197
+ // }) ;
198
+ $('#edit-iframe').contents().find('.js-section-hidden').on('click',$('#edit-iframe').contents().find('.js-section-hidden'), function(e) {
199
+ alert('test');
200
+ });
201
+ $('#edit-iframe').contents().find('.js-section-hidden').on("input", function() {
202
+ alert('aaa');
203
+ //$(document).on('click', '.js-section-hidden', function hiddenSection(){
204
+ $(this).parent().css('opacity', '0.2');
205
+ //ソースをDBにインサート前に削除対象とするためにクラス付与
206
+ $(this).parent().addClass('js-remove-element');
207
+ hiddenAjax($(this).parent().attr('id'));
208
+ $(this).parent().before(`<label class="js-section-appear js-remove-element hidden-btn hidden-btn--appear">このエリアを表示する</label>`);
209
+ $(this).remove();
210
+ });
211
+
212
+ $(document).on('click', '.js-section-appear', function appearSection(){
213
+ $(this).next('section').css('opacity', '1');
214
+ //ソースをDBにインサート前に削除対象外とするためにクラス削除
215
+ $(this).next('section').removeClass('js-remove-element');
216
+ hiddenAjax($(this).next('section').attr('id'));
217
+ $(this).next('section').append('<label class="js-section-hidden js-remove-element hidden-btn">このエリアを非表示にする</label>');
218
+ $(this).remove();
219
+ });
220
+ //非表示決定ボタンとDBへのインサートここまで
221
+ //
222
+ //プレビュー or 編集モード切り替え
223
+ $(".js-hp-edit").on("click", function(){
224
+ state = $(this).attr('state');
225
+ if(state=="preview")
226
+ {
227
+ $('#edit-iframe').show();
228
+ $('#iframe').hide();
229
+ $('#edit-iframe').contents().find('head').append('<link rel="stylesheet" href="/sass/css/edit-iframe.css" type="text/css" />');
230
+ $('#edit-iframe').contents().find('head').append('<link rel="stylesheet" href="/sass/css/sections.css" type="text/css" />');
231
+ //$('#edit-iframe').contents().find('head').append('<script src="/js/HP/edit-iframe.js" type="text/javascript" />');
232
+ var iframe = $('#edit-iframe')[0];
233
+ iframe.contentDocument.body.contentEditable = true;
234
+ iframe.designMode = 'on';
235
+ $(this).html('編集中 <i class="fas fa-desktop"></i>(クリックで戻る)');
236
+ $(this).addClass('hp-edit__edit--now');
237
+ $(this).attr('state', 'edit');
238
+ $('#edit-iframe').contents().find('article').attr('id', 'sortable');
239
+ $('section').addClass('js-droppable');
240
+ $('.js-edit-submit').show();
241
+ $('.js-edit-section').show();
242
+ //非表示ラベルの付与
243
+ $('#edit-iframe').contents().find('section').each(function(i)
244
+ {
245
+ if($(this).attr('class')!='js-no-hidden')
246
+ {
247
+ $(this).append(`<label class="js-section-hidden js-remove-element hidden-btn" data="section-${i}">このエリアを非表示にする</label>`);
248
+ }
249
+ });
250
+
251
+ $(".js-add-section").each(function(i){
252
+ //hidden要素の数だけループし、その中で一致するものがあれば非表示要素にチェンジ!
253
+ $(this).addClass('js-remove-element');
254
+ $(this).before(`<label class="js-section-remove js-remove-element hidden-btn hidden-btn--appear">この要素を削除</label>`);
255
+ $(this).find('js-hidden-btn').remove();
256
+ $('iframe').contents().find($(this)).hide();
257
+ });
258
+ sortable();
259
+ }else{
260
+ $('.js-edit-window').hide();
261
+ $('.js-iframe').show();
262
+ $(this).html('このページを編集する <i class="far fa-desktop"></i>');
263
+ $(this).removeClass('hp-edit__edit--now');
264
+ $(this).attr('state', 'preview');
265
+ $('article').attr('id', '');
266
+ $('.js-edit-submit').hide();
267
+ $('.js-edit-section').hide();
268
+ //↓ プレビューに戻した際に変更点をアップデート
269
+ $(".js-remove-element").remove();
270
+ //window.updatePreview();
271
+ //window.updatePreviewImage();
272
+ //window.changeColor();
273
+ }
274
+ });
275
+
276
+ ```