Skip to content
This repository has been archived by the owner on Nov 7, 2019. It is now read-only.

Commit

Permalink
Merge pull request #596 from liqd/mn-2019-02-add-js
Browse files Browse the repository at this point in the history
add js files
  • Loading branch information
Magdalena Noffke committed Feb 28, 2019
2 parents 0bb320a + c6bc58d commit c6ce2d8
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
54 changes: 54 additions & 0 deletions liqd_product/apps/contrib/static/js/ajax_modal.js
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">&times;</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()
})
})
})
28 changes: 28 additions & 0 deletions liqd_product/apps/contrib/static/js/unload_warning.js
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
})
})

0 comments on commit c6ce2d8

Please sign in to comment.