# this var holds whatever HTML has been parked
# to make room for a loader
#
# cloneObject will make a copy of an object - not a copy of the reference
# to some object!
#
# var obj1= {bla:'blabla',foo:'foofoo',etc:'etc'};
# var obj2= new cloneObject(obj1);
#
# 03-07-2015 (whd) not sure whether this method is OK!!!!
# cp'ed from: http://scriptcult.com/subcategory_1/article_414-copy-or-clone-javascript-array-object
#
@cloneObject = (source) ->
for i in source
if typeof source[i] == 'source'
this[i] = new cloneObject source[i]
else
this[i] = source[i]
# it will be re-instated with releaseLoader
loaded_html = ""
loaded_element = ""
#
# set a loader - see http://materializecss.com/preloader.html
# for examples
#
@setLoader = (elem,html) ->
html ||= '
'
loaded_element = $(elem)
loaded_html = elem.html()
elem.html(html)
#
# release the loader
#
@releaseLoader = (elem=null) ->
elem ||= loaded_element
$(elem).html(loaded_html)
#
# make sure fields with values do not have their labels obscuring your view
#
@setLabels = (selector) ->
$(selector).each () ->
try
fld = '#'+$(this).attr('for')
$(this).addClass('active') unless $(fld)[0].value.nil?
catch
#console.log this
@tellResponse = (msg,anchor='.message_container',fade=15000,selector='.alert') ->
$(anchor).prepend(msg)
if fade>0
fadeItOut $(anchor).find(selector), 15000
@reportError = (msg) ->
Materialize.toast( msg, 4500, 'red lighten-3')
@closeSweetAlert = ($elem=null) ->
try
unless $elem == undefined
$elem.show()
swal.close()
catch error
$('.sweet-overlay').hide()
$('.sweet-alert').hide()
#
# initializeSweetAlert
# initializes the sweetalert prompt
#
@initializeSweetAlert = () ->
try
# console.log 'sweet alert initializing...'
# sweetHTML = ''
# sweetWrap = document.createElement('div')
# sweetWrap.innerHTML = sweetHTML
# $(document.body).append(sweetWrap)
# console.log 'sweetalert initialized!'
catch error
console.log 'sweetalert says: ' + error
#
# handleAttachLinks
# allows for attaching/detaching resources from their parents - like: /admin/users/2/printers/3/attach.js
#
@handleAttachLinks = (e) ->
e.preventDefault()
e.stopPropagation()
$elem = $(e.currentTarget)
request = $.ajax
url: $elem.attr('href'),
type: "get",
dataType: 'html'
.done (data) ->
$( $elem[0].parentElement).html(data)
.error (data) ->
swal("Oops", "We couldn't connect to the server!", "error")
return false
#
# handleActivateLinks
# allows for activating/deactivating resources - like: /admin/users/2/activate
#
@handleActivateLinks = (e) ->
e.preventDefault()
e.stopPropagation()
$elem = $(e.currentTarget)
request = $.ajax
url: $elem.attr('href'),
type: "get",
dataType: 'html'
.done (data) ->
$( $elem[0].parentElement).html(data)
.error (data) ->
swal("Oops", "We couldn't connect to the server!", "error")
return false
#
# handlePreferredLinks
# allows for activating/deactivating resources - like: /admin/users/2/activate
#
@handlePreferredLinks = (e) ->
e.preventDefault()
e.stopPropagation()
$elem = $(e.currentTarget)
request = $.ajax
url: $elem.attr('href'),
type: "get",
dataType: 'html'
.done (data) ->
# here we have to write all the existing preferred ones
$.when $('i.preferred').each (k,e) ->
$e = $(e)
$( $e[0].parentElement).html( data.replace /{{id}}/, $e.data('id'))
# $( $elem[0].parentElement).html(data)
.then $( $elem[0].parentElement).html('bookmark')
.error (data) ->
swal("Oops", "We couldn't connect to the server!", "error")
return false
#
# handlePrintLinks
# initializes the tags classed with '.print_post_link' to print a post
#
@handlePrintLinks = (e) ->
e.preventDefault()
e.stopPropagation()
$elem = $(e.currentTarget)
loader = ''
loaded_element = $elem.closest('.loader_container')
setLoader( loaded_element,loader )
printPost($elem)
return false
#
# handleDeleteLinks
# initializes the tags classed with '.delete_link' to verify deleting an issue
#
@handleDeleteLinks = (e) ->
e.preventDefault()
e.stopPropagation()
$elem = $(e.currentTarget)
$elem.hide()
$('.sweet-overlay').show()
swal
title: "Are you sure?",
text: "Are you sure that you want to delete this photo?",
type: "warning",
animation: "slide-from-bottom",
showLoaderOnConfirm: true,
showCancelButton: true,
closeOnConfirm: false,
confirmButtonText: "Yes, delete it!",
confirmButtonColor: "#ec6c62",
(confirmed) ->
if !confirmed
closeSweetAlert($elem)
else
deletePost($elem)
return false
#
# starting a search - and making some noise about it!
#
@searchKey = (e) ->
if e.keyCode == 13
e.preventDefault()
e.stopPropagation()
$elem = $('input.search-list')
orderSearch(inputForm.action, $elem.val())
#
# starting a search - and making some noise about it!
#
@searchClick = (e) ->
e.preventDefault()
e.stopPropagation()
$elem = $('input.search-list')
orderSearch(inputForm.action, $elem.val())
inputForm = null
#
# initializeForm
# on pages with a FORM element - like show, new and edit
# prepare the FORM element
#
@initializeForm = (f) ->
inputForm = f
#
# Initialize SELECT's
#
$('select').each () ->
# remove span.caret's from previous 'runs' - however that happens
$(this).parent().parent().find('span.caret').remove()
$(this).material_select()
#
# make labels on fields with content move out of the way
#
setLabels('.input-field label')
$(document.body).on 'keydown.search', 'input.search-list', searchKey
$(document.body).on 'click.search', 'form a.search-list[type="submit"]', searchClick
#
# Initialize INPUT TYPE='DATE'
#
# %input.datepicker{ type:"date" }
#
$('.datepicker').pickadate
selectMonths: true, # Creates a dropdown to control month
selectYears: 15 # Creates a dropdown of 15 years to control year
#
# setup a dropbox file upload
#
@prepareUpload = (e) ->
if $('.dropbox').size() > 0
medium = new Medium()
medium.initializeBindings()
#
# handleFABLinks
# handles click on the fab_button
#
@handleFABLinks = (e) ->
e.preventDefault()
e.stopPropagation()
elem = $(e.currentTarget)
if elem.data('browser')=='new'
window.open elem.data('url')
return
loader = ''
setLoader( $(elem).closest('.fixed-action-btn'),loader)
oxremote = elem.data('oxremote')
if (oxremote=='false' or oxremote==false)
switch elem.data('method')
when 'post', 'POST'
# elem.closest('form').submit()
$('form').submit()
when 'put', 'PUT'
console.log 'how do I put? add code in abstracted/app/assets/javascripts/initializers.js.coffee'
when 'get', 'GET'
window.location.href = elem.data('url')
else
jqxhr = $.ajax
url: elem.data('url') || elem.attr('href')
type: elem.data('method') || 'get'
data: dataArgumentOn(elem)
dataType: elem.data('type') || 'html'
.done (data) ->
releaseLoader()
if elem.data('modal')
$(elem.data('modal')).html(data).openModal()
else
$(document.body).append(data)
.error (data) ->
releaseLoader()
$(document.body).append(data)
return false
#
# fadeItOut will fade an element out with a preset or
# supplied delay
#
@fadeItOut = (e,delay=3500) ->
$(e).delay( delay ).fadeOut( 1000 )
#
# dataArgumentOn constructs the data: argument on AJAX calls
# from what ever data- attributes an element holds
#
# excemptions: data-id, data-remote, data-ajax, data-method, data-type and data-url
#
@dataArgumentOn = (elem) ->
$(elem).data()
#
# closeNotice
# will close the notice DIV
@closeNotice = (e) ->
fadeItOut $(e).closest('.alert') #.remove()
#
# PageOnLoad loads and initializes
# fixed elements like SELECT's, Materialized's elements, et al.
#
@pageOnLoad = () ->
# fileApiSupportCheck()
#
# make sure all eventhandlers are gone!
$(document.body).unbind('click.close_notice')
$(document.body).unbind('click.print')
$(document.body).unbind('click.delete')
$(document.body).unbind('click.fab')
$(document.body).unbind('click.attach')
$(document.body).unbind('click.activate')
$(document.body).unbind('click.prefer')
$(document.body).unbind('keydown.search')
$(document.body).unbind('click.search')
#
# Initialize the 'hamburger'
#
$(".button-collapse").sideNav();
#
# Prepare close-notice's for acting on clicks to remove div
#
$(document.body).on 'click.close_notice', 'a.close-notice', closeNotice
#
# Prepare print_link's
#
$(document.body).on 'click.print', 'a.print_post_link, a.print_item', handlePrintLinks
#
# Prepare delete_link's for acting on clicks to delete posts
#
$(document.body).on 'click.delete', 'a.delete_link, a.delete_item', handleDeleteLinks
#
# Add event on the FAB (fixed action button)
#
$(document.body).on 'click.fab', 'a.fab-button', handleFABLinks
#
# else
# $form = $(e.currentTarget).closest('form')
# data = $form.serialize()
# jqxhr = $.ajax
# url: $(e.currentTarget).data('url'),
# type: $(e.currentTarget).data('method'),
# data: data
# .done (response) ->
# $('body').html(response)
# .fail (response) ->
# console.log 'øv!'
#
# Initialize collapsible (uncomment the line below if you use the dropdown variation)
#
$('.collapsible').collapsible
accordion : true # A setting that changes the collapsible behavior to expandable instead of the default accordion style
#
# If this page has FORM element(s)
#
$('form').first().map (k,f) ->
initializeForm(f)
prepareUpload(f)
#
# If this page has a resources_list
#
$('table.resources_table').map (k,t) ->
console.log 'table ' + k
$(document.body).on 'click.attach', 'a.attached, a.detached', handleAttachLinks
$(document.body).on 'click.activate', 'a.activated, a.deactivated', handleActivateLinks
$(document.body).on 'click.prefer', 'a.preferred, a.deferred', handlePreferredLinks
scrollTable(t)
#
# Try to keep users from double-clicking submit's
#
# document.addEventListener('DOMContentLoaded', disableMultipleSubmits, false);
#
# make drop-downs react nicely
#
$(".dropdown-button").dropdown()
#
# prepare for tabbed display of tabbed UL's
#
$('ul.tabs').tabs()
$('.materialboxed').materialbox()
try
if ($('.sweet-alert').length<1)
initializeSweetAlert()
if ($('.sweet-alert').length>0)
console.log 'sweet-alert initialized correctly!' #swal( 'pageOnLoad', 'pageOnLoad blev kaldt!', 'success')
catch error
console.log 'autch!'
console.log error
# try
#
# swal( 'pageOnLoad', 'pageOnLoad blev kaldt!', 'success')
#
# catch error
# console.log 'ok - giving up'
# console.log 'page loaded!'
#
# check to see iff this browser supperts the File APIs
#
@fileApiSupportCheck = () ->
if (window.File && window.FileReader && window.FileList && window.Blob)
# All the File APIs are supported.
console.log 'file APIs supported '
else
document.getElementById('message_container').innerHTML = 'This browser does not support this application fully! Use latest Chrome - or advance cautiously!
';
#
# PageOnChange really just calls a pageload - as of now 19-06-15
# fixed elements like SELECT's, Materialized's elements, et al.
#
# @pageOnChange = () ->
# console.log 'page changed '
# pageOnLoad()