| | 331 | |
|---|
| | 332 | |
|---|
| | 333 | MochiKit.DateTime |
|---|
| | 334 | ================= |
|---|
| | 335 | |
|---|
| | 336 | * JavaScript Date objects aren't very convenient |
|---|
| | 337 | * W3C profile ISO 8601 style timestamps are Good |
|---|
| | 338 | |
|---|
| | 339 | Dates |
|---|
| | 340 | ===== |
|---|
| | 341 | |
|---|
| | 342 | isoDate(str): |
|---|
| | 343 | Convert ISO 8601 date to a Date object |
|---|
| | 344 | |
|---|
| | 345 | toISODate(date): |
|---|
| | 346 | Convert a Date object to an ISO 8601 date |
|---|
| | 347 | |
|---|
| | 348 | americanDate(str): |
|---|
| | 349 | Convert an MM/DD/YYYY date to a Date object |
|---|
| | 350 | |
|---|
| | 351 | toAmericanDate(date): |
|---|
| | 352 | Convert a Date object to a M/D/YYYY date (1/1/2001) |
|---|
| | 353 | |
|---|
| | 354 | toPaddedAmericanDate(date): |
|---|
| | 355 | Convert a Date object to a MM/DM/YYYY date (01/01/2001) |
|---|
| | 356 | |
|---|
| | 357 | |
|---|
| | 358 | Time and Timestamps |
|---|
| | 359 | =================== |
|---|
| | 360 | |
|---|
| | 361 | isoTimestamp(str): |
|---|
| | 362 | Convert a YYYY-MM-DD hh:mm:ss or YYYY-MM-DDThh:mm:ssZ timestamp to a |
|---|
| | 363 | Date object |
|---|
| | 364 | |
|---|
| | 365 | toISOTime(date): |
|---|
| | 366 | Convert a Date object to a hh:mm:ss string |
|---|
| | 367 | |
|---|
| | 368 | toISOTimestamp(date, realISO=false): |
|---|
| | 369 | Convert a Date object to a YYYY-MM-DD hh:mm:ss string. If realISO is true, |
|---|
| | 370 | then use the proper YYYY-MM-DDThh:mm:ssZ form. |
|---|
| | 371 | |
|---|
| | 372 | |
|---|
| | 373 | MochiKit.Format |
|---|
| | 374 | =============== |
|---|
| | 375 | |
|---|
| | 376 | * JavaScript doesn't really do string formatting |
|---|
| | 377 | * Users like to see big numbers with thousands separators |
|---|
| | 378 | * Number formatting based on Java's Number Format Pattern Syntax |
|---|
| | 379 | |
|---|
| | 380 | |
|---|
| | 381 | Simple String Mangling |
|---|
| | 382 | ====================== |
|---|
| | 383 | |
|---|
| | 384 | strip(str, chars="\\s"): |
|---|
| | 385 | Return a string based on str with leading and trailing whitespace stripped. |
|---|
| | 386 | If the chars regex is specified, it will be used instead of the default. |
|---|
| | 387 | |
|---|
| | 388 | lstrip(str, chars="\\s"): |
|---|
| | 389 | strip only leading whitespace |
|---|
| | 390 | |
|---|
| | 391 | rstrip(str, chars="\\s"): |
|---|
| | 392 | strip only trailing whitespace |
|---|
| | 393 | |
|---|
| | 394 | |
|---|
| | 395 | Number Formatting |
|---|
| | 396 | ================= |
|---|
| | 397 | |
|---|
| | 398 | numberFormatter(pattern, placeholder="", locale="default"): |
|---|
| | 399 | Return a function that formats numbers using the given pattern |
|---|
| | 400 | |
|---|
| | 401 | Examples:: |
|---|
| | 402 | |
|---|
| | 403 | >>> dollarFormat = numberFormatter("$###,###.##") |
|---|
| | 404 | >>> dollarFormat(1234567.89) |
|---|
| | 405 | "$1,234,567.89" |
|---|
| | 406 | |
|---|
| | 407 | >>> percentFormat = numberFormatter("###,###%") |
|---|
| | 408 | >>> percentFormat(123.45) |
|---|
| | 409 | "12,345%" |
|---|
| | 410 | |
|---|
| | 411 | |
|---|
| | 412 | MochiKit.Logging |
|---|
| | 413 | ================ |
|---|
| | 414 | |
|---|
| | 415 | * alert() sucks |
|---|
| | 416 | * Debugging is hard enough |
|---|
| | 417 | * Not every browser has FireBug |
|---|
| | 418 | * I've had no luck with Venkman |
|---|
| | 419 | * Less luck with Microsoft's Script Debugger |
|---|
| | 420 | |
|---|
| | 421 | |
|---|
| | 422 | Simple logging |
|---|
| | 423 | ============== |
|---|
| | 424 | |
|---|
| | 425 | log(msg): |
|---|
| | 426 | Logs a message at the INFO level |
|---|
| | 427 | |
|---|
| | 428 | logDebug(msg): |
|---|
| | 429 | Logs a message at the DEBUG level |
|---|
| | 430 | |
|---|
| | 431 | logWarning, logError, logFatal... |
|---|
| | 432 | |
|---|
| | 433 | |
|---|
| | 434 | Where'd my log messages go? |
|---|
| | 435 | =========================== |
|---|
| | 436 | |
|---|
| | 437 | * In Safari, Firefox with FireBug, and Opera they will be logged to the |
|---|
| | 438 | appropriate console (but you can turn this off). |
|---|
| | 439 | * Will also go to any logging listener |
|---|
| | 440 | * Logging listeners are just functions that take log message objects |
|---|
| | 441 | |
|---|
| | 442 | |
|---|
| | 443 | Bookmarklet Debugging |
|---|
| | 444 | ===================== |
|---|
| | 445 | |
|---|
| | 446 | Just bookmark this:: |
|---|
| | 447 | |
|---|
| | 448 | javascript:MochiKit.Logging.logger.debuggingBookmarklet() |
|---|
| | 449 | |
|---|
| | 450 | Currently, this is a pop-up MochiKit.LoggingPane. |
|---|
| | 451 | |
|---|
| | 452 | |
|---|
| | 453 | MochiKit.LoggingPane |
|---|
| | 454 | ==================== |
|---|
| | 455 | |
|---|
| | 456 | * A usable MochiKit.Logging listener |
|---|
| | 457 | * Can be used in-line or as a pop-up window |
|---|
| | 458 | * Could be fancier, but it gets the job done |
|---|
| | 459 | |
|---|
| | 460 | |
|---|
| | 461 | Manually creating a LoggingPane |
|---|
| | 462 | =============================== |
|---|
| | 463 | |
|---|
| | 464 | Pop-up:: |
|---|
| | 465 | |
|---|
| | 466 | createLoggingPane() |
|---|
| | 467 | |
|---|
| | 468 | Inline:: |
|---|
| | 469 | |
|---|
| | 470 | createLoggingPane(true) |
|---|
| | 471 | |
|---|
| | 472 | |
|---|
| | 473 | MochiKit.DOM |
|---|
| | 474 | ============ |
|---|
| | 475 | |
|---|
| | 476 | * DOM is painful, but it doesn't have to be |
|---|
| | 477 | * Functions for finding, creating, modifying DOM nodes |
|---|
| | 478 | * Most functions will take either a string or a DOM node reference, saving |
|---|
| | 479 | you the trouble of getElementById(s), $(s) or similar. |
|---|
| | 480 | |
|---|
| | 481 | |
|---|
| | 482 | createDOM |
|---|
| | 483 | ========= |
|---|
| | 484 | |
|---|
| | 485 | * Does a good job of turning any object into a DOM node |
|---|
| | 486 | * Strings and numbers to text, flattens Arrays or iterators of anything |
|---|
| | 487 | * Extensible with an adapter registry |
|---|
| | 488 | |
|---|
| | 489 | |
|---|
| | 490 | createDOM example |
|---|
| | 491 | ================= |
|---|
| | 492 | |
|---|
| | 493 | createDOM(tagName, attributes, contents...) |
|---|
| | 494 | |
|---|
| | 495 | A simple list:: |
|---|
| | 496 | |
|---|
| | 497 | var node = createDOM("ul", null, |
|---|
| | 498 | createDOM("li", null, "first item"), |
|---|
| | 499 | createDOM("li", null, "second item")); |
|---|
| | 500 | |
|---|
| | 501 | Equivalent to:: |
|---|
| | 502 | |
|---|
| | 503 | <ul> |
|---|
| | 504 | <li>first item</li> |
|---|
| | 505 | <li>second item</li> |
|---|
| | 506 | </ul> |
|---|
| | 507 | |
|---|
| | 508 | |
|---|
| | 509 | But that's still ugly... |
|---|
| | 510 | ======================== |
|---|
| | 511 | |
|---|
| | 512 | We use aliases instead, for all of the common tags:: |
|---|
| | 513 | |
|---|
| | 514 | var node = UL(null, |
|---|
| | 515 | LI("first item"), |
|---|
| | 516 | LI("second item")); |
|---|
| | 517 | |
|---|
| | 518 | Note that MochiKit 1.3 and later allows a text node as the first |
|---|
| | 519 | parameter for convenience (saves a lot of "null" typing) |
|---|
| | 520 | |
|---|
| | 521 | |
|---|
| | 522 | Flattening lists |
|---|
| | 523 | ================ |
|---|
| | 524 | |
|---|
| | 525 | Functional programming comes in handy for DOM creation:: |
|---|
| | 526 | |
|---|
| | 527 | var items = ["first item", "second item"]; |
|---|
| | 528 | var node = UL(null, map(LI, items)); |
|---|
| | 529 | |
|---|
| | 530 | |
|---|
| | 531 | What about attributes? |
|---|
| | 532 | ====================== |
|---|
| | 533 | |
|---|
| | 534 | First parameter is either an object (attributes), or a string (text node):: |
|---|
| | 535 | |
|---|
| | 536 | var classes = repeat({"class": "myitemclass"}); |
|---|
| | 537 | var items = ["first item", "second item"]; |
|---|
| | 538 | var node = UL({"class": "mylistclass"}, |
|---|
| | 539 | map(LI, classes, items)); |
|---|
| | 540 | |
|---|
| | 541 | |
|---|
| | 542 | Alternating classes |
|---|
| | 543 | =================== |
|---|
| | 544 | |
|---|
| | 545 | More MochiKit.Iter goodies, good for table rows:: |
|---|
| | 546 | |
|---|
| | 547 | var classes = cycle({"class": "even"}, {"class": "odd"}); |
|---|
| | 548 | var items = ["first item", "second item"]; |
|---|
| | 549 | var node = UL(null, map(LI, classes, items)); |
|---|
| | 550 | |
|---|
| | 551 | |
|---|
| | 552 | Scraping text |
|---|
| | 553 | ============= |
|---|
| | 554 | |
|---|
| | 555 | Scraping text is useful for progressive enhancement... |
|---|
| | 556 | |
|---|
| | 557 | HTML:: |
|---|
| | 558 | |
|---|
| | 559 | <span id="scrape_me">text is <b>here</b></span> |
|---|
| | 560 | |
|---|
| | 561 | JavaScript:: |
|---|
| | 562 | |
|---|
| | 563 | >>> scrapeText("scrape_me"); |
|---|
| | 564 | "text is here" |
|---|
| | 565 | |
|---|
| | 566 | |
|---|
| | 567 | Reading forms |
|---|
| | 568 | ============= |
|---|
| | 569 | |
|---|
| | 570 | HTML:: |
|---|
| | 571 | |
|---|
| | 572 | <form id="formNode"> |
|---|
| | 573 | <input type="hidden" name="foo" value="1" /> |
|---|
| | 574 | <input type="hidden" name="bar" value="2" /> |
|---|
| | 575 | </form> |
|---|
| | 576 | |
|---|
| | 577 | JavaScript:: |
|---|
| | 578 | |
|---|
| | 579 | >>> formContents("formNode") |
|---|
| | 580 | [["foo", "bar"], ["1", "2"]] |
|---|
| | 581 | |
|---|
| | 582 | |
|---|
| | 583 | Manipulating DOM nodes |
|---|
| | 584 | ====================== |
|---|
| | 585 | |
|---|
| | 586 | appendChildNodes(parentNode, childNode...): |
|---|
| | 587 | Add nodes to parentNode using the DOM coercion rules from createDOM |
|---|
| | 588 | |
|---|
| | 589 | replaceChildNodes(parentNode, childNode...): |
|---|
| | 590 | Remove all children from parentNode, then appendChildNodes |
|---|
| | 591 | |
|---|
| | 592 | swapDOM(dest, src): |
|---|
| | 593 | Replace dest with src (if src is null, then dest is removed) |
|---|
| | 594 | |
|---|
| | 595 | |
|---|
| | 596 | Manipulating DOM attributes |
|---|
| | 597 | =========================== |
|---|
| | 598 | |
|---|
| | 599 | setNodeAttribute(node, attr, value): |
|---|
| | 600 | Set the node attribute attr=value |
|---|
| | 601 | |
|---|
| | 602 | updateNodeAttributes(node, attrs): |
|---|
| | 603 | Set the node attributes based on the object attrs |
|---|
| | 604 | |
|---|