Lua DOM Samples

-- Print a list of all of the user variables associated with a 
-- specified control
function print_variables(control_name)
    -- Get the DOM object for the control name passed in
    local domObject = gredom.get_object(control_name)
    if(domObject == nil) then
        print("Can't find name for " .. tostring(control_name))
        return
    end

    -- Get the variables defined on this DOM object
    local vars = domObject:get_variables()
    if(vars == nil or #vars == 0) then
        print("No variables for " .. control_name)
    else
        print("Variables for " .. control_name)
        for i=1,#vars do
            print("# " .. tostring(vars[i]))
        end
    end
end

-- Print out all of the screens where this control's container layer is
-- being used
function print_used_on_screens(control_name)
    -- Get the DOM object for the control name passed in
    local domObject = gredom.get_object(control_name)
    if(domObject == nil) then
        print("Can't find name for " .. tostring(control_name))
        return
    end

    -- Build up the full path to this object
    -- Walk up the tree looking at all parents adding screens we find
    local screen_list = {}
    local parent_list = {}
    table.insert(parent_list, domObject:get_parents())

    local i = 1
    while i <= #parent_list do
        local parents = parent_list[i]
        for p=1,#parents do
            -- If this was a screen, add it to our collection
            if parents[p]:get_type() == gredom.SCREEN then
                screen_list[parents[p]] = true
            else
                -- If this has parents of its own, then add them to the search
                -- list
                parents = parents[p]:get_parents()
                if(parents ~= nil and #parents > 0) then
                    table.insert(parent_list, parents)
                end
            end
        end
        i = i + 1
    end

    -- Print out all of the screens that we have identified
    print(control_name .. " is used on the following screens:")
    for screen,v in pairs(screen_list) do
        print("# " .. screen:get_name())
    end
end

-- Invoke our DOM example functions with the context control
function CBDom(mapargs)
    print_used_on_screens(mapargs.context_control)
    print_variables(mapargs.context_control)
end