comparison LibModuleDBShare-1.0/LibModuleDBShare-1.0.lua @ 39:c6d1b0d7f8f9

Added slash command support. Incremented minor version number.
author Andrew Knoll <andrewtknoll@gmail.com>
date Sun, 07 Apr 2013 22:50:00 -0400
parents f971130a84bb
children e1053178ddbf
comparison
equal deleted inserted replaced
38:d0cce6fa17a8 39:c6d1b0d7f8f9
25 -- group:AddDB(database); 25 -- group:AddDB(database);
26 -- end 26 -- end
27 -- end 27 -- end
28 -- @class file 28 -- @class file
29 -- @name LibModuleDBShare-1.0 29 -- @name LibModuleDBShare-1.0
30 local MAJOR, MINOR = "LibModuleDBShare-1.0", 4 30 local MAJOR, MINOR = "LibModuleDBShare-1.0", 5
31 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR) 31 local LibModuleDBShare, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
32 32
33 if not LibModuleDBShare then return end -- No upgrade needed 33 if not LibModuleDBShare then return end -- No upgrade needed
34 34
35 -- Lua functions 35 -- Lua functions
211 -- add to members list 211 -- add to members list
212 self.members[newDB] = namespace; 212 self.members[newDB] = namespace;
213 newDB.RegisterCallback(self, "OnDatabaseShutdown", "OnMemberShutdown"); 213 newDB.RegisterCallback(self, "OnDatabaseShutdown", "OnMemberShutdown");
214 end 214 end
215 215
216 -- LibDualSpec support
217
216 --- Checks to see if this group uses LibDualSpec. 218 --- Checks to see if this group uses LibDualSpec.
217 -- @return ##true## if this group uses LibDualSpec, ##false## otherwise 219 -- @return ##true## if this group uses LibDualSpec, ##false## otherwise
218 function DBGroup:IsUsingDualSpec() 220 function DBGroup:IsUsingDualSpec()
219 return self.usesDualSpec; 221 return self.usesDualSpec;
220 end 222 end
235 namespace.char.specGroup = self.syncDB.char.specGroup; 237 namespace.char.specGroup = self.syncDB.char.specGroup;
236 self.syncDB:CheckDualSpecState(); 238 self.syncDB:CheckDualSpecState();
237 end 239 end
238 end 240 end
239 241
242 -- slash command support
243
244 --- Adds a slash command to the group.
245 -- @name DBGroup:EnableSlashCommand(slug, commandList[, handler])
246 function DBGroup:EnableSlashCommand(slug, commandList, handler)
247 if self.slug then
248 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): group already has a slash command.", 2);
249 elseif type(slug) ~= "string" then
250 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): 'slug' must be a string.", 2);
251 elseif type(commandList) ~= "string" and type(commandList) ~= "table" then
252 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): 'commandList' must be a string or table.", 2);
253 elseif handler and type(handler) ~= "function" then
254 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): 'handler' must be nil or a function.", 2);
255 elseif type(commandList) == "table" then
256 for i = 1, #commandList do
257 if type(commandList[i]) ~= "string" then
258 error("Usage: DBGroup:EnableSlashCommand(slug, commandList[, handler]): 'commandList' must contain only strings.", 2);
259 end
260 end
261 end
262
263 self.slug = slug;
264 self.slashCmdHandler = handler;
265 self.subCmdList = {};
266 if type(commandList) == "string" then
267 _G["SLASH_"..slug.."1"] = commandList;
268 else
269 for i = 1, #commandList do
270 _G["SLASH_"..slug..i] = commandList[i];
271 end
272 end
273
274 SlashCmdList[slug] = function(msg, editBox)
275 if self.slashCmdHandler then
276 self.slashCmdHandler(msg, editBox);
277 return;
278 end
279
280 for cmd, func in pairs(self.subCmdList) do
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;
288 end
289 end
290 end
291
292 for k, button in pairs(InterfaceOptionsFrameAddOns.buttons) do
293 if button.element.name == self.name and button.element.collapsed then
294 OptionsListButtonToggle_OnClick(button.toggle);
295 break;
296 end
297 end
298 InterfaceOptionsFrame_OpenToCategory(self.name);
299 end;
300 end
301
302 --- Adds an alias for the slash command
303 function DBGroup:AddSlashCommandAlias(alias)
304 if type(alias) ~= "string" then
305 error("Usage: DBGroup:AddSlashCommandAlias(alias): 'alias' must be a string.", 2);
306 elseif not self.slug then
307 error("Usage: DBGroup:AddSlashCommandAlias(alias): slash commands for this group have not be enabled.", 2);
308 end
309
310 local i = 1;
311 while _G["SLASH_"..self.slug..i] do
312 if _G["SLASH_"..self.slug..i] == alias then
313 error("Usage: DBGroup:AddSlashCommandAlias(alias): 'alias' is already added.", 2);
314 end
315 i = i + 1;
316 end
317
318 _G["SLASH_"..self.slug..i] = alias;
319 end
320
321 --- Adds a secondary command handler to the slash command for this group.
322 function DBGroup:AddSecondaryCommand(name, handler)
323 if type(name) ~= "string" then
324 error("Usage: DBGroup:AddSecondaryCommand(name, handler): 'name' must be a string.", 2);
325 elseif type(name) ~= "function" then
326 error("Usage: DBGroup:AddSecondaryCommand(name, handler): 'handler' must be a function.", 2);
327 elseif not self.slashCmdList then
328 error("Usage: DBGroup:AddSecondaryCommand(name, handler): slash commands for this group have not be enabled.", 2);
329 end
330 for k, v in pairs(self.subCmdList) do
331 if k == name then
332 error("Usage: DBGroup:AddSecondaryCommand(name, func): command '"..name.."' already exists.", 2);
333 end
334 end
335
336 self.subCmdList[name] = handler;
337 end
338
240 -- callback handlers (new profiles are handled by OnProfileChanged) 339 -- callback handlers (new profiles are handled by OnProfileChanged)
241 340
242 function DBGroup:OnProfileChanged(callback, syncDB, profile) 341 function DBGroup:OnProfileChanged(callback, syncDB, profile)
243 if not self.squelchCallbacks then 342 if not self.squelchCallbacks then
244 for db, _ in pairs(self.members) do 343 for db, _ in pairs(self.members) do