This repository has been archived by the owner on Nov 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #596 from liqd/mn-2019-02-add-js
add js files
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* globals $ django */ | ||
$(function () { | ||
var modalHTML = ( | ||
'<div class="modal" tabindex="-1" role="dialog">' + | ||
'<div class="modal-dialog modal-lg" role="document">' + | ||
'<div class="modal-content">' + | ||
'<div class="modal-header"><h2 class="modal-title u-first-heading"></h2>' + | ||
'<button type="button" class="close" data-dismiss="modal" aria-label="' + django.gettext('Close') + '"><span aria-hidden="true">×</span></button>' + | ||
'</div>' + | ||
'<div class="modal-body"></div>' + | ||
' </div>' + | ||
'</div>' + | ||
'</div>' | ||
) | ||
|
||
var extractScripts = function ($root, selector, attr) { | ||
var $existingValues = $('head').find(selector).map(function (i, e) { | ||
return $(e).attr(attr) | ||
}) | ||
|
||
$root.find(selector).each(function (i, script) { | ||
var $script = $(script) | ||
var $matches = $existingValues.filter(function (i, v) { | ||
return v === $script.attr(attr) | ||
}) | ||
if ($matches.length === 0) { | ||
$('head').append($script) | ||
} | ||
}) | ||
} | ||
|
||
$(document).on('click', '[data-toggle="ajax-modal"]', function (e) { | ||
e.preventDefault() | ||
var target = this.href + ' ' + this.dataset.targetSelector | ||
var $newModal = $(modalHTML) | ||
var _this = this | ||
|
||
$newModal.on('hidden.bs.modal', function () { | ||
$newModal.remove() | ||
}) | ||
|
||
$newModal.find('.modal-body').load(target, function (html) { | ||
var $root = $('<div>').html(html) | ||
var title = $root.find('h1').text() | ||
$newModal.find('.modal-title').text(title) | ||
$newModal.attr('aria-label', title) | ||
extractScripts($root, 'script[src]', 'src') | ||
extractScripts($root, 'link[rel="stylesheet"]', 'href') | ||
|
||
$newModal.insertAfter(_this) | ||
$newModal.modal() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* global $ CKEDITOR django */ | ||
|
||
$(function () { | ||
var submitted = false | ||
var changeHandler = function () { | ||
$(window).on('beforeunload', function (e) { | ||
if (!submitted) { | ||
var string = django.gettext('If you leave this page changes you made will not be saved.') | ||
e.returnValue = string | ||
return string | ||
} | ||
}) | ||
} | ||
|
||
if (window.CKEDITOR) { | ||
CKEDITOR.on('instanceReady', function (e) { | ||
e.editor.on('change', changeHandler) | ||
}) | ||
} | ||
|
||
$(document).one('change', changeHandler) | ||
.on('submit', function (e) { | ||
if ($(e.target).data('ignore-submit') === true) { | ||
return true | ||
} | ||
submitted = true | ||
}) | ||
}) |