質問編集履歴

1

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

2019/03/14 03:07

投稿

alberorana
alberorana

スコア52

test CHANGED
File without changes
test CHANGED
@@ -51,3 +51,501 @@
51
51
 
52
52
 
53
53
  この問題の解決策をご存知の方がいらっしゃいましたら、ご回答いただけますと幸いです。
54
+
55
+
56
+
57
+ 同じシステム(ドメイン)の /example/ID/ID をiframeのsrcにしています。
58
+
59
+
60
+
61
+ ```php
62
+
63
+ <!--{layout_header}-->
64
+
65
+ <link href="/sass/css/edit.css" rel="stylesheet">
66
+
67
+ <link href="/css/huebee.css" rel="stylesheet">
68
+
69
+ <script>
70
+
71
+ window.vars = [];
72
+
73
+ window.user = <?= $json_user ?> ;
74
+
75
+ window.templateId = <?= $user['template_id'] ?> ;
76
+
77
+ </script>
78
+
79
+ <!--{/layout_header}-->
80
+
81
+
82
+
83
+ <!--{layout_footer}-->
84
+
85
+ <script src="/plugins/jQueryUI/jquery-ui.min.js"></script>
86
+
87
+ <script src="/js/HP/edit.js"></script>
88
+
89
+
90
+
91
+
92
+
93
+ <a href="/view/<?= $this->current_user->id ?>" class="mb10" style="display: block;" target="_blank">実際のページを確認</a>
94
+
95
+
96
+
97
+ <button class="js-edit-submit save-button">保存 <br><i class="fas fa-save mt5"></i></button>
98
+
99
+ <button class="js-edit-section section-button">パーツ<br> 追加 <br><i class="fas fa-save mt5"></i></button>
100
+
101
+
102
+
103
+ <? if($source): ?>
104
+
105
+ <iframe src="/view/<?= $user['id'] ?>" width="99%" height="80%" class="js-iframe" id='iframe' device="pc"></iframe>
106
+
107
+ <iframe src="/view/<?= $user['id'] ?>" width="99%" height="80%" id='edit-iframe' device="pc" contenteditable="true"></iframe>
108
+
109
+ <? else: ?>
110
+
111
+ <iframe src="/example/<?= $user['template_id'] ?>/<?= $user['id'] ?>" width="99%" height="80%" class="js-iframe" id='iframe'device="pc"></iframe>
112
+
113
+ <iframe src="/example/<?= $user['template_id'] ?>/<?= $user['id'] ?>" width="99%" height="80%" id='edit-iframe' device="pc" contenteditable="true"></iframe>
114
+
115
+ <? endif; ?>
116
+
117
+ ```
118
+
119
+ ```edit.js
120
+
121
+ var protocol = location.protocol;
122
+
123
+ var url = location.host;
124
+
125
+ window.editingDom = null;
126
+
127
+
128
+
129
+ //要素の中身変更
130
+
131
+ $(window).on("load", function(){
132
+
133
+ $('#edit-iframe').contents().find('head').append('<link rel="stylesheet" href="/sass/css/edit-iframe.css" type="text/css" />');
134
+
135
+ //保存ボタン
136
+
137
+ $(document).on("click", ".js-edit-submit", function () {
138
+
139
+ //画像変更だけした場合の対策
140
+
141
+ //初回に文言だけ変更して保存した場合の分岐
142
+
143
+ console.log(window.editingDom);
144
+
145
+ if(window.editingDom)
146
+
147
+ {
148
+
149
+ if(window.editingDom.prop('tagName') != 'img')
150
+
151
+ {
152
+
153
+ var childVar= {'html_id': window.editingDom.attr('id'), 'content': window.editingDom.html(), 'image_flag': '0'};
154
+
155
+ console.log('childVar');
156
+
157
+ console.log(childVar);
158
+
159
+ window.vars.push(childVar);
160
+
161
+ }
162
+
163
+ }
164
+
165
+ //編集のための要素を削除してから、本番ソースにする
166
+
167
+ $('#edit-iframe').contents().find(".js-remove-element").remove();
168
+
169
+ $('#edit-iframe').contents().find(".is-hidden").remove();
170
+
171
+ $('#edit-iframe').contents().find(".js-section-hidden").each(function(i){
172
+
173
+ $(this).remove();
174
+
175
+ });
176
+
177
+ $('#edit-iframe').contents().find(".js-section-appear").each(function(i){
178
+
179
+ $(this).remove();
180
+
181
+ });
182
+
183
+ //bodyのcontenteditableを削除
184
+
185
+ var iframe = $('#edit-iframe')[0];
186
+
187
+ iframe.contentDocument.body.contentEditable = false;
188
+
189
+ iframe.designMode = 'off';
190
+
191
+
192
+
193
+ console.log($('#edit-iframe').contents().find('html').html());
194
+
195
+ commit($('#edit-iframe').contents().find('html').html(), window.vars);
196
+
197
+
198
+
199
+ //以下編集を解除する処理
200
+
201
+ unEdit();
202
+
203
+ //updatePreview();
204
+
205
+ //window.updatePreviewImage();
206
+
207
+ window.vars = [];
208
+
209
+ alert('保存が完了しました。');
210
+
211
+ location.reload();
212
+
213
+ });
214
+
215
+
216
+
217
+ function commit(source,vars=null)
218
+
219
+ {
220
+
221
+ //source = source.replace( /このエリアを非表示にする/g , "" ) ;
222
+
223
+ //ソースコード自体を保存する処理
224
+
225
+ console.log(source);
226
+
227
+ //return false;
228
+
229
+ $.ajax({
230
+
231
+ url:protocol + '//' + url + '/api/v1/source/commit' ,
232
+
233
+ type: 'POST',
234
+
235
+ dataType: 'json',
236
+
237
+ data: {'source' : source},
238
+
239
+ success: function(response){
240
+
241
+
242
+
243
+ },
244
+
245
+ error : function(XMLHttpRequest, textStatus, errorThrown) {
246
+
247
+
248
+
249
+ }
250
+
251
+ });//AJAX
252
+
253
+ //文言と画像をまとめてインサート
254
+
255
+ var i;
256
+
257
+ for(i=0;i<vars.length;i++)
258
+
259
+ {
260
+
261
+ console.log(vars[i]);
262
+
263
+ if(vars[i].content && vars[i].html_id)
264
+
265
+ {
266
+
267
+ $.ajax({
268
+
269
+ url:protocol + '//' + url + '/api/v1/' + vars[i].html_id + '/commit' ,
270
+
271
+ type: 'POST',
272
+
273
+ dataType: 'json',
274
+
275
+ data: {'content' : vars[i].content, 'image_flag': vars[i].image_flag},
276
+
277
+ success: function(response){
278
+
279
+
280
+
281
+ },
282
+
283
+ error : function(XMLHttpRequest, textStatus, errorThrown) {
284
+
285
+
286
+
287
+ }
288
+
289
+ });//AJAX
290
+
291
+ }//if
292
+
293
+ }
294
+
295
+ }
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+ //非表示要素の取得と適用
308
+
309
+ $(window).on("load", function(e){
310
+
311
+ //非表示要素をまとめて取得
312
+
313
+ $.get({
314
+
315
+ url: protocol + '//' + url + '/api/v1/hiddens/get',
316
+
317
+ headers:{
318
+
319
+ 'Content-Type': 'application/json'
320
+
321
+ },
322
+
323
+ type: 'GET',
324
+
325
+ dataType: 'json',
326
+
327
+ data:JSON.stringify(),
328
+
329
+ success: function(response){
330
+
331
+ //セクションの数だけループ
332
+
333
+ $('#edit-iframe').contents().find("js-edit-window section").each(function(i){
334
+
335
+ //hidden要素の数だけループし、その中で一致するものがあれば非表示要素にチェンジ!
336
+
337
+ console.log('hidden');
338
+
339
+ for(h=0; h < response.length; h++)
340
+
341
+ {
342
+
343
+ if(($(this).attr('id')== response[h].hidden_html_id))
344
+
345
+ {
346
+
347
+ $(this).css('opacity', '0.2');
348
+
349
+ $(this).addClass('js-remove-element');
350
+
351
+ $(this).find('.js-section-hidden').remove();
352
+
353
+ $(this).before(`<label class="js-section-appear hidden-btn hidden-btn--appear">このエリアを表示する</label>`);
354
+
355
+ $(this).find('js-hidden-btn').remove();
356
+
357
+ $('iframe').contents().find($(this)).hide();
358
+
359
+ }
360
+
361
+ }
362
+
363
+ });
364
+
365
+ },
366
+
367
+
368
+
369
+ });
370
+
371
+ $('.js-edit-window').hide();
372
+
373
+ //sectionの数だけ非表示ボタンを付与
374
+
375
+
376
+
377
+ });
378
+
379
+ //非表示要素の取得と適用ここまで
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+ //非表示決定ボタンとDBへのインサート
388
+
389
+ // iframeRef($('#edit-iframe')).on("input", function() {
390
+
391
+ // alert('test');
392
+
393
+ // }) ;
394
+
395
+ $('#edit-iframe').contents().find('.js-section-hidden').on('click',$('#edit-iframe').contents().find('.js-section-hidden'), function(e) {
396
+
397
+ alert('test');
398
+
399
+ });
400
+
401
+ $('#edit-iframe').contents().find('.js-section-hidden').on("input", function() {
402
+
403
+ alert('aaa');
404
+
405
+ //$(document).on('click', '.js-section-hidden', function hiddenSection(){
406
+
407
+ $(this).parent().css('opacity', '0.2');
408
+
409
+ //ソースをDBにインサート前に削除対象とするためにクラス付与
410
+
411
+ $(this).parent().addClass('js-remove-element');
412
+
413
+ hiddenAjax($(this).parent().attr('id'));
414
+
415
+ $(this).parent().before(`<label class="js-section-appear js-remove-element hidden-btn hidden-btn--appear">このエリアを表示する</label>`);
416
+
417
+ $(this).remove();
418
+
419
+ });
420
+
421
+
422
+
423
+ $(document).on('click', '.js-section-appear', function appearSection(){
424
+
425
+ $(this).next('section').css('opacity', '1');
426
+
427
+ //ソースをDBにインサート前に削除対象外とするためにクラス削除
428
+
429
+ $(this).next('section').removeClass('js-remove-element');
430
+
431
+ hiddenAjax($(this).next('section').attr('id'));
432
+
433
+ $(this).next('section').append('<label class="js-section-hidden js-remove-element hidden-btn">このエリアを非表示にする</label>');
434
+
435
+ $(this).remove();
436
+
437
+ });
438
+
439
+ //非表示決定ボタンとDBへのインサートここまで
440
+
441
+ //
442
+
443
+ //プレビュー or 編集モード切り替え
444
+
445
+ $(".js-hp-edit").on("click", function(){
446
+
447
+ state = $(this).attr('state');
448
+
449
+ if(state=="preview")
450
+
451
+ {
452
+
453
+ $('#edit-iframe').show();
454
+
455
+ $('#iframe').hide();
456
+
457
+ $('#edit-iframe').contents().find('head').append('<link rel="stylesheet" href="/sass/css/edit-iframe.css" type="text/css" />');
458
+
459
+ $('#edit-iframe').contents().find('head').append('<link rel="stylesheet" href="/sass/css/sections.css" type="text/css" />');
460
+
461
+ //$('#edit-iframe').contents().find('head').append('<script src="/js/HP/edit-iframe.js" type="text/javascript" />');
462
+
463
+ var iframe = $('#edit-iframe')[0];
464
+
465
+ iframe.contentDocument.body.contentEditable = true;
466
+
467
+ iframe.designMode = 'on';
468
+
469
+ $(this).html('編集中 <i class="fas fa-desktop"></i>(クリックで戻る)');
470
+
471
+ $(this).addClass('hp-edit__edit--now');
472
+
473
+ $(this).attr('state', 'edit');
474
+
475
+ $('#edit-iframe').contents().find('article').attr('id', 'sortable');
476
+
477
+ $('section').addClass('js-droppable');
478
+
479
+ $('.js-edit-submit').show();
480
+
481
+ $('.js-edit-section').show();
482
+
483
+ //非表示ラベルの付与
484
+
485
+ $('#edit-iframe').contents().find('section').each(function(i)
486
+
487
+ {
488
+
489
+ if($(this).attr('class')!='js-no-hidden')
490
+
491
+ {
492
+
493
+ $(this).append(`<label class="js-section-hidden js-remove-element hidden-btn" data="section-${i}">このエリアを非表示にする</label>`);
494
+
495
+ }
496
+
497
+ });
498
+
499
+
500
+
501
+ $(".js-add-section").each(function(i){
502
+
503
+ //hidden要素の数だけループし、その中で一致するものがあれば非表示要素にチェンジ!
504
+
505
+ $(this).addClass('js-remove-element');
506
+
507
+ $(this).before(`<label class="js-section-remove js-remove-element hidden-btn hidden-btn--appear">この要素を削除</label>`);
508
+
509
+ $(this).find('js-hidden-btn').remove();
510
+
511
+ $('iframe').contents().find($(this)).hide();
512
+
513
+ });
514
+
515
+ sortable();
516
+
517
+ }else{
518
+
519
+ $('.js-edit-window').hide();
520
+
521
+ $('.js-iframe').show();
522
+
523
+ $(this).html('このページを編集する <i class="far fa-desktop"></i>');
524
+
525
+ $(this).removeClass('hp-edit__edit--now');
526
+
527
+ $(this).attr('state', 'preview');
528
+
529
+ $('article').attr('id', '');
530
+
531
+ $('.js-edit-submit').hide();
532
+
533
+ $('.js-edit-section').hide();
534
+
535
+ //↓ プレビューに戻した際に変更点をアップデート
536
+
537
+ $(".js-remove-element").remove();
538
+
539
+ //window.updatePreview();
540
+
541
+ //window.updatePreviewImage();
542
+
543
+ //window.changeColor();
544
+
545
+ }
546
+
547
+ });
548
+
549
+
550
+
551
+ ```