views/engine2actions.coffee in engine2-1.0.2 vs views/engine2actions.coffee in engine2-1.0.3

- old
+ new

@@ -17,13 +17,14 @@ bootstrap_action_off = $scope.$on "bootstrap_action", (evt, action) -> bootstrap_action_off() create(action) else $http.get("api/meta").then (mresponse) -> $scope.$broadcast "bootstrap_action", - $scope.action = new E2Actions.default_action(mresponse.data, $scope, null, $element, action_resource: 'api') + $scope.action = new E2Actions.root(mresponse.data, $scope, null, $element, action_resource: 'api') .factory 'E2Actions', (E2, $http, $timeout, $e2Modal, $injector, $compile, $templateCache, $q, localStorageService, $route, $window, $rootScope, $location) -> + globals = {} action: class Action constructor: (response, scope, parent, element, action_info) -> @find_action_info = (name, raise = true) -> act = response.actions[name] throw "Undefined action '#{name}' for action #{@action_info().name} (under #{@parent()?.action_info().action_resource})" if raise && !act @@ -45,18 +46,16 @@ act = parent act = act.parent() while act && !act.meta.panel unless act # no modal for top level panels @meta.panel.modal_action = false - if @meta.menus.panel_menu - _.remove(@meta.menus.panel_menu.entries, (m) -> m.name == 'cancel') + @meta.panel.footer = true unless @meta.panel.footer == false @initialize() initialize: -> @process_static_meta() - @process_meta() console.log "CREATE #{@action_info().action_resource}" process_static_meta: -> if @meta.menus _.each @meta.menus, (menu, name) => E2.process_menu @, name @@ -75,33 +74,29 @@ get_invoke = if info.invokable params.initial = true if !@action_invoked && params && info.method == 'get' $http[info.method](info.action_resource, if info.method == 'post' then params else (params: params)) else $q.when(data: (response: {})) + globals.action_pending = if @meta.panel then @ else @parent() + get_invoke.then (response) => - E2.merge(@meta, response.data.meta) + E2.merge(@, response.data) @process_meta() - _.assign(@, response.data.response) - if @meta.response - E2.merge(@, @meta.response) - delete @meta.response - @arguments = _.keys(response.data.response) - unless @meta.panel # persistent action - prnt = @parent() - throw "Attempted parent merge for root action: #{info.name}" unless prnt - E2.merge(prnt.meta, @meta) - _.assign(prnt, response.data.response) + (if @meta.reload_routes then $route.load_routes() else $q.when({})).then => + @arguments = _.keys(response.data) + unless @meta.panel # persistent action + prnt = @parent() + throw "Attempted parent merge for root action: #{info.name}" unless prnt + E2.merge(prnt, response.data) - # promise = if @meta.panel && !@action_invoked - # $q.when(promise) # .then -> response.data - if @meta.panel && !@action_invoked - @action_invoked = true - @panel_render() - # else $q.when() + globals.action_pending = false + if @meta.panel && !@action_invoked + @action_invoked = true + @panel_render() , (err) => - @parent().action_pending = false + globals.action_pending = false @handle_error(err, info, @element()) create_action: (name, sc, el) -> info = @find_action_info(name) info.action_resource = "#{@action_info().action_resource}/#{info.name}" @@ -124,29 +119,26 @@ create_action_path: (action_names, sc, elem) -> last_name = action_names.pop() _.reduce(action_names, ((pr, nm) -> pr.then (act) -> act.create_action(nm)), $q.when(@)).then (act) -> act.create_action(last_name, sc, elem).then (act) -> sc.action = act + globals: -> globals + action_pending: -> globals.action_pending == @ pre_invoke: -> - @parent().action_pending = true - @action_pending = true - # @parent().parent().action_pending = true if @parent().parent() post_invoke: -> - delete @parent().action_pending # = false - delete @action_pending - # @parent().parent().action_pending = false if @parent().parent() + invoke: -> args = arguments @pre_invoke(args...) @perform_invoke(args...).then (response) => @post_invoke(args...) @ save_state: () -> - _.each @meta.state, (s) => localStorageService.set("#{@action_info().action_resource}/#{s}", @[s]) + _.each @meta.state, (s) => localStorageService.set("#{globals.application}/#{@action_info().action_resource}/#{s}", @[s]) load_state: () -> - _.each @meta.state, (s) => E2.merge(@[s], localStorageService.get("#{@action_info().action_resource}/#{s}")) + _.each @meta.state, (s) => E2.merge(@[s], localStorageService.get("#{globals.application}/#{@action_info().action_resource}/#{s}")) destroy: (e) -> console.log "DESTROY #{@action_info().action_resource}" panel_render: -> @@ -197,10 +189,16 @@ @parent().invoke() panel_menu_cancel: -> @panel_close() + root: class RootAction extends Action + initialize: -> + super() + E2.merge(globals, @meta) + @meta = {} + default_action: class DefaultAction extends Action initialize: -> super() # console.log "DEFAULT ACTION: #{@action_info().action_resource}" @@ -309,22 +307,10 @@ @ui_state = {} @load_state() delete @query.order unless @meta.info[@query.order]?.sort # _.includes(@meta.fields, @query.order) _.each @query.search, ((sv, sn) => delete @query.search[sn] unless _.includes(@meta.search_fields, sn)) - - _.each @meta.info, (info, name) => - if info.remote_onchange - @scope().$watch (=> @query.search?[name]), (n) => if n? - params = value: @query.search[name] - params.record = @query.search if info.remote_onchange_record - @invoke_action(info.remote_onchange, params) - - if info.onchange - @scope().$watch (=> @query.search?[name]), (n) => if n? - @scope().$eval(info.onchange) - # $window.addEventListener 'beforeunload', (e, v) => @save_state() destroy: -> @save_state() super() @@ -372,10 +358,11 @@ query = _.cloneDeep(@query) delete query.search if _.isEmpty(E2.compact(query.search)) _.merge(query, arg) super(query).then => @ui = _.pick @query, ['order', 'asc', 'page'] + @ui.pagination_active = @ui.page != 0 || @entries.length >= @meta.config.per_page @scope().$broadcast 'render_table' load_new: -> @query.page = 0 @invoke() @@ -383,32 +370,44 @@ order: (col) -> @query.asc = if @query.order == col then !@query.asc else true @query.order = col @load_new() - prev_active: -> @query.page > 0 + prev_active: -> !@action_pending() && @query.page > 0 prev: -> - @query.page = Math.max(0, @query.page - @meta.config.per_page) - @invoke() + if @prev_active() + @query.page = Math.max(0, @query.page - @meta.config.per_page) + @invoke() - next_active: -> @entries.length == @meta.config.per_page + next_active: -> !@action_pending() && @entries.length == @meta.config.per_page next: -> - @query.page += @meta.config.per_page # min & count - @invoke() + if @next_active() + @query.page += @meta.config.per_page # min & count + @invoke() page_info: -> page = @ui.page / @meta.config.per_page + 1 - if @count then "#{page} / #{Math.ceil(@count / @meta.config.per_page)} (#{@count})" else page || '' + @meta.loc.page + ": " + if @count then "#{page} / #{Math.ceil(@count / @meta.config.per_page)} (#{@count})" else page || '' search_reset: -> E2.clean(@query.search) @scope().$broadcast "search_reset" @load_new() - search_live: (f) -> - @load_new() if @meta.info[f].search_live + search_field_change: (f) -> + info = @meta.info[f] + @scope().$eval(info.onchange) if info.onchange + + if remote_onchange = info.remote_onchange + params = value: @query.search[f] + params.record = @query.search if info.remote_onchange_record + @invoke_action(remote_onchange, params).then => + @load_new() if info.search_live + else + @load_new() if info.search_live + selected_class: (index) -> (entry = @entries[index]) && @selection && @selection[E2.id_for(entry, @meta)] && 'info' select: (index, ev) -> if ev.target.nodeName == "TD" @@ -418,10 +417,13 @@ if @selection[id] then delete @selection[id] else @selection[id] = rec selected_size: -> _.size(@selection) + selected_info: -> + @meta.loc.selected + ": " + @selected_size() + bulk_delete: class BulkDeleteAction extends Action invoke: -> super(ids: [_.keys(@parent().parent().selection)]).then => @parent().parent().selection = {} @@ -580,11 +582,11 @@ clear_record: -> _.each @dinfo.fields, (fk) => @record()[fk] = null # null_value reset: -> @clean() - @parentp().search_live?(@decode_field) + @parentp().search_field_change?(@decode_field) decode_description: (entry) -> fields = @meta.decode_fields ? @meta.fields fields.map((f) => E2.render_field(entry, f, @meta)).join(@meta.separator) @@ -618,11 +620,11 @@ else if @selected _(@dinfo.fields).zip(E2.split_keys(@selected)).each(([fk, k]) => record[fk] = E2.parse_entry(k, @parentp().meta.info[fk])).value else @clear_record() - @parentp().search_live?(@decode_field) + @parentp().search_field_change?(@decode_field) clean: -> @clear_selected() @clear_record() @@ -644,11 +646,11 @@ delete @decode if _.isEmpty(sel) else [ids, rec] = _(sel).toPairs().head() _(@dinfo.fields).zip(E2.split_keys(ids)).each(([k, v]) => record[k] = E2.parse_entry(v, @parentp().meta.info[k])).value @invoke_decode [rec] - @parentp().search_live?(@decode_field) + @parentp().search_field_change?(@decode_field) invoke_decode: (recs, f) -> if @multiple && _.size(recs) > @meta.show_max_selected @decode = "#{_.size(recs)} #{@meta.decode_selected}" else @@ -679,11 +681,11 @@ @decode = id: E2.id_for(@entry, @meta), value: @decode_description(@entry) @scope().$on "$typeahead.select", (e, v, index) => e.stopPropagation() _(@dinfo.fields).zip(E2.split_keys(@values[index].id)).each(([fk, k]) => @record()[fk] = E2.parse_entry(k, @parentp().meta.info[fk])).value - @parentp().search_live?(@decode_field) + @parentp().search_field_change?(@decode_field) @scope().$watch "action.decode", (e) => if e? @reset() if e.length == 0 load: (value) -> @@ -806,16 +808,16 @@ select: (files) -> _.each files, (file) => upload = $injector.get('Upload').upload url: "#{@action_info().action_resource}/upload", file: file upload.progress (e) => + globals.action_pending = false @progress = parseInt(100.0 * e.loaded / e.total) - # @parent().action_pending = true upload.success (data, status, headers, config) => - @files.push mime: file.type, name: file.name, rackname: data.response.rackname, id: data.response.id + @files.push mime: file.type, name: file.name, rackname: data.rackname, id: data.id @message = "Wysłano, #{file.name}" - # @parent().action_pending = false + globals.action_pending = false @sync_record() delete_file: (file) -> @scope().$broadcast 'confirm_delete', confirm: => @@ -845,8 +847,8 @@ _.each files, (file) => upload = $injector.get('Upload').upload url: "#{@action_info().action_resource}/upload", file: file upload.progress (e) => @progress = parseInt(100.0 * e.loaded / e.total) upload.success (data, status, headers, config) => - @file = mime: file.type, name: file.name, rackname: data.response.rackname, id: data.response.id + @file = mime: file.type, name: file.name, rackname: data.rackname, id: data.id @message = "Wysłano, #{file.name}" @sync_record()