Mercurial > wow > libmoduledbshare-1-0
comparison LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 40:e1053178ddbf
Updated comments.
Separated custom and default handler functions.
Added overwrite parameter to :AddSecondaryCommand()
| author | Andrew Knoll <andrewtknoll@gmail.com> |
|---|---|
| date | Tue, 09 Apr 2013 22:15:58 -0400 |
| parents | c6d1b0d7f8f9 |
| children | 047d80e6aadc |
comparison
equal
deleted
inserted
replaced
| 39:c6d1b0d7f8f9 | 40:e1053178ddbf |
|---|---|
| 9 -- methods of the DBGroup object described below.\\ | 9 -- methods of the DBGroup object described below.\\ |
| 10 -- \\ | 10 -- \\ |
| 11 -- **LibDualSpec Support**\\ | 11 -- **LibDualSpec Support**\\ |
| 12 -- LibModuleDBShare can use LibDualSpec to manage automatic profile switching with talent spec | 12 -- LibModuleDBShare can use LibDualSpec to manage automatic profile switching with talent spec |
| 13 -- changes. This integration is handled by the library; there is no need to use LibDualSpec | 13 -- changes. This integration is handled by the library; there is no need to use LibDualSpec |
| 14 -- on member databases directly. | 14 -- on member databases directly.\\ |
| 15 -- \\ | |
| 16 -- **Slash Command Support**\\ | |
| 17 -- LibModuleDBShare can associate a slash command with a DBGroup. The default handler function | |
| 18 -- for the slash command opens the root options panel.\\ | |
| 19 -- Additional handler functions can be registered to respond to specific arguments given to the | |
| 20 -- slash command. | |
| 15 -- | 21 -- |
| 16 -- @usage | 22 -- @usage |
| 17 -- local database; | 23 -- local database; |
| 18 -- -- this function is called after the ADDON_LOADED event fires | 24 -- -- this function is called after the ADDON_LOADED event fires |
| 19 -- function initializeDB() | 25 -- function initializeDB() |
| 22 -- if not group then | 28 -- if not group then |
| 23 -- group = LibStub("LibModuleDBShare-1.0"):NewGroup("Group Name", "A description for this group.", database); | 29 -- group = LibStub("LibModuleDBShare-1.0"):NewGroup("Group Name", "A description for this group.", database); |
| 24 -- else | 30 -- else |
| 25 -- group:AddDB(database); | 31 -- group:AddDB(database); |
| 26 -- end | 32 -- end |
| 33 -- -- if you want to add a slash command | |
| 34 -- if not group:HasSlashCommand() then | |
| 35 -- group:EnableSlashCommand("COMMAND_NAME", "/groupname"); | |
| 36 -- end | |
| 27 -- end | 37 -- end |
| 28 -- @class file | 38 -- @class file |
| 29 -- @name LibModuleDBShare-1.0 | 39 -- @name LibModuleDBShare-1.0 |
| 30 local MAJOR, MINOR = "LibModuleDBShare-1.0", 5 | 40 local MAJOR, MINOR = "LibModuleDBShare-1.0", 5 |
| 31 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR) | 41 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR) |
| 47 LibModuleDBShare.groups = LibModuleDBShare.groups or {}; | 57 LibModuleDBShare.groups = LibModuleDBShare.groups or {}; |
| 48 | 58 |
| 49 local DBGroup = {}; | 59 local DBGroup = {}; |
| 50 | 60 |
| 51 --- Creates a new DB group. | 61 --- Creates a new DB group. |
| 52 -- @param groupName The name of the new DB group, as shown in the options panel. | 62 -- @param groupName The name of the new DB group, as shown in the options panel. (string) |
| 53 -- @param groupDescription A description of the group to be shown in the root options panel. | 63 -- @param groupDescription A description of the group to be shown in the root options panel. (string) |
| 54 -- @param initialDB The first DB to add to the group. | 64 -- @param initialDB The first DB to add to the group. (table) |
| 55 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. | 65 -- @param usesDualSpec True if this group should use LibDualSpec, false otherwise. (boolean or nil) |
| 56 -- @usage | |
| 57 -- local myDB = LibStub("AceDB-3.0"):New("MySavedVar"); | |
| 58 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):NewGroup("MyAddonGroupName", "MyDescription", myDB, true) | |
| 59 -- @return the new DB group object | 66 -- @return the new DB group object |
| 60 -- @name LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB[, usesDualSpec]); | 67 -- @name LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB[, usesDualSpec]); |
| 61 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec) | 68 function LibModuleDBShare:NewGroup(groupName, groupDescription, initialDB, usesDualSpec) |
| 62 -- check to see if LibDualSpec has been loaded | 69 -- check to see if LibDualSpec has been loaded |
| 63 if not LibDualSpec then | 70 if not LibDualSpec then |
| 147 LibModuleDBShare.groups[groupName] = group; | 154 LibModuleDBShare.groups[groupName] = group; |
| 148 return group; | 155 return group; |
| 149 end | 156 end |
| 150 | 157 |
| 151 --- Retrieves an existing DB group. | 158 --- Retrieves an existing DB group. |
| 152 -- @param groupName The name of the DB group to retrieve. | 159 -- @param groupName The name of the DB group to retrieve. (string) |
| 153 -- @usage | |
| 154 -- local myAddonDBGroup = LibStub("LibModuleDBShare-1.0"):GetGroup("MyAddonGroupName") | |
| 155 -- @return the DB group object, or ##nil## if not found | 160 -- @return the DB group object, or ##nil## if not found |
| 156 function LibModuleDBShare:GetGroup(groupName) | 161 function LibModuleDBShare:GetGroup(groupName) |
| 157 if type(groupName) ~= "string" then | 162 if type(groupName) ~= "string" then |
| 158 error("Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string.", 2); | 163 error("Usage: LibModuleDBShare:GetGroup(groupName): 'groupName' must be a string.", 2); |
| 159 end | 164 end |
| 160 return LibModuleDBShare.groups[groupName]; | 165 return LibModuleDBShare.groups[groupName]; |
| 161 end | 166 end |
| 162 | 167 |
| 163 --- Adds a database to the group. | 168 --- Adds a database to the group. |
| 164 -- @param newDB The database to add. | 169 -- @param newDB The database to add. (table) |
| 165 -- @usage | |
| 166 -- myAddonDBGroup:AddDB(MyAddon.db) | |
| 167 function DBGroup:AddDB(newDB) | 170 function DBGroup:AddDB(newDB) |
| 168 -- verify parameters | 171 -- verify parameters |
| 169 if type(newDB) ~= "table" or not AceDB.db_registry[newDB] then | 172 if type(newDB) ~= "table" or not AceDB.db_registry[newDB] then |
| 170 error("Usage: DBGroup:AddDB(newDB): 'newDB' must be an AceDB-3.0 database.", 2); | 173 error("Usage: DBGroup:AddDB(newDB): 'newDB' must be an AceDB-3.0 database.", 2); |
| 171 elseif newDB.parent then | 174 elseif newDB.parent then |
| 240 end | 243 end |
| 241 | 244 |
| 242 -- slash command support | 245 -- slash command support |
| 243 | 246 |
| 244 --- Adds a slash command to the group. | 247 --- Adds a slash command to the group. |
| 248 -- @param slug The base identifier to use for the slash command. (string) | |
| 249 -- @param commandList The command itself, or a list of commands to use. (string or table) | |
| 250 -- @param handler A handler function for the command. If nil, defaults to a function that | |
| 251 -- calls the appropriate secondary command, or opens the root options panel. (function) | |
| 245 -- @name DBGroup:EnableSlashCommand(slug, commandList[, handler]) | 252 -- @name DBGroup:EnableSlashCommand(slug, commandList[, handler]) |
| 246 function DBGroup:EnableSlashCommand(slug, commandList, handler) | 253 function DBGroup:EnableSlashCommand(slug, commandList, handler) |
| 247 if self.slug then | 254 if self.slug then |
| 248 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): group already has a slash command.", 2); | 255 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): group already has a slash command.", 2); |
| 249 elseif type(slug) ~= "string" then | 256 elseif type(slug) ~= "string" then |
| 259 end | 266 end |
| 260 end | 267 end |
| 261 end | 268 end |
| 262 | 269 |
| 263 self.slug = slug; | 270 self.slug = slug; |
| 264 self.slashCmdHandler = handler; | |
| 265 self.subCmdList = {}; | 271 self.subCmdList = {}; |
| 266 if type(commandList) == "string" then | 272 if type(commandList) == "string" then |
| 267 _G["SLASH_"..slug.."1"] = commandList; | 273 _G["SLASH_"..slug.."1"] = commandList; |
| 268 else | 274 else |
| 269 for i = 1, #commandList do | 275 for i = 1, #commandList do |
| 270 _G["SLASH_"..slug..i] = commandList[i]; | 276 _G["SLASH_"..slug..i] = commandList[i]; |
| 271 end | 277 end |
| 272 end | 278 end |
| 273 | 279 |
| 274 SlashCmdList[slug] = function(msg, editBox) | 280 if handler then |
| 275 if self.slashCmdHandler then | 281 SlashCmdList[slug] = handler; |
| 276 self.slashCmdHandler(msg, editBox); | 282 else |
| 277 return; | 283 SlashCmdList[slug] = function(msg, editBox) |
| 278 end | 284 for cmd, func in pairs(self.subCmdList) do |
| 279 | 285 if msg == cmd then |
| 280 for cmd, func in pairs(self.subCmdList) do | 286 func("", editBox); |
| 281 if msg == cmd then | |
| 282 func("", editBox); | |
| 283 return; | |
| 284 elseif msg:len() > cmd:len() then | |
| 285 if msg:sub(1, cmd:len() + 1) == (cmd.." ") then | |
| 286 func(msg:sub(cmd:len() + 2), editBox); | |
| 287 return; | 287 return; |
| 288 elseif msg:len() > cmd:len() then | |
| 289 if msg:sub(1, cmd:len() + 1) == (cmd.." ") then | |
| 290 func(msg:sub(cmd:len() + 2), editBox); | |
| 291 return; | |
| 292 end | |
| 288 end | 293 end |
| 289 end | 294 end |
| 290 end | |
| 291 | 295 |
| 292 for k, button in pairs(InterfaceOptionsFrameAddOns.buttons) do | 296 for k, button in pairs(InterfaceOptionsFrameAddOns.buttons) do |
| 293 if button.element.name == self.name and button.element.collapsed then | 297 if button.element.name == self.name and button.element.collapsed then |
| 294 OptionsListButtonToggle_OnClick(button.toggle); | 298 OptionsListButtonToggle_OnClick(button.toggle); |
| 295 break; | 299 break; |
| 300 end | |
| 296 end | 301 end |
| 297 end | 302 InterfaceOptionsFrame_OpenToCategory(self.name); |
| 298 InterfaceOptionsFrame_OpenToCategory(self.name); | 303 end; |
| 299 end; | 304 end |
| 300 end | 305 end |
| 301 | 306 |
| 302 --- Adds an alias for the slash command | 307 --- Checks to see if this group has a slash command. |
| 308 -- @return ##true## if this group has a slash command, ##false## otherwise | |
| 309 function DBGroup:HasSlashCommand() | |
| 310 if self.slug then | |
| 311 return true; | |
| 312 else | |
| 313 return false; | |
| 314 end | |
| 315 end | |
| 316 | |
| 317 --- Adds an alias for the slash command. | |
| 318 -- @param alias The alternate name for the slash command. (string) | |
| 303 function DBGroup:AddSlashCommandAlias(alias) | 319 function DBGroup:AddSlashCommandAlias(alias) |
| 304 if type(alias) ~= "string" then | 320 if type(alias) ~= "string" then |
| 305 error("Usage: DBGroup:AddSlashCommandAlias(alias): 'alias' must be a string.", 2); | 321 error("Usage: DBGroup:AddSlashCommandAlias(alias): 'alias' must be a string.", 2); |
| 306 elseif not self.slug then | 322 elseif not self.slug then |
| 307 error("Usage: DBGroup:AddSlashCommandAlias(alias): slash commands for this group have not be enabled.", 2); | 323 error("Usage: DBGroup:AddSlashCommandAlias(alias): slash commands for this group have not be enabled.", 2); |
| 308 end | 324 end |
| 309 | 325 |
| 310 local i = 1; | 326 local i = 1; |
| 311 while _G["SLASH_"..self.slug..i] do | 327 while _G["SLASH_"..self.slug..i] do |
| 312 if _G["SLASH_"..self.slug..i] == alias then | 328 if _G["SLASH_"..self.slug..i] == alias then |
| 313 error("Usage: DBGroup:AddSlashCommandAlias(alias): 'alias' is already added.", 2); | 329 error("Usage: DBGroup:AddSlashCommandAlias(alias): alias '"..alias.."' is already in use by this command.", 2); |
| 314 end | 330 end |
| 315 i = i + 1; | 331 i = i + 1; |
| 316 end | 332 end |
| 317 | 333 |
| 318 _G["SLASH_"..self.slug..i] = alias; | 334 _G["SLASH_"..self.slug..i] = alias; |
| 319 end | 335 end |
| 320 | 336 |
| 321 --- Adds a secondary command handler to the slash command for this group. | 337 --- Adds a secondary command handler to the slash command for this group. |
| 322 function DBGroup:AddSecondaryCommand(name, handler) | 338 -- This handler will be called if the argument to the slash command matches the name provided. |
| 339 -- @param name The name of the secondary command. (string) | |
| 340 -- @param handler The function to handle the command. (function) | |
| 341 -- @param overwrite ##True## if you want to replace the currently registered command, ##false## | |
| 342 -- otherwise. (boolean) | |
| 343 -- @name DBGroup:AddSecondaryCommand(name, handler[, overwrite]) | |
| 344 function DBGroup:AddSecondaryCommand(name, handler, overwrite) | |
| 323 if type(name) ~= "string" then | 345 if type(name) ~= "string" then |
| 324 error("Usage: DBGroup:AddSecondaryCommand(name, handler): 'name' must be a string.", 2); | 346 error("Usage: DBGroup:AddSecondaryCommand(name, handler[, overwrite]): 'name' must be a string.", 2); |
| 325 elseif type(name) ~= "function" then | 347 elseif type(name) ~= "function" then |
| 326 error("Usage: DBGroup:AddSecondaryCommand(name, handler): 'handler' must be a function.", 2); | 348 error("Usage: DBGroup:AddSecondaryCommand(name, handler[, overwrite]): 'handler' must be a function.", 2); |
| 327 elseif not self.slashCmdList then | 349 elseif not self.slashCmdList then |
| 328 error("Usage: DBGroup:AddSecondaryCommand(name, handler): slash commands for this group have not be enabled.", 2); | 350 error("Usage: DBGroup:AddSecondaryCommand(name, handler[, overwrite]): slash commands for this group have not be enabled.", 2); |
| 329 end | 351 elseif type(overwrite) ~= "boolean" and type(overwrite) ~= "nil" then |
| 330 for k, v in pairs(self.subCmdList) do | 352 error("Usage: DBGroup:AddSecondaryCommand(name, handler[, overwrite]): 'overwrite' must be a boolean or nil", 2); |
| 331 if k == name then | 353 end |
| 332 error("Usage: DBGroup:AddSecondaryCommand(name, func): command '"..name.."' already exists.", 2); | 354 if not overwrite then |
| 333 end | 355 for k, v in pairs(self.subCmdList) do |
| 334 end | 356 if k == name then |
| 335 | 357 error("Usage: DBGroup:AddSecondaryCommand(name, handler[, overwrite]): command '"..name.."' already exists.", 2); |
| 358 end | |
| 359 end | |
| 360 end | |
| 361 | |
| 336 self.subCmdList[name] = handler; | 362 self.subCmdList[name] = handler; |
| 337 end | 363 end |
| 338 | 364 |
| 339 -- callback handlers (new profiles are handled by OnProfileChanged) | 365 -- callback handlers (new profiles are handled by OnProfileChanged) |
| 340 | 366 |
