'use strict' _.templateSettings.interpolate = /{{([\s\S]+?)}}/g; angular.module('Engine2', ['ngRoute', 'ngSanitize', 'ngAnimate', 'ngCookies', 'mgcrea.ngStrap', 'ngFileUpload', 'ui.tree', 'LocalStorageModule']) # 'draggabilly' .factory 'E2Snippets', -> icon = (name) -> "" aicon = (name) -> "" ng_class_names = ['active', 'enabled', 'disabled'] icon: icon aicon: aicon boolean_true_value: icon('check') boolean_false_value: icon('unchecked') make_ng_class: (o) -> out = [] _.each ng_class_names, (e) -> out.push(if e == 'enabled' then "'disabled': !(#{o[e]})" else "'#{e}': #{o[e]}") if o[e]? if out.length > 0 then "ng-class=\"{#{out.join(',')}}\"" else "" .config ($httpProvider, $routeProvider, $compileProvider, localStorageServiceProvider, $logProvider) -> loaderOn = -> angular.element(document.querySelectorAll('.loader')).eq(-1).css("visibility", 'visible') $httpProvider.interceptors.push 'e2HttpInterceptor' $httpProvider.defaults.transformRequest.push (data, headersGetter) -> loaderOn() data # $httpProvider.defaults.headers.common['Cache-Control'] = 'no-cache' # $httpProvider.defaults.cache = false; $httpProvider.defaults.headers.get ||= {} # if !$httpProvider.defaults.headers.get $httpProvider.defaults.headers.get['If-Modified-Since'] = '0' # localStorageServiceProvider.setStorageType('sessionStorage') localStorageServiceProvider.setPrefix('E2') $compileProvider.debugInfoEnabled(false) $logProvider.debugEnabled(true) $httpProvider.useApplyAsync(true) # $locationProvider.html5Mode(true); .factory 'e2HttpInterceptor', ($q, $injector, E2Snippets) -> loaderOff = -> angular.element(document.querySelectorAll('.loader')).eq(-1).css("visibility", 'hidden') response: (response) -> loaderOff() response responseError: (response) -> loaderOff() if response.status != 401 response = (status: response.status, statusText: "Connection refused", data: (message: "Connection refused")) if response.status in [-1, 0] if response.constructor == SyntaxError message = response.message cause = response.stack else message = response.data.message cause = if _.isString(response.data) then response.data else response.data.cause || response.data.message $injector.get('$e2Modal').error("#{E2Snippets.icon('bell')} #{response.status}: #{message}", cause) $q.reject(response) .factory 'E2', ($templateCache, $http, E2Snippets, $e2Modal, $q, $injector, e2HttpInterceptor, $route, $dateFormatter) -> compact: (o) -> _.each o, (v, k) => if (v == null || (_.isString(v) && !v)) || (!_.isDate(v) && _.isObject(v) && @compact(v) && _.isEmpty(v)) delete o[k] clean: (o) -> _.each o, (v, k) => if _.isArray(v) v.length = 0 # o[k] = [] else if _.isObject(v) && !_.isDate(v) @clean(v) else # delete o[k] o[k] = null merge: (dst, src) -> for k, v of src if _.isObject(v) && !_.isArray(v) if k.slice(-1) == '!' dst[k.slice(0, -1)] = v else dst[k] = @merge(dst[k] ? {}, v) else dst[k] = v dst transpose: (a) -> _.keys(a[0]).map((c) -> a.map (r) -> r[c]) join_keys: (keys) -> keys.join('|') split_keys: (key) -> key.split('|') id_for: (rec, meta) -> @join_keys(meta.primary_fields.map((e) -> rec[e])) from_id: (id, meta) -> _.zipObject(meta.primary_fields, id) # _.zip(meta.primary_fields, id).reduce(((rec, [k, v]) -> rec[k] = v; rec), {}) icon: E2Snippets.icon aicon: E2Snippets.aicon fetch_template: (template) -> $q.when($templateCache.get(template) || $http.get(template)).then (res) -> if angular.isObject(res) $templateCache.put(template, res.data) res.data else res fetch_panel: (panel, modal) -> $q.when(panel.template_string || @fetch_template(panel.template)).then (template) => if panel.panel_template prefix = if modal then 'modals' else 'panels' @fetch_template("#{prefix}/#{panel.panel_template}").then (panel_template) -> panel_template.replace("modal-content-to-replace", template) else template each_menu: (menu, fun) -> _.each menu, (m) => if m.menu @each_menu(m.menu.entries, fun) else if !m.divider fun(m) process_menu: (action, menu_name) -> processor = @menu_processors[menu_name] @each_menu action.meta.menus[menu_name].entries, (m) -> fun_name = "#{menu_name}_#{m.name}" fun_invoke = "action['#{fun_name}'](#{processor.arg_name})" fun_invoke = "(#{m.enabled}) && #{fun_invoke}" if m.enabled? fun_invoke = "!(#{m.disabled}) && #{fun_invoke}" if m.disabled? click = m.click m.click = fun_invoke if click action[fun_name] = (args...) -> processor.arg_fun(action, args...) action.scope().$eval(click) else action[fun_name] ?= (args...) -> processor.arg_fun(action, args...) action.invoke_action(m.name, processor.arg_ret(action)) if action.find_action_info(m.name, false)? show = if m.show then " && " + m.show else "" m.show = "action.find_action_info('#{m.name}').access" + show menu_processors: menu: arg_name: '' arg_fun: (action) -> undefined arg_ret: (action) -> undefined panel_menu: arg_name: '' arg_fun: (action) -> undefined arg_ret: (action) -> undefined item_menu: arg_name: '$index' arg_ret: (action) -> id: action.current_id arg_fun: (action, index) -> action.current_id = $injector.get('E2').id_for(action.entries[index], action.meta) renderers: boolean: (value, render) => switch value when render.true_value then E2Snippets.boolean_true_value when render.false_value then E2Snippets.boolean_false_value else "?" list_select: (value, render) -> render.list_hash ||= render.list.reduce(((h, [a, b]) -> h[a] = b; h), {}) render.list_hash[value] ? value datetime: (value, render) -> value.split('\.')[0] # $dateFormatter.formatDate(value, "yyyy-MM-dd", $dateFormatter.getDefaultLocale()) integer: (value, render) -> # ? value.toString() decimal_date: (value, render) -> value = value.toString() if value.length == 8 && match = value.match(/^(\d{4})(\d{2})(\d{2})$/) then match.slice(1, 4).join('-') else value # if value.length == 8 then $dateFormatter.formatDate(value, "yyyy-MM-dd", $dateFormatter.getDefaultLocale()) else value decimal_time: (value, render) -> value = value.toString() if match = value.match(/^(\d{1,2}?)(\d{2})(\d{2})$/) then match.slice(1, 4).join(':') else value string: (value, render) -> value.toString() render_field: (entry, name, meta) -> value = entry[name] if value? && info = meta.info f_info = info[name] if f_info? && type = f_info.type @renderers[type](value, f_info.render) else (value + "").replace(/&/g, "&").replace(//g, ">") else value parsers: integer: (value, info) -> parseInt(value) parse_entry: (value, info) -> parser = @parsers[info.type] if parser then parser(value, info) else value .provider '$e2Modal', -> $get: ($rootScope, $modal, $timeout, $window, $injector) -> class MManager @Z_INDEX: 1050 @index: 0 constructor: () -> backdrop_z_index: (num, index) -> angular.element(document.querySelectorAll('.modal-backdrop')).eq(num).css('z-index', index) modal_num: (num) -> angular.element(document.querySelectorAll('.modal')).eq(num) backdrop: -> 'static' show_before: -> @z_index = MManager.Z_INDEX + MManager.index * 2 @modal.css('z-index', @z_index + 1) hide_before: -> @z_index = MManager.Z_INDEX + ((MManager.index - 1) * 2) show: -> hide: -> angular.element($window.document.body).addClass('modal-open modal-with-am-fade') if MManager.index > 0 class DefaultMManager extends MManager show_before: -> super() @backdrop.css('z-index', @z_index) # @backdrop_z_index(-MManager.index - 1, z_index) class FirstMManager extends MManager constructor: () -> super() @threshold = 2 backdrop: -> if MManager.index > @threshold then false else super() show_before: -> super() if MManager.index > @threshold @modal_num((MManager.index - 1) - @threshold).css('display', 'none') @backdrop_z_index(0, @z_index) else @backdrop.css('z-index', @z_index) # @backdrop_z_index(-MManager.index - 1, z_index) hide_before: -> super() if MManager.index > @threshold @backdrop_z_index(0, @z_index) @modal_num((MManager.index - 1) - @threshold).css('display', 'block') class SingleBackdropMManager extends MManager backdrop: -> if MManager.index > 0 then false else super() show_before: -> super() @backdrop_z_index(0, @z_index) hide_before: -> super() @backdrop_z_index(0, @z_index) is_modal: -> MManager.index > 0 show: (action) -> scope = if action.scope then action.scope().$new(true) else $rootScope.$new() scope.action = action manager = new SingleBackdropMManager() scope.$on 'modal.show.before', (e, m) -> e.stopPropagation() manager.modal = m.$element manager.backdrop = m.$backdrop throw "Modal has element" if scope.action.element?() scope.action.element = -> m.$element scope.action.panel_show?() manager.show_before() MManager.index++ scope.$on 'modal.show', (e, m) -> e.stopPropagation() manager.show() scope.action.panel_shown?() scope.$on 'modal.hide.before', (e) -> e.stopPropagation() MManager.index-- manager.hide_before() scope.action.panel_hide?() scope.$on 'modal.hide', (e) -> e.stopPropagation() manager.hide() scope.action.panel_hidden?() scope.$destroy() $injector.get('E2').fetch_panel(scope.action.meta.panel, true).then (template) -> # template = "